一、应用场景
# /usr/bin/mysql -u root -p # /usr/sbin/apachectl -k stop
脚本后面的数据,是如何传递到脚本中的呢?
二、关于【$#】【$@】【$*】 的使用
先看一组示意图
# /path/to/scriptname opt1 opt2 opt3 opt4
$0 $1 $2 $3 $4
- 【$#】:表示参数的总数.
- 【$@】:$1、$2……$5,按顺序表示每一个参数.
- 【$*】:表示所有参数的名称.
举例说明
脚本:sh07.sh #!/bin/bash # Program # test # History # 2015/03/31 woodie first release PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH echo "The script name is ==>$0" echo "Total parameter number is ==>$#" [ "$#" -lt 2 ] && echo -e "The number of parameter is less than 2. Stop here."\ && exit 0 echo "Your whole parameter is ==>$*" echo "The 1st parameter ==>$1" echo "The 2nd parameter ==>$2" 运行结果: # sh sh07.sh aaa bbb ccc The script name is ==>sh07.sh Total parameter number is ==>3 Your whole parameter is ==>aaa bbb ccc The 1st parameter ==>aaa The 2nd parameter ==>bbb
三、shift 参数偏移
举例说明用法:
脚本:sh08.sh #!/bin/bash # Program # test # History # 2015/03/31 woodie first release PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH echo "Total parameter number is ==>$#" echo "Your whole parameter is ==>$*" shift echo "Total parameter number is ==>$#" echo "Your whole parameter is ==>$*" shift 3 echo "Total parameter number is ==>$#" echo "Your whole parameter is ==>$*" 运行结果: # sh sh08.sh aaa bbb ccc ddd eee fff Total parameter number is ==>6 Your whole parameter is ==>aaa bbb ccc ddd eee fff Total parameter number is ==>5 Your whole parameter is ==>bbb ccc ddd eee fff Total parameter number is ==>2 Your whole parameter is ==>eee fff