I executed a command in a shell script and the command ends with "completed successfully" or "completed unsuccessfully". How can I read that in the script and use it for if and else conditions?
1 Answer
Try this:
your_command | \
tee >(sleep; [[ `tail -n1` =~ 'completed successfully' ]] && echo OK || echo NOTOK)
Explanation:
tee: splityour_commandoutputs into two (i)>(...)and (ii)stdoutsleep: (optionally) wait for 1 second, change1sto what you needtail -n1: extract last line=~: matching test; change the test to what you needecho OK,echo NOTOK: just examples, change to what you need
$?) If the command isn't a liar, you can just useif <command>; then <stuff>; else <other stuff>; fi