为什么从不存在的命令行参数分配成功?

时间:2018-01-14 19:09:30

标签: bash shell

我有一个BASH脚本来添加两个数字,我想通过命令行参数传递两个数字。

cat add.sh

a=$1
echo "exit status for a " $?

b=$2
echo "exit status for b " $?

sum=`expr $a + $b`
echo "exit status for sum " $?

echo "result is " $sum
echo "exit status for result " $?

当我运行上面的脚本而不提供任何值或参数时,我得到以下输出。

$ ./add.sh 
exit status for a  0  (expecting 1 here)
exit status for b  0 (expecting 1 here)
expr: syntax error
exit status for sum  2  
result is 
exit status for result  0 (expecting 1 here)

我期待$的价值?当我没有通过命令行提供任何参数时,为1,因此分配a和b的值必定是错误。

有人解释为什么当通过命令行提供NO值时,a = $ 1或b = $ 2不会返回退出状态1? 为什么它返回0?

0 个答案:

没有答案
相关问题