Bash - if any process is killed, exit without passing to the next steps [duplicate] - bash

This question already has answers here:
How to exit if a command failed? [duplicate]
(9 answers)
Closed 6 years ago.
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,

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.

You can try it so:
./process1 && ./process2 && ./process3 && ./process4 && ./process5

Related

`shopt -s inherit_errexit` has no effect in declare command [duplicate]

This question already has answers here:
Exit code of variable assignment to command substitution in Bash
(5 answers)
Closed 1 year ago.
#!/bin/bash
set -e
set -o pipefail
shopt -s inherit_errexit
declare _x=`command-with-error`
echo "_x=${_x}"
Run the script shows:
bash xx.sh
xx.sh: line 6: command-with-error: command not found
_x=
Apparently line 6 did not exit the shell. What option should I use to make the script exit when the subshell command on declare line fails?
The successful exit status of declare is overriding the unsuccessful exit status of command-with-error.
Break it into two separate commands:
declare _x
_x=$(command-with-error)
...as you can see running correctly (which is to say, without writing anything to stdout) at https://ideone.com/TGyFCZ

Can a bash script distinguish between being called as a script and being run as a "source"? [duplicate]

This question already has answers here:
How to detect if a script is being sourced
(22 answers)
Closed 3 years ago.
I have a bash script that has inside it:
exit 1
When I "source" this script instead of running it, it causes the caller to exit.
Is there a way that the script can determine that it's being run with "source" and not as its script?
You can use this check inside your script:
[[ $0 = $BASH_SOURCE ]] && echo "normal run" || echo "sourced run"
Or using if/else/fi wherever you're calling exit:
if [[ $0 = $BASH_SOURCE ]]; then
exit 1
else
# don't call exit
echo "some error..."
fi

Automatically exit when bash command produce return code non zero [duplicate]

This question already has answers here:
Automatic exit from Bash shell script on error [duplicate]
(8 answers)
Error handling in Bash [closed]
(15 answers)
Closed 4 years ago.
In bash how do we make the script to automatically exit if a command line return code is not zero. For example:
#!/bin/bash
cd /something_something
mv file_a /somedir/file_a # this produce an error
echo $? # This produce a non-zero output
echo "We should not continue to this line"
I know we can debug bash script with #!/bin/bash -x but sometime the script is too long, it run so fast, and we missed important error.
And I don't want to keep writing
[[ $? -ne 0 ]] && run next_command
There are lots of problems with using set -e. Just join the commands with &&, and test the result with an if statement.
if cd /something_something && mv file_a /somedir/file_a; then
echo $?
exit
fi
echo "Both cd and mv worked"

Check that process is running [duplicate]

This question already has answers here:
How to check if a process id (PID) exists
(11 answers)
Closed 6 years ago.
I have the following bash code which checks that a process is running:
is_running() {
ps `cat "$pid_file"` > /dev/null 2>&1
}
The problem is that is_running is always evaluated to true.
$pid_file contains a process ID that isn't listed when I run ps.
I would like in this case, is_running to return false.
How can I modify it for that purpose?
You are missing the -p option:
ps -p PID
In bash, i would simply do:
is_running() { ps -p $(<"$1") &>/dev/null ;}
and will give the filename as an argument to the function:
is_running /pid/file
You need to start using the newer command substitution syntax $() instead of the arcane `...`
bash supports a shorthand (&>) for indicating the redirection of STDOUT and STDERR

What does `echo $?` mean in bash? [duplicate]

This question already has answers here:
Meaning of $? (dollar question mark) in shell scripts
(8 answers)
Closed 9 years ago.
I came upon the following command:
echo $?
what does that command do?
Echoes (prints) the exit value for the previous command.
If it failed it will be different than zero (0).
$ cd ~
$ echo $?
> 0
$ cd //whatever/
> bash: cd: //whatever/: No such file or directory
$ echo $?
> 1
Programs exit with a status code. Every program is unique and has a different set of failure codes, but it's universally acknowledged that 0 is the 'success' code.

Resources