View Code
1 #!/bin/bash 2 #arglist.h 3 #2012-06-14 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 echo 13 14 index=1 15 #注意 16 echo "Listing args with \"\$*\":" 17 for arg in "$*" # "" 18 do 19 echo "Arg #$index = $arg" 20 let "index+=1" 21 done 22 23 echo "Entire arg list seen as single word." 24 echo 25 26 index=1 27 28 echo "Listing args with\"\$@\":" 29 for arg in "$@" 30 do 31 echo "Arg #$index = $arg" 32 let "index+=1" 33 done 34 echo "Arg list seen as seperate words." 35 36 echo 37 38 index=1 39 echo "Listing args with \$* (unquoted):" 40 for arg in $* 41 do 42 echo "Arg #$index = $arg" 43 let "index+=1" 44 done 45 echo "Arg list seen as separate words." 46 exit 0
调试的时候直接用了
sh -x arglist.sh t1 t2 t3 t4 t5 t6 t7
结果就悲催了:
+ E_BADARGS=65 + [ ! -n t1 ] + echo + index=1 + echo Listing args with "$*": Listing args with "$*": + echo Arg #1 = t1 t2 t3 t4 t5 t6 t7 Arg #1 = t1 t2 t3 t4 t5 t6 t7 + let index+=1 arglist.sh: 20: arglist.sh: let: not found + echo Entire arg list seen as single word. Entire arg list seen as single word. + echo + index=1 + echo Listing args with"$@": Listing args with"$@": + echo Arg #1 = t1 Arg #1 = t1 + let index+=1 arglist.sh: 32: arglist.sh: let: not found + echo Arg #1 = t2 Arg #1 = t2 + let index+=1 arglist.sh: 32: arglist.sh: let: not found + echo Arg #1 = t3 Arg #1 = t3 + let index+=1 arglist.sh: 32: arglist.sh: let: not found + echo Arg #1 = t4 Arg #1 = t4 + let index+=1 arglist.sh: 32: arglist.sh: let: not found + echo Arg #1 = t5 Arg #1 = t5 + let index+=1 arglist.sh: 32: arglist.sh: let: not found + echo Arg #1 = t6 Arg #1 = t6 + let index+=1 arglist.sh: 32: arglist.sh: let: not found + echo Arg #1 = t7 Arg #1 = t7 + let index+=1 arglist.sh: 32: arglist.sh: let: not found + echo Arg list seen as seperate words. Arg list seen as seperate words. + echo + index=1 + echo Listing args with $* (unquoted): Listing args with $* (unquoted): + echo Arg #1 = t1 Arg #1 = t1 + let index+=1 arglist.sh: 43: arglist.sh: let: not found + echo Arg #1 = t2 Arg #1 = t2 + let index+=1 arglist.sh: 43: arglist.sh: let: not found + echo Arg #1 = t3 Arg #1 = t3 + let index+=1 arglist.sh: 43: arglist.sh: let: not found + echo Arg #1 = t4 Arg #1 = t4 + let index+=1 arglist.sh: 43: arglist.sh: let: not found + echo Arg #1 = t5 Arg #1 = t5 + let index+=1 arglist.sh: 43: arglist.sh: let: not found + echo Arg #1 = t6 Arg #1 = t6 + let index+=1 arglist.sh: 43: arglist.sh: let: not found + echo Arg #1 = t7 Arg #1 = t7 + let index+=1 arglist.sh: 43: arglist.sh: let: not found + echo Arg list seen as separate words. Arg list seen as separate words. + exit 0
百度了一下,大概是因为:
/bin/sh指向了dash而不是bash,dash不支持let命令。
所以改用,
bash -x arglist.sh t1 t2 t3 t4 t5 t6 t7
结果就OK了,若非是用sh -x,还遇不到这个小问题,呵呵。
很水的东东,私录一下。