Bash - if…then语句-意外的令牌



我正在写一个脚本,想检查grep命令是否匹配。但每次我都收到意想不到的礼物。错误。你能解释一下问题出在哪里吗?我检查了几次语法

我使用的是Debian GNU/Linux version 8 (jessie)

#!/bin/bash
if monit status wildfly | grep -q "OK" ; then
echo found
else
echo not found
fi

该语法在使用ZSH的macOS中工作,猜测您在Linux中或使用任何其他shell。两个建议:

  • if使用防弹语法
if monit status wildfly | grep -q "OK"
then
echo found
else
echo not found
fi
  • 在脚本的顶部添加一个shebang来使用bash:
#!/bin/sh

最新更新