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?
Asked
Active
Viewed 2.8k times
1

peterh
- 9,731

user1991142
- 11
1 Answers
2
Try this:
your_command | \
tee >(sleep; [[ `tail -n1` =~ 'completed successfully' ]] && echo OK || echo NOTOK)
Explanation:
tee
: splityour_command
outputs into two (i)>(...)
and (ii)stdout
sleep
: (optionally) wait for 1 second, change1s
to 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

Bach Lien
- 229
$?
) If the command isn't a liar, you can just useif <command>; then <stuff>; else <other stuff>; fi
– Fox Jan 29 '18 at 03:53