Bash shell脚本未通过TRUE条件

wi3ka0sx  于 12个月前  发布在  Shell
关注(0)|答案(2)|浏览(128)

而条件没有传入TRUE结果。语法有什么问题吗?

RESULT="Exited (0) Less than a second ago"
if [ "$RESULT" = "Exited (0) Less than a second ago" || "$RESULT" = "Stop" ]
then
    echo 'exited'
elif [ "$RESULT" = *"Up"* ]
then
    echo 'stopped and exited'
else
    echo 'Docker already removed'
fi
ohfgkhjo

ohfgkhjo1#

以下脚本根据提供的条件正常工作。

#!/bin/sh

RESULT="Exited (0) Less than a second ago"

if [ "$RESULT" = "Exited (0) Less than a second ago" ] || [ "$RESULT" = "Stop" ]; then
    echo 'exited'
elif [ "$RESULT" = "Up" ]; then
    echo 'stopped and exited'
else
    echo 'Docker already removed'
fi

我将参数“-o”更改为“[ ]||在tjm3772通知“-o”选项在POSIX 2017中已过时后,

v6ylcynt

v6ylcynt2#

由于接受的答案是使用POSIX sh,因此另一种选择是使用case语句。

#!/bin/sh

RESULT="Exited (0) Less than a second ago"

case "$RESULT" in
   "Exited (0) Less than a second ago"|"STOP")
      echo exited;;
   "Up") 
      echo 'stopped and exited';;
    *) 
     echo 'Docker already removed';;
esac
  • 参见格条件构造
  • 参见help case

相关问题