The if Command Looks for What Exit Code to Consider a Condition to Be True?
Agreement Exit Codes and Using them in Fustigate scripts
Lately I've been working on a lot of automation and monitoring projects, a big function of these projects are taking existing scripts and modifying them to be useful for automation and monitoring tools. I thing I accept noticed is sometimes scripts utilise leave codes and sometimes they don't. Information technology seems like exit codes are like shooting fish in a barrel for people to forget, just they are an incredibly important part of any script. Peculiarly if that script is used for the command line.
What are exit codes?
On Unix and Linux systems, programs can pass a value to their parent process while terminating. This value is referred to equally an get out lawmaking or exit status. On POSIX systems the standard convention is for the programme to pass 0 for successful executions and i or higher for failed executions.
Why is this important? If you look at exit codes in the context of scripts written to be used for the command line the answer is very unproblematic. Any script that is useful in some fashion will inevitably exist either used in some other script, or wrapped with a fustigate one liner. This becomes especially true if the script is used with automation tools like SaltStack or monitoring tools like Nagios, these programs will execute scripts and cheque the status lawmaking to determine whether that script was successful or not.
On top of those reasons, go out codes exist inside your scripts fifty-fifty if yous don't ascertain them. By not defining proper go out codes yous could be falsely reporting successful executions which can cause bug depending on what the script does.
What happens if I don't specify an go out code
In Linux whatever script run from the control line has an go out code. With Fustigate scripts, if the exit lawmaking is non specified in the script itself the leave code used will exist the get out code of the terminal command run. To assist explain exit codes a little improve we are going to use a quick sample script.
Sample Script:
#!/bin/bash
touch /root/test
echo created file The above sample script will execute both the touch control and the echo control. When we execute this script (every bit a non-root user) the touch command will fail, ideally since the affect command failed nosotros would desire the get out code of the script to point failure with an appropriate leave code. To check the exit lawmaking nosotros tin simply print the $? special variable in bash. This variable will impress the exit code of the final run command.
Execution:
$ ./tmp.sh
touch: cannot bear upon '/root/test': Permission denied
created file
$ echo $?
0 As you tin come across after running the ./tmp.sh command the exit lawmaking was 0 which indicates success, even though the touch command failed. The sample script runs two commands affect and repeat, since we did non specify an leave code the script exits with the leave code of the final run command. In this case, the last run command is the echo command, which did execute successfully.
Script:
#!/bin/bash
touch /root/exam If we remove the echo control from the script we should meet the exit code of the touch control.
Execution:
$ ./tmp.sh
touch: cannot affect '/root/test': Permission denied
$ repeat $?
1 Every bit you can see, since the concluding command run was touch the exit lawmaking reflects the true status of the script; failed.
Using exit codes in your bash scripts
While removing the echo command from our sample script worked to provide an go out code, what happens when we want to perform 1 activeness if the affect was successful and some other if information technology was not. Deportment such as printing to stdout on success and stderr on failure.
Testing for exit codes
Earlier nosotros used the $? special variable to impress the exit lawmaking of the script. We tin likewise use this variable inside our script to test if the touch command was successful or non.
Script:
#!/bin/bash touch on /root/test 2> /dev/null if [ $? -eq 0 ]
then
echo "Successfully created file"
else
echo "Could not create file" >&ii
fi
In the in a higher place revision of our sample script; if the exit code for impact is 0 the script will echo a successful message. If the go out code is anything other than 0 this indicates failure and the script will echo a failure message to stderr.
Execution:
$ ./tmp.sh
Could not create file Providing your own get out code
While the above revision will provide an error message if the touch command fails, it withal provides a 0 exit lawmaking indicating success.
$ ./tmp.sh
Could non create file
$ echo $?
0 Since the script failed, it would non exist a good idea to pass a successful exit code to whatever other program executing this script. To add our own get out code to this script, we tin can simply use the exit command.
Script:
#!/bin/bash touch /root/test 2> /dev/null if [ $? -eq 0 ]
then
repeat "Successfully created file"
exit 0
else
echo "Could non create file" >&ii
get out 1
fi
With the exit control in this script, we will exit with a successful message and 0 get out code if the touch command is successful. If the touch control fails still, we will print a failure bulletin to stderr and exit with a 1 value which indicates failure.
Execution:
$ ./tmp.sh
Could not create file
$ repeat $?
1 Using exit codes on the command line
At present that our script is able to tell both users and programs whether it finished successfully or unsuccessfully we tin can use this script with other administration tools or just use it with bash one liners.
Bash One Liner:
$ ./tmp.sh && echo "bam" || (sudo ./tmp.sh && echo "bam" || echo "fail")
Could not create file
Successfully created file
bam The to a higher place grouping of commands employ what is called list constructs in fustigate. List constructs allow you to concatenation commands together with simple && for and and || for or atmospheric condition. The above command volition execute the ./tmp.sh script, and if the exit lawmaking is 0 the control repeat "bam" volition be executed. If the exit code of ./tmp.sh is 1 still, the commands inside the parenthesis volition exist executed next. Within the parenthesis the commands are chained together using the && and || constructs again.
The listing constructs use exit codes to sympathize whether a command has successfully executed or not. If scripts exercise non properly use exit codes, whatsoever user of those scripts who utilise more advanced commands such every bit list constructs will get unexpected results on failures.
More exit codes
The exit control in bash accepts integers from 0 - 255, in most cases 0 and one will suffice however there are other reserved get out codes that can be used for more specific errors. The Linux Documentation Project has a pretty practiced tabular array of reserved go out codes and what they are used for.
Source: https://madflojo.medium.com/understanding-exit-codes-in-bash-6942a8b96ce5
0 Response to "The if Command Looks for What Exit Code to Consider a Condition to Be True?"
Post a Comment