0

I know how to get exit code from last called program. But how can I check exit codes in the following scenario

prog1 &
prog2 &

while [ true ]; do # main loop
    while [ condition1 ]; do
        prog1 <input >output
    done

    while [ condition2 ]; do
        prog2 <input >output
    done
done

my goal is to break the main loop and clean up processes after program 1 or 2 exits (with code =0 or !=0).

For instance:

  1. Program 1 exits with code 0. Then I want to kill program2 and exit my script.

  2. Program 2 exits with code 333. Then I want to kill program1 and exit script with code 222.

shellter
  • 36,525
  • 7
  • 83
  • 90
  • 1
    maybe you're just giving examples, but I'm pretty sure that 255 is the highest value that can be returned via the `exit` cmd in shell scripts. Good luck. – shellter Jun 02 '16 at 04:18
  • 1
    Your code doesn't really makes sense. It shows you running `prog1` in the background, then upon some unspecified condition running it again in the foreground, forever. Surely this cannot be even a rough approximation of what you actually want to do. Code which doesn't do what you want is rarely a good explanation of what you *do* want. – tripleee Jun 02 '16 at 04:21
  • 2
    Hints: After running something in the background, you get its PID in `$!` so you can save that and later `wait` for it to obtain its exit status. With `kill -0` you can check whether a PID is still alive. – tripleee Jun 02 '16 at 04:22

0 Answers0