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.7k times
1
peterh
- 9,488
- 16
- 59
- 88
user1991142
- 11
- 1
- 1
- 2
-
1Does the command also set a reasonable exit status, or does it lie to the shell and say it succeeded when completing unsuccessfully? (Check `$?`) If the command isn't a liar, you can just use `if
; then – Fox Jan 29 '18 at 03:53; else ; fi` -
1It's not really a duplicate of "assign command outputs to a var". OP may need to WATCH the output WHILE the command is running. – Bach Lien Jan 29 '18 at 07:54
1 Answers
2
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
Bach Lien
- 219
- 1
- 3