shell 脚本传参之命令行
例子:
#!/bin/bash
echo $0
echo "$1 parameter $2 parameter"
echo "parameter($#)"
echo $*
echo $@
echo $_
打印结果:
xiewenhao@Zienon:~/xwh$ ./command.sh 1 2
./command.sh
1 parameter 2 parameter
parameter(2)
1 2
1 2
2
xiewenhao@Zienon:~/xwh$ ./command.sh 1 2 3
./command.sh
1 parameter 2 parameter
parameter(3)
1 2 3
1 2 3
3
xiewenhao@Zienon:~/xwh$ ./command.sh 1 2 3 4
./command.sh
1 parameter 2 parameter
parameter(4)
1 2 3 4
1 2 3 4
4
shell之扩展传参(getopts用法)
#!/bin/bash
echo "OPTIND starts at $OPTIND"
while getopts ":pq:" optname
do
case "$optname" in
"p")
echo "Option $optname is specified"
;;
"q")
echo "option $optname has value $OPTARG"
esac
echo "OPTIND is now $OPTIND"
done
运行结果:
xiewenhao@Zienon:~/xwh$ ./opt.sh -q
OPTIND starts at 1
OPTIND is now 2