分类: LINUX
2014-07-24 17:06:05
1 #!/bin/bash 2 # arglist.sh 3 # 多使用几个参数来调用这个脚本, 比如"one two three". 4 5 E_BADARGS=65 6 7 if [ ! -n "$1" ] 8 then 9 echo "Usage: `basename $0` argument1 argument2 etc." 10 exit $E_BADARGS 11 fi 12 13 echo 14 15 index=1 # 起始计数. 16 17 echo "Listing args with \"\$*\":" 18 for arg in "$*" # 如果"$*"不被""引用,那么将不能正常地工作. 19 do 20 echo "Arg #$index = $arg" 21 let "index+=1" 22 done # $* 将所有的参数看成一个单词. 23 echo "Entire arg list seen as single word." 24 25 echo 26 27 index=1 # 重置计数(译者注: 从1开始). 28 # 如果你写这句会发生什么? 29 30 echo "Listing args with \"\$@\":" 31 for arg in "$@" 32 do 33 echo "Arg #$index = $arg" 34 let "index+=1" 35 done # $@ 把每个参数都看成是单独的单词. 36 echo "Arg list seen as separate words." 37 38 echo 39 40 index=1 # 重置计数(译者注: 从1开始). 41 42 echo "Listing args with \$* (unquoted):" 43 for arg in $* 44 do 45 echo "Arg #$index = $arg" 46 let "index+=1" 47 done # 未引用的$*将会把参数看成单独的单词. 48 echo "Arg list seen as separate words." 49 50 exit 0