BASH: How tell if script uses exit? - bash

Say I have two scripts that just print back the return code from a useless subscript:
script1
(echo; exit 0)
echo $?
script2
(echo)
echo $?
Both give back 0. But is there a way to tell that the first subscript explicitly uses the exit command?

After some research I got some breakthrough. Namely you can setup an exit_handler that can tell if there was an exit call by simply examining the last command.
#! /bin/bash
exit_handler () {
ret=$?
if echo "$BASH_COMMAND" | grep -e "^exit " >> /dev/null
then
echo "it was an explicit exit"
else
echo "it was an implicit exit"
fi
exit $ret
}
trap "exit_handler" EXIT
exit 22
This will print
it was an explicit exit
Now in order to tell the parent, instead of echoing, we can rather write to a file, a named pipe or whatever.
As per noting of choroba, exit without an argument will give implicit call, which is admittedly wrong since exit (without argument) is the same as exit $?. For that reason the regex has to take that into consideration:
#! /bin/bash
exit_handler () {
ret=$?
if echo "$BASH_COMMAND" | grep -e "^exit \|^exit$" >> /dev/null
then
echo "it was an explicit exit"
else
echo "it was an implicit exit"
fi
exit $ret
}
trap "exit_handler" EXIT
exit 22

Related

Case statement not working in bash, conditions not apply

Case statement not working. Pressing Enter(empty string) not make script exit, other cases not working too. No one exit 1 commands run when it should, all cases fails when I type text specially for it.
I find out what case works, but exit 1 statement in it not exits the script. How to exit script in that place correctly?
#!/bin/bash
...
get_virtual_host() {
if [ -t 0 ]; then
read -p "Create virtualhost (= Folder name,case sensitive)" -r host
else
# same as 'read' but for GUI
host=$(zenity --forms --add-entry=Name --text='Create virtualhost (= Folder name,case sensitive)')
fi
case "$host" in
"") notify_user "Bad input: empty" ; exit 1 ;;
*"*"*) notify_user "Bad input: wildcard" ; exit 1 ;;
*[[:space:]]*) notify_user "Bad input: whitespace" ; exit 1 ;;
esac
echo "$host"
}
host=$(get_virtual_host)
Addition to clarify:
notify_user () {
echo "$1" >&2
[ -t 0 ] || if type -p notify-send >/dev/null; then notify-send "$1"; else xmessage -buttons Ok:0 -nearmouse "$1" -timeout 10; fi
}
The function is in fact written correctly. It's how it's called that's the problem.
host=$(get_virtual_host)
When you capture a command's output the command runs in a subshell. Exiting the subshell doesn't directly cause the parent shell to exit; the parent shell needs to check the subshell's exit status.
host=$(get_virtual_host) || exit
This will exit the parent if get_virtual_host fails. A bare exit without an explicit exit code forwards the existing value of $?.

Exit script function in bash

I have below script which I will be running via jenkins:
#!/bin/bash
function getToken
{
echo "function to get token"
}
function call_init
{
echo "Creating a config file"
}
function call_list
{
echo "calling list*"
}
#Starting execution
if [[ -z "$TOKEN" ]]; then
TOKEN=$(getToken)
if [ $? -ne 0 ]; then
exit 1
fi
fi
echo "Creating a config file and populating it"
call_init
if [ $? -ne 0 ]; then
exit 1
fi
if [ -n $ACTION ]; then
case "$ACTION" in
'list') echo "Action is list"
call_list
if [ $? -ne 0 ]; then
exit 1
fi
;;
'update') echo "Section is update"
;;
'delete') echo "Section is delete"
;;
*) echo "This is a default message"
;;
esac
fi
As you see that theres a lot of repetition of the below code which helps me fail the jenkins job by throwing the error code 1:
if [ $? -ne 0 ]; then
exit 1
fi
What would be the most efficient way to handle this? I need it to always exit the code with 1.
P.S: I went through Checking Bash exit status of several commands efficiently, however was not able to get it work for the above script.
The best approach is to use explicit error checking.
Your current pattern can be streamlined, the following are all equivelant:
run_command
if [ $? -ne 0 ]; then
print_error
exit 1
fi
if ! run_command; then
print_error
exit 1
fi
run_command || { print_error; exit 1; }
Or in its simplest form, with no error message:
run_command || exit 1
As an alternative, you might want to use set -e.
You might also be interested in set -o pipefail.
These are not the preferred solution, as #William has pointed out, but can be useful for getting simple scripts to throw errors:
Note that set -e is generally not considered best practice. It's semantics are extremely unexpected in the edge cases (eg, if you invoke set -e in a function), and more importantly have changed dramatically with different versions of the shell. It is far better to explicitly invoke exit by running cmd || exit
You can use set -e to cause bash to bail out if a command returns non-zero, and set +e to disable this behaviour.
set: set [-abefhkmnptuvxBCHP] [-o option-name] [--] [arg ...]
Set or unset values of shell options and positional parameters.
Change the value of shell attributes and positional parameters, or
display the names and values of shell variables.
Options:
[...]
-e Exit immediately if a command exits with a non-zero status.
[...]
-o option-name
Set the variable corresponding to option-name:
[...]
pipefail the return value of a pipeline is the status of
the last command to exit with a non-zero status,
or zero if no command exited with a non-zero status
[...]
To make use of this, you must enable the option before it would be required.
For example:
# disable 'exit immediately' (the default)
set +e
echo "running false..."
false
echo "we're still running"
# enable 'exit immediately'
set -e
echo "running false..."
false
echo "this should never get printed"
set -o pipefail must be used in conjunction with set -e:
# enable 'exit immediately'
set -e
# disable 'pipefail' (the default)
set +o pipefail
echo "running false | true..."
false | true
echo "we're still running (only the last exit status is considered)"
# enable 'pipefail'
set -o pipefail
echo "running false | true..."
false | true
echo "this should never get printed"

