How to use logical operators with exit code In Linux

The ‘&&’ logical operator is used for a successful exit code and ‘||’ logical operator is used for an unsuccessful exit code. Let’s test valid and invalid user scenarios using logical operators. The following command allows test_user to log in as it is a valid user.

cat /etc/passwd | grep test_user && su test_user || echo “invalid user”

 

The following command prints “invalid user” as the jack is not present in the server.

cat /etc/passwd | grep jack && su jack || echo “invalid user”

 

Conclusion:

Thus we have learned about various exit codes, how to read them from a terminal or script and how to use them with logical operators.



How to use exit code to read from terminal, from script and with logical operators

Scripting is an important part of automation in Linux and it is one of the key tools every system administrators use to manage day-to-day activities like taking backups, adding/removing users/groups, updating/removing packages, etc. Since bash is the default shell in most of the Linux distributions, bash scripting is very efficient as it can simplify certain operations that are hard to accomplish with GUI.

A bash script is a plain text file that includes single command or series of commands that are read and executed by the bash program. The most crucial part while writing a bash script is to handle errors effectively. When not handled properly, it may leave less of an adverse impact on the system. Exit code is the traditional method of detecting errors in bash script. Thus in this article, we shall discuss various exit codes, and how to read and handle errors using them.

Bash Exit Code

Exit code is returned by bash for every command executed in the terminal. This code can be used to display error messages when there is any problem while running the script. When a command is executed successfully, bash returns 0 as the exit code, and for any error while executing the command non-zero value is returned which varies from 1 to 255 based on the error type. 

Exit code

Description

Examples

0 Successful execution  
1 Catch generic errors

“Divide by zero”,

“missing operand”,

“Permission denied”

2 Improper command usage

“Missing keyword”,

“No such file or directory”

  Unable to execute the command Permission problem
127 The issue in path resolution Command not found
130 Fatal error When interrupted by Ctrl+C
255 Out of range When the exit code exceeds 255

Note : The exit code only informs that the script or command is executed fine, but it doesn’t tell whether the output from the executed command has got any value or not. For example, listing files in the home directory (doesn’t have any files other than hidden files), so simply giving ls will not give any output, but $? gives exit code as 0 which means ls command got executed successfully. 

pwd
ls
echo $?

 

Similar Reads

Read the Exit Code from terminal

Bash uses a special variable ‘$?‘ which holds the exit code of the last command executed either through the terminal or via script. Let us try to execute some commands in the terminal and check their exit code....

Read the Exit Code from a script

Exit code can also be read from the bash script using $?. It can either be assigned to a variable or displayed directly in the echo command. Let’s write a sample script list.sh which would list two files and display the exit code based on the file presence....

Use the Exit code to do a task

Let’s create a script named user.sh which will take a username as input. The script will check if the entered user is valid or invalid based on the exit code returned by “cat /etc/passwd | grep $username”. For valid users, exit code 0 is returned and for invalid users, exit code 1 is returned....

Using logical operators with exit code

The ‘&&’ logical operator is used for a successful exit code and ‘||’ logical operator is used for an unsuccessful exit code. Let’s test valid and invalid user scenarios using logical operators. The following command allows test_user to log in as it is a valid user....