shell脚本的参数问题

一个简单例子

para.sh
########################
#!/bin/sh
#scriptname:para

echo "this script is call $0"
echo "$0 $1 $2"
echo "The number  of  positionalparameters is $#"

echo "$* is the dollar star"

echo "$@ is the dollar at"

echo " "$*" is the dollar star which has quoation marks"

echo " "$@" is the dollar at which has quoation marks"
################
[root@yqrh5u2]chmod +x para.sh
[root@yqrh5u2 rbf]# ./para.sh  1st_name 2nd_para 3rd_last
this script is call ./para.sh
./para.sh 1st_name 2nd_para
The number  of  positionalparameters is 3
1st_name 2nd_para 3rd_last is the dollar star
1st_name 2nd_para 3rd_last is the dollar at
1st_name 2nd_para 3rd_last is the dollar star which has quoation marks
1st_name 2nd_para 3rd_last is the dollar at which has quoation marks




Positional Parameter    What It References
 
$0          References the name of the script,脚本的名称
 
$#          Holds the value of the number of positional parameters,参数的个数
 
$*           Lists all of the positional parameters,列出所有的参数
 
$@          Means the same as $*, except when enclosed in double quotes
 
"$*"        Expands to a single argument (e.g., "$1 $2 $3"),
 
"$@"       Expands to separate arguments (e.g., "$1" "$2" "$3")
 
$1 .. $9    References up to nine positional parameters,应用对应的参数


 $* and $@ 区别,只有单他们有双引号是才有区别:
 $ set 'apple pie' pears peaches

6   $ for i in $@
    > do
    > echo $i
    > done

    apple
    pie
    pears
    peaches



7   $ set 'apple pie' pears peaches
8   $ for i in "$@"         # At last!!
    > do
    > echo $i
    > done

    apple pie
    pears
    peaches



脚本的参数至少是1,
name=${1:?"requires an argument" }

echo Hello $name
 :? 将会检验$1是否有值,加入没有,脚本退出,并且报错误信息。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值