【问题标题】:Using if elif fi in shell scripts [duplicate]在 shell 脚本中使用 if elif fi [重复]
【发布时间】:2011-01-22 11:44:45
【问题描述】:

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

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: [: missing `]' command not found

【问题讨论】:

  • 不少评论员建议您使用 [[ 而不是 [ 但这会使您的脚本特定于 bash。如果您能坚持使用简单的旧 Bourne shell (sh) 语法,您将遇到更少的维护和可移植性问题。

标签: bash shell


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    sh 将 && 解释为 shell 运算符。改成-a,也就是[的连词:

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

    此外,您应该始终引用变量,因为 [ 在您省略参数时会感到困惑。

    【讨论】:

      【解决方案3】:

      使用双括号...

      if [[ expression ]]

      【讨论】:

      • 请注意,这是一个解决方案,因为 [[ 构造内置于 shell 中,而 [ 是 test 命令的另一个名称,因此受制于其语法 -- 请参阅 @ 987654325@
      • 从技术上讲,[ 是一个内置的 shell,但 [[ 是一个 shell 关键字。这就是区别。
      • 因此请查看 bash 命令 type [、type [[、help [ 和 help [[ 的结果。
      • @JoshLee 不,有一个文件叫/usr/bin/[
      【解决方案4】:

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

      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
      

      这应该可以。 :)

      【讨论】:

      • 天哪,这对我来说看起来很古怪。为什么在else 之后没有then?它实际上可能并不古怪,但与 C 相比,它看起来就是这样。
      • $option 之后和 = 之前需要空格才能正常运行,否则每次 1
      【解决方案5】:

      将[ 更改为[[,将] 更改为]]。

      【讨论】:

      • 更好的是,将 '[' 更改为 'test'
      【解决方案6】:

      这对我有用,

       # 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
      

      您可以在脚本中添加set -x 来排查错误。

      【讨论】:

        猜你喜欢
        • 2012-09-22
        • 1970-01-01
        • 1970-01-01
        • 2019-07-10
        • 2015-05-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多