Shell脚本学习之基本参数

本文介绍了Shell脚本中常见的命令行参数及其含义,包括 $?、$#、$0、$@ 和 $* 的区别和使用场景,并通过示例演示了它们如何在实际脚本中发挥作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Shell脚本学习笔记---基本参数解析

常见命令行参数意义

    菜鸟们总会被shell脚本中常见变量弄混淆,这些变量有$?, $#, $0, $@, $*

$?表示前一个命令的返回值,0表示正确,非0值表示错误

#test05.sh #!/bin/bash

sh test01.sh

if [ $? -ne 0 ]; then     echo "test01.sh excute failed!" else     echo "test01.sh excute right!" fi

#test01.sh
#!/bin/bash

array=(zl love ll forever)

for((i=0;i<${#array[@]};i++)); do
    printf "%s " ${array[$i]}
done
printf "\n"


[work@yx-dpfqa-a111.yx01.baidu.com shell]$ sh test05.sh
zl love ll forever
test01.sh excute right!

$#表示命令行参数个数,相当于C语言中的argc

$0表示本脚本的名称

[work@yx-dpfqa-a111.yx01.baidu.com shell]$ vim test03.sh

#!/bin/bash

echo $0

结果如下
 [work@yx-dpfqa-a111.yx01.baidu.com shell]$ sh test03.sh                                                                                                       
test03.sh
                                                                                                                                                                                                 

 所以$0参数还是很有用的,尤其在目录结构上,例如: 

#!/bin/bash

cd `dirname $0`
source ../conf.sh

$@和$*都表示命令行参数,二者存在细微的区别:

共同之处:

echo "begin:"
for i in $@
do
    echo $i
done;

result:
[work@yx-dpfqa-a111.yx01.baidu.com shell]$ sh test05.sh zl love ll 1314                                                                                
begin:                                                                                                                                                                                                       
zl                                                                                                                                                                                                               
love                                                                                                                                                                                                           
ll                                                                                                                                                                                                               
1314
                                                                                                                                                                                                        

echo "begin:"
for i in $*
do
    echo $i
done;


[work@yx-dpfqa-a111.yx01.baidu.com shell]$ sh test05.sh zl love ll 1314                      
begin:                                                                                       
zl                                                                                           
love                                                                                         
ll                                                                                       
1314

不同之处:

for i in "$@"
do
    echo $i
done;

result:
[work@yx-dpfqa-a111.yx01.baidu.com shell]$ sh test05.sh zl love ll 1314                                                                                 
begin:                                                                                                                                                                                                         
zl                                                                                                                                                                                                                  
love                                                                                                                                                                                                             
ll                                                                                                                                                                                                                  
1314
                                                                                                                                                                                                          

echo "begin:"
for i in "$*"
do
    echo $i
done;


[work@yx-dpfqa-a111.yx01.baidu.com shell]$ sh test05.sh zl love ll 1314                                                                               
begin:                                                                                                                                                                                                      
zl love ll 1314
                                                                                                                                                                                         

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值