位置变量
时间:2010-08-30 来源:sorghum_cu
如果要想一个shell搅拌传递信息,可以使用位置参数完成此功能。参数相关书目传入搅拌,数目可以任意多,但是只有前9个可以被访问(用shift名利可以改变此限制)。
$0代表了此脚本名
举例:
myadmin@myubuntu:~$ cat position_param
#!/bin/bash
#position_param
echo "this is the file name:$0"
echo "this is 1st param:$1"
echo "this is 2nd param:$2"
echo "this is 3rd param:$3"
yadmin@myubuntu:~$ ./position_param America Brazil
this is the file name:./position_param
this is 1st param:America
this is 2nd param:Brazil
this is 3rd param:
myadmin@myubuntu:~$ ./position_param America Brazil China
this is the file name:./position_param
this is 1st param:America
this is 2nd param:Brazil
this is 3rd param:China
myadmin@myubuntu:~$ ./position_param America Brazil China Denmark
this is the file name:./position_param
this is 1st param:America
this is 2nd param:Brazil
this is 3rd param:China
小结:
如果输入数少于既定值,则未输入的部分为空
如果输入数多于既定值,则多余输出部分无效
myadmin@myubuntu:~$ cat show_filename
#!/bin/bash
#show_filename
echo "this is `basename $0` calling"
myadmin@myubuntu:~$ ./show_filename
this is show_filename calling