mac OS X上的上一个命令输出

时间:2018-04-27 00:51:15

标签: bash shell

我有下面的脚本。当我在MacOs上运行iTerm中的每一行时,每个命令都有效。但是,如果我将其保存为shell脚本,它会说" !!命令未找到"。我试过#!/ bin / bash。但它仍然没有用。

#!/bin/sh
./ongoingShellScript.sh 
if !! | grep "errors: 0"
then
  echo Success
else
  echo Failure
fi

我本可以做到

if ./ongoingShellScript.sh | grep "errors: 0"

但在这种情况下,proceedShellScript的输出不会实时打印。

我在这做什么? 提前谢谢

GV

1 个答案:

答案 0 :(得分:2)

!!没有引用先前的输出 - 它再次运行整个命令,因此生成 new 输出集。此外,它来自的功能集 - 称为"历史扩展" - 是在脚本执行期间默认关闭的交互式扩展。

如果您想在测试字符串的stdout时为用户打印状态,那么该作业的简单工具是grep

if ./ongoingShellScript.sh | tee /dev/stderr | grep -q "errors: 0"; then
  echo "Success" >&2
else
  echo "Failure" >&2
fi

...假设errors: 0出现在输出的末尾,因此只要tee看到此字符串,grep就可以退出了{{1}}。

相关问题