Oherror processing — is a very important part of any programming language. Bash has no better option than others programming languages, to process the script error. But it is important that the Bash script is unmistakable while executing the script from the terminal. The error processing function can be implemented for the Bash script in several ways. This article shows various error handling methods in the Bash script.
Example 1. Processing errors using a conditional operator
Create a Bash file with the following script that shows the use of a conditional operator to handle errors. First operator « if » used to verify the total number of command line arguments and display an error message if the value is less than 2. Then the values of the divisible and divider are taken from the arguments of the command line. If the divider value is 0, an error is generated, and the error message is printed in the error.txt file. The second « if » command is used to check whether the error.txt file is empty or not. An error message is printed if the error.txt file is not empty.
#!/bin / bash #Check the meaning of the arguments if [ $ # -lt 2 ]; then echo "There are no one or more arguments." exit fi #Reading the value of the divisible from the first command line argument dividend = $ 1 #Reading the value of the divider from the second command line argument divisor = $ 2 #Divide the divisible into the divider result = `echo "scale = 2; $ dividend / $ divisor" | bc 2 > error.txt` #Read the contents of the error file content = `cat error.txt` if [ -n "$ content" ]; then #Sownload error message if the error.txt file is not empty echo "An error has occurred, a multiple of zero." else #Success the result echo "$ dividend / $ divisor = $ result"
Conclusion:
The following output appears after the previous script is executed without any arguments:
andreyex@andreyex: - / Desktop / bash $ bash error1.bash One or more argument is missing. andreyex@andreyex: ~ / Desktop / bash $
The following output appears after the previous script is executed with one argument value:
andreyex@andreyex: - / Desktop / bash $ bash error1.bash 75 One or more argument is missing. andreyex@andreyex: ~ / Desktop / bash $
The following output appears after the previous script is executed with two valid argument values:
andreyex@andreyex: - / Desktop / bash $ bash error1.bash 75 8 75/8 = 9.37 andreyex@andreyex: - / Desktop / bash $
The next conclusion appears after the execution of the previous script with two values of arguments, where the second argument is 0. An error message is displayed:
andreyex@andreyex: ~ / Desktop / bash $ bash error1.bash 75 0 Divisible by zero error occurred. andreyex@andreyex: ~ / Desktop / bash $
Example 2: Processing errors using the output state code
Create a Bash file with the following script that shows the use of Bash error processing by the output status code. Any Bash command is accepted as the input value, and this command is executed later. If the output status code is not zero, an error message is printed. Otherwise, a message about successful execution is printed.
#!/bin / bash #Take the name of the Linux team echo -n "Enter command: " read cmd_name #Run the team $ cmd_name #Check if the team is valid, if [ $? -ne 0 ]; then echo "$ cmd_name is an unacceptable team." else echo "$ cmd_name is the correct command." fi fi
Conclusion:
The next output appears after the execution of the previous script with the valid command. Here « data » is accepted as a command in the input value, which is acceptable:
andreyex@andreyex: - / Desktop / bash $ bash error2.bash Enter a command: date Tue Dec 27 19:18:39 + 06 2022 date is a valid command. andreyex@andreyex: - / Desktop / bash $
The next output appears after the previous script is executed for an invalid command. Here « cmd » is perceived as an unacceptable command in the input value:
andreyex@andreyex: - / Desktop / bash $ bash error2.bash Enter a command: cmd error2.bash: line 7: cmd: command not found cmd is a invalid command. andreyex@andreyex: - / Desktop / bash $
Example 3: stop executing on the first error
Create a Bash file with the following script that shows the method of stopping execution when the first script error appears. The next script uses two invalid commands. Thus, two errors are issued. The script stops execution after the first invalid command is executed using the « set -e » command.
#!/bin / bash #Set the option to complete the script at first error set -e echo 'Current date and time: ' #Action team date echo 'Current work directory: ' #Incorrect team cwd echo 'user name: ' #Action team whoami echo 'List of files and folders: ' #Incorrect list list
Conclusion:
The next output appears after the previous script is executed. The script stops execution after the invalid command « cwd » is executed:
andreyex@andreyex: - / Desktop / bash $ bash error3.bash Current date and time: Tue Dec 27 19:19:38 + 06 2022 Current orking Directory: error3.bash: line 9: cwd: command not found andreyex@andreyex: - / Desktop / bash $
Example 4: Stop Run for Uninitialized Variable
Create a Bash file with the following script that shows the method for stopping script execution for an uninitiated variable. The values of the username and password are taken from the values of the command line arguments. If any of the values of these variables is not initialized, an error message is displayed. If both variables are initialized, the script checks if the username and password are valid or invalid.
#!/bin / bash #Set the script completion option for an uninitialized variable set -u #Set the value of the first command line argument to the user name username = $ 1 #Check the correct or invalid of the username and password password = $ 2 #Check the correct or invalid of the username and password if [ [ $ username = = 'admin' && $ password = = 'hidenseek' ] ]; then echo "Aliant user." else echo "Invalid user." fi
Conclusion:
The next conclusion appears if the script is executed without using any command line argument value. The script stops execution after receiving the first non-initialized variable:
andreyex@andreyex: ~ / Desktop / bash $ bash error4.bash error4.bash: line 7: $ 1: unbound variable andreyex@andreyex: ~ / Desktop / bash $
The next conclusion appears if the script is executed with one value of the command line argument. The script stops execution after receiving a second non-initialized variable:
andreyex@andreyex: - / Desktop / bash $ bash error4.bash admin error4.bash: line 9: $ 2: unbound variable andreyex@andreyex: - / Desktop / bash $
The next conclusion appears if the script is executed with two values of the command line argument — « admin » and « hide ». Here the username is valid, but the password is invalid. So, the message « Invalid user » is displayed:
andreyex@andreyex: - / Desktop / bash $ bash error4.bash admin hide Invalid user. andreyex@andreyex: - / Desktop / bash $
The next conclusion appears if the script is executed with two values of the command line arguments — « admin » and « hidenseek ». Here the username and password are valid. So, the message « Valid user » is displayed:
andreyex@andreyex: - / Desktop / bash $ bash error4.bash admin hidenseek Valid user. andreyex@andreyex: ~ / Desktop / bash $
Conclusion
Various methods for processing errors in the Bash script are shown in this article with a few examples. We hope this helps Bash users implement the error processing function in their Bash scripts.