How to read the output of previous command and then proceed to the next command

156 Views Asked by At

I wanted to write a script which will execute the next command only if the previous command was successful.

As part of the script I am trying to add files. Once the command to add the file is successfully executed, I get a message like as follows:

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

Once I receive the "Successfully pushed policy to server" message, I wanted the script to push the next file. (so on for 50 different files)

Can somebody let me know how to implement it?

1

There are 1 best solutions below

6
UtLox On BEST ANSWER

try this:

#!/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"