How to execute a bash script line by line? [duplicate]

This question already has answers here:
Automatic exit from Bash shell script on error [duplicate]
(8 answers)
Closed 6 years ago.
#Example Script
wget http://file1.com
cd /dir
wget http://file2.com
wget http://file3.com
I want to execute the bash script line by line and test the exit code ($?) of each execution and determine whether to proceed or not:
It basically means I need to add the following script below every line in the original script:
if test $? -eq 0
then
echo "No error"
else
echo "ERROR"
exit
fi
and the original script becomes:
#Example Script
wget http://file1.com
if test $? -eq 0
then
echo "No error"
else
echo "ERROR"
exit
fi
cd /dir
if test $? -eq 0
then
echo "No error"
else
echo "ERROR"
exit
fi
wget http://file2.com
if test $? -eq 0
then
echo "No error"
else
echo "ERROR"
exit
fi
wget http://file3.com
if test $? -eq 0
then
echo "No error"
else
echo "ERROR"
exit
fi
But the script becomes bloated.
Is there a better method?
One can use set -e but it's not without it's own pitfalls. Alternative one can bail out on errors:
command || exit 1
And an your if-statement can be written less verbose:
if command; then
The above is the same as:
command
if test "$?" -eq 0; then
set -e makes the script fail on non-zero exit status of any command. set +e removes the setting.
There are many ways to do that.
For example can use set in order to automatically stop on "bad" rc; simply by putting
set -e
on top of your script. Alternatively, you could write a "check_rc" function; see here for some starting points.
Or, you start with this:
check_error () {
if [ $RET == 0 ]; then
echo "DONE"
echo ""
else
echo "ERROR"
exit 1
fi
}
To be used with:
echo "some example command"
RET=$? ; check_error
As said; many ways to do this.
Best bet is to use set -e to terminate the script as soon as any non-zero return code is observed. Alternatively you can write a function to deal with error traps and call it after every command, this will reduce the if...else part and you can print any message before exiting.
trap errorsRead ERR;
function errorsRead() {
echo "Some none-zero return code observed..";
exit 1;
}
somecommand #command of your need
errorsRead # calling trap handling function
You can do this contraption:
wget http://file1.com || exit 1
This will terminate the script with error code 1 if a command returns a non-zero (failed) result.

Check the last exit code of a command echoed by a bash script

#!/bin/bash
test(){
return 1;
}
VAR=$(expect -c 'puts "Exiting"; exit 1;');
echo "$VAR";
RETURN_CODE=$?;
echo $RETURN_CODE;
test
RETURN_CODE=$?;
echo $RETURN_CODE;
The output of this script will be:
Exiting
0
1
My guess is that the first 0 is the return code of "echo". Am I right? If so then how do I capture the return code of expect?
Exit/Return code needs be extracted immediately after running any command.
So use:
VAR=$(expect -c 'puts "Exiting"; exit 1;')
RETURN_CODE=$?
echo "$VAR"
echo $RETURN_CODE
Since your code is doing echo right after expect call therefore $? is giving you exit status of echo rather than the expect command.

Exit shell from a subshell

I believe I am calling exit in a subshell that causes my program to continue:
#!/bin/bash
grep str file | while read line
do
exit 0
done
echo "String that should not really show up!"
Any idea how I can get out of the main program?
You can trivially restructure to avoid the subshell -- or, rather, to run the grep inside the subshell rather than the while read loop.
#!/bin/bash
while read line; do
exit 1
done < <(grep str file)
Note that <() is bash-only syntax, and does not work with /bin/sh.
In general, you can check the return code of the spawned subshell to see whether the main main should continue or not.
For instance:
#!/bin/bash
grep str file | while read line
do
exit 1
done
if [[ $? == 1 ]]; then
exit 1
fi
echo "String that should not really show up!"
Will not print the message because the subshell exited with code 1.
You can "exit" your shell by sending a signal to it form your subshell:replace exit 0 with kill -1 $PPID
But i don't recommend this approach.I suggest your subshell to return a special meaning value,like exit 1
#!/bin/bash
grep str file | while read line
do
exit 1
done
exit 0
then your can check your subshell's return value by $?
like subshell.sh ;if [[ $? == 1 ]]; then exit 1 ;fi
or simply subshell.sh || exit

Resources