After a script or a command ended, it's useful to leave a trace in the system of its execution, in order to allow the following scripts to know if it was successful and to behave consequently. For this purpose this command exists:
exit return_value
It ends the execution of a script and makes
return_value available in the operating system environment.
The following script shows an interesting usage of the return values and command line parameters:
#!/bin/bash
# The following script searches for the user "pippo" in the system
# user list through the grep command. According to the return
# value of grep, it visualizes a message telling if the user
# was found or not.
# It searches for the user "pippo" in the list of system users
grep "admin" /etc/passwd
# According to the return value, it prints a message.
# Remember that $? returns the return value of the last
# executed command.
if [ "$?" -eq 0 ];
then
echo "User found"
exit 0
else
echo "User not found"
exit 1
fi