v>

shell脚本的位置参数


命令可以带有选项与参数,同样shell script脚本文件名后面也可以带有参数。
例如要重新启动系统的网络:
            [peter@study ~]$ file /etc/init.d/network  #使用file查询文件是个bash的可执行script!
            /etc/init.d/network: Bourne-Again shell script, ASCII text executable
            [peter@study ~]$ /etc/init.d/network restart
            
restart重启,上面/etc/init.d/network restart可以重新启动服务!
如果在/etc/init.d/network后面加上stop就可以关闭服务了!
可以使用read依据程序的执行给予一些变量去进行不同的任务!但read得要手动键盘输入一些判断式。
通过命令后面接参数, 就能够处理完毕而不需要手动输入变量!
script针对参数已经有设定好一些变量名称了!对应如下:
            /path/to/scriptname opt1 opt2 opt3 opt4
            $0                  $1   $2   $3   $4
            
脚本文件名为$0这个变量,第一个参数就是$1,只要在script里面善用$1的话,就可以很简单的立即下达某些命令功能了!
除了这些数字的变量之外,还有一些较为特殊的变量可以在script内使用来呼叫这些参数喔!
$# :代表后接的参数个数,以上表为例这里显示为4;
$@ :代表 "$1" "$2" "$3" "$4" 之意,每个变量是独立的(用双引号括起来);
$* :代表 "$1c$2c$3c$4" ,其中c为分隔字符,默认为空格键, 所以本例中代表 "$1 $2 $3 $4"之意。
$@ 与 $* 还是有所不同!不过,一般使用情况下可以直接记忆$@即可! 假设我要执行一个可以携带参数的script,执行该脚本后屏幕会显示如下的数据: 程序的文件名为何? 共有几个参数? 若参数的个数小于2 则告知用户参数数量太少 全部的参数内容为何? 第一个参数为何? 第二个参数为何
[peter@study bin]$ vim how_paras.sh
#!/bin/bash
# Program:
# Program shows the script name, parameters...
# History:
# 2015/07/16 VBird 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 "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}"
执行结果如下:
            [peter@study bin]$ sh how_paras.sh theone haha quot
            The script name is: how_paras.sh
            Total parameter number is: 3
            Your whole parameter is: 'theone haha quot'
            The 1st parameter: theone
            The 2nd parameter: haha
            
shift:造成参数变量号码偏移
脚本后面所接的变量能够进行偏移(shift)
我们将how_paras.sh的内容稍作变化一下,用来显示每次偏移后参数的变化情况:
[peter@study bin]$ vim shift_paras.sh
#!/bin/bash# Program:
# Program shows the effect of shift function.
# History:
# 2009/02/17 VBird 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      # 进行第一次一个变量的 shift

echo "Total parameter number is: $#"
echo "Your whole parameter is: '$@'"
shift 3   # 进行第二次三个变量的 shift
echo "Total parameter number is: $#"
echo "Your whole parameter is: '$@'"
执行成果如下:
            [peter@study bin]$ sh shift_paras.sh one two three four five six #给予六个参数
            Total parameter number is: 6       #最原始的参数变量情况
            Your whole parameter is: 'one two three four five six'
            Total parameter number is: 5      #第一次偏移,看底下发现第一个 one 不见了
            Your whole parameter is: 'two three four five six'   
            Total parameter number is: 2
            Your whole parameter is: 'five six'    #第二次偏移掉三个,two three four 不见了
            
shift会移动变量,shift后面可以接数字,代表拿掉最前面的几个参数的意思。
上面的执行结果中,第一次进行shift后他的显示情况是two three four five six,
就剩下五个!第二次直接拿掉三个,就变成five six!
上面例子几乎都是利用bash的相关功能.

initroot编辑整理,转载请注明www.initroot.com

100次点赞 100次阅读