There are different techniques that we can use to handle errors in bash/shell scripting. Let us see each of these techniques one by one.
The easiest way to prevent errors is to first to run your command in the terminal. And, if the command in the terminal runs as expected, then you should add it to your script. Let’s look at the different ways you can debug and handle errors in Bash.
Using the Bash script’s exit status
Suppose you run the following commands in the terminal one by one:
pwd echo $?
Here $? is a special variable that you can use to check the exit status of the last executed command. In this case, echo $? will return the exit status of the pwd command.
Remember, the value of exit status can be zero ( 0 ) or greater than zero, where zero means success, and the value greater than zero means failure.
Now, let’s make a script and check the exit status of multiple commands.
#!/bin/bash whoami echo "exit status of whoami command: $?" fdw echo "exit status of fdw command: $?" pwd echo "exit status of pwd command: $?" mand echo "exit status of mand command: $?"
When you run this script, you will notice that only two commands return the exit status of zero. It means that these commands are executed successfully. While the other two commands, fdw and mand fail to execute successfully.
How to stop any script on the first Error
Instead of getting all the errors of your script, you might want to exit it on the first error.
Let’s see through a script how can we stop the script after it comes across the first error.
#!/bin/bash set -e whoami fdw pwd mand
Now, if you run this script, it will exit after as soon as it encounters the first error. To stop the script at the first error, we have to use a command set -e at the top of the script as you can see in this script.
This script contains two commands fdw and mand that do not exist in Linux. So, the first error will occur when it tries to execute fdw command.
Now, let’s see another script in which we use the command set -u instead of the set -e command at the top of the script.
#!/bin/bash set -u echo "value of variable is $var" pwd
Using set -u at the top of your script will exit your script if you try to use an uninitialized variable.
How to debug Errors in your Bash script
To debug any script, you can use the x option while running the script. Let’s make a script named script.sh and run it with this option.
#!/bin/bash uname whoami hostname pwd
Now, run this script with the following command:
bash -x script.sh
When you run any script with the x option, it shows each command of that script followed by the output of that command.
It shows that command followed by the error of that command if there is an error. And, then you can go ahead and correct that error.
How to handle error through the if-else statement
Besides using the e and u option at the top of the script, we can also handle the errors using if-else statements. Let us create a script that prints the contents of the file in case the file exists in the current directory.
#!/bin/bash filename='info.txt' for file in * do if test -f "$filename"; then cat info.txt exit fi done echo "Error - "$filename" does NOT Exist"
This script searches for a file named info.txt in the current directory. It prints the contents of this file through the cat command if the file exists; otherwise, it throws an error that the file does not exist.
How to handle errors using AND & OR
We can also use AND ( && ) and OR ( || ) to catch errors in a script or in the terminal. The syntax of using && is as follows:
First_command && Second_command
The second command will only work if the first command does not throw any error. If the first command throws an error, then the second command will not be executed.
Let us run commands using &&:
pwd && ls
In this case, both commands will be executed as the first command will not throw any error.
Similar to &&, the syntax of using || is as follows:
First_command || Second_command
In this case, either the first command will run or the second command will run. Both commands might run in this case. We will get the error only when both commands can’t run successfully.
So, in short, there are different ways through which we can catch and handle errors.