如果使用shell脚本ELIF网络 [英] using if elif fi in shell scripts

查看:196
本文介绍了如果使用shell脚本ELIF网络的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道该怎么办了,如果在外壳多次测试,我无法写这个剧本:

I'm not sure how to do an if with multiple tests in shell, I'm having trouble writing this script:

echo "You have provided the following arguments $arg1 $arg2 $arg3"
if [ "$arg1" = "$arg2" && "$arg1" != "$arg3" ]
then 
    echo "Two of the provided args are equal."
    exit 3
elif [ $arg1 = $arg2 && $arg1 = $arg3 ]
then
  echo "All of the specified args are equal"
  exit 0
else
  echo "All of the specified args are different"
  exit 4 
fi

问题是我每次收到此错误:./compare.sh:[:缺少`]命令未找到
谢谢!

the problem is I get this error every time: ./compare.sh: [: missing `]' command not found Thank you!

推荐答案

SH 是间preting的&放大器;&安培; 作为外壳运营商。将其更改为 -a ,这是的[配合运营商:

sh is interpreting the && as a shell operator. Change it to -a, that’s [’s conjunction operator:

[ "$arg1" = "$arg2" -a "$arg1" != "$arg3" ]

此外,你应该总是引用变量,因为 [当你离开过争论变得迷茫。

Also, you should always quote the variables, because [ gets confused when you leave off arguments.

这篇关于如果使用shell脚本ELIF网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