如何读取上一条命令的输出,然后继续执行下一条命令

时间:2019-05-10 06:01:10

标签: linux shell scripting tivoli

我想编写一个仅在上一个命令成功的情况下才执行下一个命令的脚本。

作为脚本的一部分,我试图添加文件。成功执行添加文件的命令后,我将收到如下消息:

May 10, 2019 5:57:12 AM com.ibm.ws.jmx.connector.client.rest.internal.RESTMBeanServerConnection findInitialEndpoint
INFO: CWWKX0230I: The collective member opened JMX client to the collective controller: ncr-aus-e171.corp.wayport.net:9081
May 10, 2019 5:57:12 AM com.ibm.ws.jmx.connector.client.rest.internal.RESTMBeanServerConnection findInitialEndpoint
INFO: CWWKX0230I: The collective member opened JMX client to the collective controller: ncr-aus-e171.corp.wayport.net:9081
Successfully pushed policy to server

一旦收到“已成功将策略推送到服务器”消息,我希望脚本推送下一个文件。 (对于50个不同的文件,依此类推)

有人可以让我知道如何实现它吗?

1 个答案:

答案 0 :(得分:0)

尝试一下:

#!/bin/bash
Commands=(
  "command1 parameter1 parameter2"
  "command2 parameter1"
  "command3 parameter1 parameter2 parameter3"
)

tmplog=$(mktemp)
for cmd in "${Commands[@]}"; do
   echo "$cmd"
   $cmd >"$tmplog"
   tail -1 "$tmplog" | grep -v "^Successfully" && break
done
rm "$tmplog"
相关问题