Should Bash exit 1 show anything on the terminal? - bash

If another script or process, such as the sequential tasks in a Gitlab .gitlab-ci.yml file for instance, depend on a Bash script exiting without an error to continue, or fail to exit further execution; should the Bash script do any thing else than exit 0 for success or exit 1 for catch all errors?
Example code below tests if a site was successfully deployed.
#!/bin/bash
flag="false"
for i in {1..10}
do
response=$(curl -Is http://mysite/ | head -n 1 | tr -d '\r')
echo "$response"
if [ "$response" = "HTTP/1.1 200 OK" ]; then
echo "SITE UP"
flag="true"
exit 0
fi
sleep 1s
done
if [ "$flag" = "false" ]; then
echo "SITE DOWN"
exit 1
fi
Everything works 100% as expected, and the script is executed as ./test-up.sh > /dev/null to suppress the debugging echos used (as well as text returned by the curl command).
However, if the script fails nothing is printed on the command line with regard to errors. The command line stays blank. The same happens if exit 0 was encountered.
Is the above intended?

Echo to stderr
echo "SITE DOWN" >&2

Related

How the value is assigned to the variable with shell script

I have this code but I don't understand it very well.
Can you explain to me how the finish variable gets the value.
what does this assignment "finish=$?" mean?
or if they have any documentation to help me understand.
Thanks in advance.
#!/usr/bin/bash
/usr/bin/java -jar Report.jar $user $pass $base $dateini $datefin $iphost >> file.log 2>&1
finish=$?
if [ $finish -eq 0 ]; then
echo "Report executed successfully" >> file.log
exit 0
else
echo "There was an error in the report" >> file.log
exit 1
fi
The exit status of the last command you ran (/usr/bin/java -jar ...) is stored in the special parameter $?.
Commands executed by the shell script or user have a exit status being either 0 (meaning the command was successful without errors), or a non-zero (1-255) value indicating the command was a failure.
Check here for further information

false | true; echo $? [duplicate]

I currently have a script that does something like
./a | ./b | ./c
I want to modify it so that if any of a, b, or c exit with an error code I print an error message and stop instead of piping bad output forward.
What would be the simplest/cleanest way to do so?
In bash you can use set -e and set -o pipefail at the beginning of your file. A subsequent command ./a | ./b | ./c will fail when any of the three scripts fails. The return code will be the return code of the first failed script.
Note that pipefail isn't available in standard sh.
You can also check the ${PIPESTATUS[]} array after the full execution, e.g. if you run:
./a | ./b | ./c
Then ${PIPESTATUS} will be an array of error codes from each command in the pipe, so if the middle command failed, echo ${PIPESTATUS[#]} would contain something like:
0 1 0
and something like this run after the command:
test ${PIPESTATUS[0]} -eq 0 -a ${PIPESTATUS[1]} -eq 0 -a ${PIPESTATUS[2]} -eq 0
will allow you to check that all commands in the pipe succeeded.
If you really don't want the second command to proceed until the first is known to be successful, then you probably need to use temporary files. The simple version of that is:
tmp=${TMPDIR:-/tmp}/mine.$$
if ./a > $tmp.1
then
if ./b <$tmp.1 >$tmp.2
then
if ./c <$tmp.2
then : OK
else echo "./c failed" 1>&2
fi
else echo "./b failed" 1>&2
fi
else echo "./a failed" 1>&2
fi
rm -f $tmp.[12]
The '1>&2' redirection can also be abbreviated '>&2'; however, an old version of the MKS shell mishandled the error redirection without the preceding '1' so I've used that unambiguous notation for reliability for ages.
This leaks files if you interrupt something. Bomb-proof (more or less) shell programming uses:
tmp=${TMPDIR:-/tmp}/mine.$$
trap 'rm -f $tmp.[12]; exit 1' 0 1 2 3 13 15
...if statement as before...
rm -f $tmp.[12]
trap 0 1 2 3 13 15
The first trap line says 'run the commands 'rm -f $tmp.[12]; exit 1' when any of the signals 1 SIGHUP, 2 SIGINT, 3 SIGQUIT, 13 SIGPIPE, or 15 SIGTERM occur, or 0 (when the shell exits for any reason).
If you're writing a shell script, the final trap only needs to remove the trap on 0, which is the shell exit trap (you can leave the other signals in place since the process is about to terminate anyway).
In the original pipeline, it is feasible for 'c' to be reading data from 'b' before 'a' has finished - this is usually desirable (it gives multiple cores work to do, for example). If 'b' is a 'sort' phase, then this won't apply - 'b' has to see all its input before it can generate any of its output.
If you want to detect which command(s) fail, you can use:
(./a || echo "./a exited with $?" 1>&2) |
(./b || echo "./b exited with $?" 1>&2) |
(./c || echo "./c exited with $?" 1>&2)
This is simple and symmetric - it is trivial to extend to a 4-part or N-part pipeline.
Simple experimentation with 'set -e' didn't help.
Unfortunately, the answer by Johnathan requires temporary files and the answers by Michel and Imron requires bash (even though this question is tagged shell). As pointed out by others already, it is not possible to abort the pipe before later processes are started. All processes are started at once and will thus all run before any errors can be communicated. But the title of the question was also asking about error codes. These can be retrieved and investigated after the pipe finished to figure out whether any of the involved processes failed.
Here is a solution that catches all errors in the pipe and not only errors of the last component. So this is like bash's pipefail, just more powerful in the sense that you can retrieve all the error codes.
res=$( (./a 2>&1 || echo "1st failed with $?" >&2) |
(./b 2>&1 || echo "2nd failed with $?" >&2) |
(./c 2>&1 || echo "3rd failed with $?" >&2) > /dev/null 2>&1)
if [ -n "$res" ]; then
echo pipe failed
fi
To detect whether anything failed, an echo command prints on standard error in case any command fails. Then the combined standard error output is saved in $res and investigated later. This is also why standard error of all processes is redirected to standard output. You can also send that output to /dev/null or leave it as yet another indicator that something went wrong. You can replace the last redirect to /dev/null with a file if yo uneed to store the output of the last command anywhere.
To play more with this construct and to convince yourself that this really does what it should, I replaced ./a, ./b and ./c by subshells which execute echo, cat and exit. You can use this to check that this construct really forwards all the output from one process to another and that the error codes get recorded correctly.
res=$( (sh -c "echo 1st out; exit 0" 2>&1 || echo "1st failed with $?" >&2) |
(sh -c "cat; echo 2nd out; exit 0" 2>&1 || echo "2nd failed with $?" >&2) |
(sh -c "echo start; cat; echo end; exit 0" 2>&1 || echo "3rd failed with $?" >&2) > /dev/null 2>&1)
if [ -n "$res" ]; then
echo pipe failed
fi
This answer is in the spirit of the accepted answer, but using shell variables instead of temporary files.
if TMP_A="$(./a)"
then
if TMP_B="$(echo "TMP_A" | ./b)"
then
if TMP_C="$(echo "TMP_B" | ./c)"
then
echo "$TMP_C"
else
echo "./c failed"
fi
else
echo "./b failed"
fi
else
echo "./a failed"
fi

How to write process ID and get exit code of the next to last command

I want to run a command, write the process id instantly to a file when the command started and afterwards get the exit status of the command. This means, while the process id has to be written instantly, I want the exit status only when the initial command has finished.
The following statement will unfortunately run the command, write the process id instantly but it won't wait for the command to be finished. Furthermore I will only get the exit status of the echo command, not of the initial command
command in my case is rdiff-backup.
How do I need to modify the statement?
<command> & echo $! > "/pid_file"
RESULT=$?
if [ "$RESULT" -ne "0" ]; then
echo "Finished with errors"
fi
You need to wait on the background process to get its exit status:
_command_for_background_ & echo $! > pid_file
: ... do other things, if any ...
#
# it is better to grab $? on the same line to prevent any
# future modifications inadvertently breaking the strict sequence
#
wait $(< pid_file); child_status=$?
if [[ $child_status != 0 ]]; then
echo "Finished with errors"
fi

posix shell: stdout to file, exitcode to a variable and last line of stderr to another variable

I implemented the following in POSIX shell (not bash):
fail.sh:
#!/bin/sh
echo something useful
echo warning 1 >&2
echo warning 2 >&2
echo an error message >&2
exit 100
The command prints something I want to use on stdout, some warnings on stderr and an error message on stderr as well before failing with exit code 100.
success.sh:
#!/bin/sh
echo something useful
echo warning 1 >&2
echo warning 2 >&2
exit 0
This command prints something to stdout and some warnings to stderr but finishes successfully with exit code 0.
test.sh:
#!/bin/sh -e
script=$1
rm -f success
msg=$({ $script > useful; touch success; } 2>&1 | tail -1;)
if [ -f success ]; then
echo success
else
echo failure
echo last error was: $msg
fi
In this script I want to run either of those two scripts and provide the following functionality:
the output of the scripts must be redirected to a file
the last line of stderr must be saved to a variable so that I can print that last line later in case the command didnt exit successfully
I want to detect whether or not the command exited successfully by checking its exit status
My script test.sh achieves all of that but it uses an external file. Since I use -e the touch will only be executed if $script executed successfully. Can I capture the exit code of $script without this technique?
The script must be written in POSIX shell and must use -e.
#!/bin/sh -e
script=$1
if msg=$($script 2>&1 >useful); then
echo success
else
echo failure
msg=$(echo "$msg" | tail -1)
echo last error was: $msg
fi

Problem with pidof in Bash script

I've written a script for me to start and stop my Perforce server. To shutdown the server I use the kill -SIGTERM command with the PID of the server daemon. It works as it should but there are some discrepancies in my script concerning the output behavior.
The script looks as follows:
#!/bin/sh -e
export P4JOURNAL=/var/log/perforce/journal
export P4LOG=/var/log/perforce/p4err
export P4ROOT=/var/local/perforce_depot
export P4PORT=1666
PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin"
. /lib/lsb/init-functions
p4start="p4d -d"
p4stop="p4 admin stop"
p4user=perforce
case "$1" in
start)
log_action_begin_msg "Starting Perforce Server"
daemon -u $p4user -- $p4start;
echo "\n"
;;
stop)
echo "BLABLA"
echo "$(pidof /usr/local/bin/p4d)"
#daemon -u $p4user -- $p4stop;
p4dPid="$(pidof /usr/local/bin/p4d)"
echo $p4dPid
if [ -z "$(pidof /usr/local/bin/p4d)" ]; then
echo "ERROR: No Perforce Server running!"
else
echo "SUCCESS: Found Perforce Server running!\n\t"
echo "Shutting down Perforce Server..."
kill -15 $p4dPid;
fi
echo "\n"
;;
restart)
stop
start
;;
*)
echo "Usage: /etc/init.d/perforce (start|stop|restart)"
exit 1
;;
esac
exit 0
When p4d is running the stop block works as intended, but when there is no p4d running the script with stop only outputs BLABLA and an empty new line because of the echo "$(pidof /usr/local/bin/p4d)". The error message stating that no server is running is never printed. What am I doing wrong here?
PS: The part if [ -z "$(pidof /usr/local/bin/p4d)" ]; then has been changed from if [ -z "$p4dPid" ]; then for debug reasons.
EDIT: I narrowed down the problem. If I don't use the p4dPid variable and comment out the lines p4dPid="$(pidof /usr/local/bin/p4d)" and echo $p4dPid the if block is processed and the error messages is printed. Still I don't unterstand what is causing this behavior.
EDIT 2: Problem solved!
The -e in #!/bin/sh -e was causing the shell to exit the script after any statement returning a non-zero return value.
When your service is not running, the command
echo "$(pidof /usr/local/bin/p4d)"
is processed as
echo ""
because pidof did not return any string. So the command outputs an empty line.
If you do not want this empty line, then just remove this statement, after all you print an error message when the process is not running.
Problem solved!
The -e in #!/bin/sh -e was causing the shell to exit after any statement returning a non-zero return value.

Resources