shell 嵌套条件语句时,尽量把靠近两次判断

本文介绍了一个Shell脚本中关于参数数量校验的方法,并展示了如何根据不同情况给出帮助信息或错误提示。通过示例代码解释了如何判断传入参数的数量是否符合预期,以及如何根据参数值提供帮助信息。

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

 if [ $# != 2 ]
then
        if [ $1 = "h" -o $1 = "H" ]
        then
                echo "help info"
        else
                echo "ERROR: Invalid argument amount! Plz input 2 directory names!"
        fi
        echo "**************************************************************"
        echo "*               by Richard Zhang, Oct, 2008.                 *"
        echo "*            China Contruction Bank, SuZhou Branch           *"
        echo "*            Usage example: 'sh $0 <dir1> <dir2>'            *"
        echo "*            for help: 'sh $0 h'                             *"
        echo "**************************************************************"
        exit 0
fi

没有问题,但是,如果
if [ $# != 2 ]
then
        echo "**************************************************************"
        echo "*               by Richard Zhang, Oct, 2008.                 *"
        echo "*            China Contruction Bank, SuZhou Branch           *"
        echo "*            Usage example: 'sh $0 <dir1> <dir2>'            *"
        echo "*            for help: 'sh $0 h'                             *"
        echo "**************************************************************"
        if [ $1 = "h" -o $1 = "H" ]
        then
                echo "help info"
        else
                echo "ERROR: Invalid argument amount! Plz input 2 directory names!"
        fi

        exit 0
fi

则在FreeBSD6.1下运行时,提示“[: =: unexpected operator”
真实奇怪。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值