shell脚本中位置参数 $0 $1 .. $* $#的备忘

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

#!/bin/bash
MIN_PARAMS=10
echo
echo "The name of this script is \"`basename $0`\"."

echo
if [ -n "$1" ]; then
 echo "param #1 is $1"
fi
if [ -n "$2" ]; then
  echo "param #2 is $2"
fi
if [ -n "$3" ]; then
  echo "param #3 is $3"
fi
echo "______________________________"
echo "All command-line params are : $*"
echo '$0 =' "$0"
echo '$# =' "$#"
echo '$@ =' "$@"
echo '$* =' "$*"
if [ $# -lt $MIN_PARAMS ]
then 
   echo
   echo "this script need at least $MIN_PARAMS"
fi
exit 0
________________________________________________________
执行 $ /tmp/ts.sh 1 2 3 4 5 6
打印出来的结果

The name of this script is "ts.sh".

param #1 is 1
param #2 is 2
param #3 is 3
______________________________
All command-line params are : 1 2 3 4 5 6
$0 = /tmp/ts.sh
$# = 6
$@ = 1 2 3 4 5 6
$* = 1 2 3 4 5 6
this script need at least 10