1、命令行参数
(1)读取参数
bash shell使用位置参数的特殊变量记录命令行参数,$0是程序名,$1~$9依次为输入参数;
如果脚本需要多余9个变量,需要将变量数字加花括号,如${10};
命令行输入的参数需要以空格分隔,当空格作为参数的一部分时可以使用引号分开;
在脚本中对命令行参数进行处理时需要对参数进行有效性判断,如:
1 $ cat test
2 #!/bin/bash
3 #test param
4 if [ -n "$1" ]; then
5 echo Nice to meet $1
6 else
7 echo "no paran input"
8 fi
(2)读取程序名
可以使用$0获取命令行启动的程序的名字;
当传给$0变量的字符串是整个脚本路径时,$0变量则是整个路径,而不仅仅是程序名;
使用basename命令可以获取程序名而不包括路径,如:
(3)参数计数
$#变量保存了执行脚本时命令行输入参数的个数,可以在脚本的任何地方访问$#变量的值;
可以通过$#变量访问命令行的最后一个参数:${!#} —— 由于在花括号内不能使用美元符,可以使用叹号替换;
1 $ cat test
2 #!/bin/bash
3 # test $#
4 params=$#
5 echo the last param is $params
6 echo the last param is ${!#}
7 $ ./test 1 2 3 4 5
8 the last param is 5
9 the last param is 5
(4)获取所有的参数
可以通过 $#变量获取命令行参数的个数,依次对所有参数进行处理;
$*和$@变量,提供了对所有参数的快速访问;
$*变量会将所有参数当做单个参数,而$@变量会单独处理每个参数,如:
1 $ cat test
2 #!/bin/bash
3 # test $* and $@
4 echo "\$* is $*"
5 echo "\$@ is $@"
6 count=1
7 for param in "$*"; do
8 echo "\$* param #$count=$param"
9 count=$[ $count + 1 ]
10 done
11 count=1
12 for param in "$*"; do
13 echo "\$* param #$count=$param"
14 count=$[ $count + 1 ]
15 done
16 $ ./test a b c
17 $* is a b c
18 $@ is a b c
19 $* param #1=a b c
20 $@ param #1=a
21 $@ param #2=b
22 $@ param #3=c
2、参数处理
(1)移动变量
shift命令可以用来移动命令行参数。
默认情况下,shift命令会将命令行参数减一。$n的值依次递减,$1的值会被删除;
使用shift时,$0的值不改变;
可以使用shift n 命令修改每次移动的位数;
注意,在使用shift命令时,一旦参数被移除后,不可恢复。
(2)使用getopt命令处理选项
选项(options)是只命令行参数中单破折号后面的单个字母;
getopt命令可以用来处理命令行选项和参数,格式:
getopt optstring options params
optstring定义了命令行有效的选项字母,和那些选项需要参数值(在选项后加冒号表示);
例如:
$getopt ab:c -a -b test1 -c test2 test3
-a -b test1 -c -- test2 test3
使用--将选项和非选项自带的参数分开;
如果定义了不在optstring的选项,getopt命令会产生一条错误,-p选项可以忽略错误消息;
在脚本中使用getopt命令,可以使用set命令,set命令的--选项可以将命令行参数替换成set命令的命令行参数;
$cat test.sh
#!/bin/bash
# extracting command line options and values with getopt
set -- `getopt -q ab:c "$@"`
while [ -n "$1" ]; do
case "$1" in
-a) echo "find -a option" ;;
-b) echo "find -b option,and value is $2"
shift ;;
-c) echo "find -c option" ;;
--) shift
break ;;
*) echo "$1 is not a option"
esac
shift
done
count=1
for param in "$@"; do
echo "parameter #$count: $param"
count=$[ $count + 1 ]
done
$ ./test.sh -a -b 111 -c 222 333
find -a option
find -b option,and value is '111'
find -c option
parameter #1: '222'
parameter #2: '333'
(3)getopts命令
getopt命令可以将命令行上的所有选项和参数处理后只生成一个输出;
getopts命令每次调用时只处理一个命令行上检测的参数,处理完所有的参数后,它会退出并返回一个大于0的状态;
格式:
getopts optstring variable
optstring和getopt命令基本一致,需要去掉错误提示时,只需要在optstring前加一个冒号;
getopts将当前参数保存在variable中;
getopts会用到两个环境变量,如果选项需要跟一个参数,OPTARG环境变量保存这个参数;OPTIND环境变量保存了参数列表中getopts正在处理的参数位置;
1 $ cat test.sh
2 #!/bin/bash
3 # processing options and parameters with getopts
4 while getopts :ab:cd opt ; do
5 case "$opt" in
6 a) echo "find the -a option";;
7 b) echo "find the -b option, with value $OPTARG";;
8 c) echo "find the -c option";;
9 d) echo "find the -d option";;
10 *) echo "unknown option $opt";;
11 esac
12 done
13 shift $[ $OPTIND - 1 ]
14 count=1
15 for param in "$@"; do
16 echo "parameter $count : $param"
17 count=$[ $count + 1 ]
18 done
19 $ ./getopt.sh -a -btest1 -c test2 test3
20 find -a option
21 find -b option,and value is 'test1'
22 find -c option
23 parameter #1: 'test2'
24 parameter #2: 'test3'
getopts在使用时会移除选项前的单线破折号;
getopts允许选项和参数拼接在一起使用,不用加空格;
getopts会把所有未定义的选项统一输出成问号;