1

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?

peterh
  • 9,731
  • 1
    Does 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 <command>; then <stuff>; else <other stuff>; fi – Fox Jan 29 '18 at 03:53
  • 1
    It'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 Answers1

2

Try this:

your_command | \
tee >(sleep; [[ `tail -n1` =~ 'completed successfully' ]] && echo OK || echo NOTOK)

Explanation:

  1. tee: split your_command outputs into two (i) >(...) and (ii) stdout
  2. sleep: (optionally) wait for 1 second, change 1s to what you need
  3. tail -n1: extract last line
  4. =~: matching test; change the test to what you need
  5. echo OK, echo NOTOK: just examples, change to what you need
Bach Lien
  • 229