0

I have such a bash file;

./process1
./process2
./process3
./process4
./process5

Let's say I run this bash script, and process2 is killed for some reason. Without passing to process3, I directly want to exit. How can I manage this?

Thanks,

yusuf
  • 3,591
  • 8
  • 45
  • 86

2 Answers2

3

Just exit if non-zero exit code:

./process1 || exit

and so on ...

Another way in bash, use -e flag:

#!/bin/bash
set -e

-e Exit immediately if a command exits with a non-zero status.

Juan Diego Godoy Robles
  • 14,447
  • 2
  • 38
  • 52
  • Downvote: Explicitly examining `$?` is a beginner antipattern. The shell's flow control constructs all examine this variable behind the scene; you should almost never need to look at it directly. The proper way to do what you describe is simply `./process1 || exit` (which has the added benefit that the exit code from `process1` is propagated back to the caller). – tripleee Nov 11 '16 at 09:54
  • Good point @tripleee . Thanks – Juan Diego Godoy Robles Nov 11 '16 at 10:00
  • That was quick. Downvote redacted. – tripleee Nov 11 '16 at 10:02
1

You can try it so:

./process1 && ./process2 && ./process3 && ./process4 && ./process5
quantummind
  • 2,086
  • 1
  • 14
  • 20