bash 在 shell 脚本中使用 if elif fi

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2359270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 18:59:05  来源:igfitidea点击:

Using if elif fi in shell scripts

bashshell

提问by Zenet

I'm not sure how to do an ifwith multiple tests in shell. I'm having trouble writing this script:

我不确定如何if在 shell 中进行多个测试。我在编写此脚本时遇到问题:

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

The problem is I get this error every time:

问题是我每次都会收到此错误:

./compare.sh: [: missing `]' command not found

./compare.sh: [: 未找到缺少的 `]' 命令

采纳答案by Josh Lee

shis interpreting the &&as a shell operator. Change it to -a, that's ['s conjunction operator:

sh将 解释&&为 shell 运算符。将其更改为-a,这是[的连接运算符:

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

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

此外,你应该总是引用变量,因为[当你离开参数时会感到困惑。

回答by Splitlocked

Josh Lee's answer works, but you can use the "&&" operator for better readability like this:

Josh Lee 的答案有效,但您可以使用“&&”运算符来提高可读性,如下所示:

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

回答by Henryk Konsek

Use double brackets...

使用双括号...

if [[ expression ]]

if [[ expression ]]

回答by Gaurav Kolarkar_InfoCepts

I have a sample from your code. Try this:

我有一个来自您的代码的示例。尝试这个:

echo "*Select Option:*"
echo "1 - script1"
echo "2 - script2"
echo "3 - script3 "
read option
echo "You have selected" $option"."
if [ $option="1" ]
then
    echo "1"
elif [ $option="2" ]
then
    echo "2"
    exit 0
elif [ $option="3" ]
then
    echo "3"
    exit 0
else
    echo "Please try again from given options only."
fi

This should work. :)

这应该有效。:)

回答by Ethan Post

Change "[" to "[[" and "]" to "]]".

将“[”更改为“[[”并将“]”更改为“]]”。

回答by Ranjithkumar T

This is working for me,

这对我有用,

 # cat checking.sh
 #!/bin/bash
 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

 # ./checking.sh
 You have provided the following arguments
 All of the specified args are equal

You can add "set -x" in script to troubleshoot the errors,

您可以在脚本中添加“set -x”来解决错误,

Thanks.

谢谢。