Objectives: In this chapter you will learn how to:
work with the return code; test files and compare them; test variables, strings and integers; perform an operation with numeric integers;
linux, script, bash, variable
Knowledge: Complexity:
Reading time: 10 minutes
Upon completion, all commands executed by the shell return a return code (also called status or exit code).
If the command ran correctly, the convention is that the status code will be zero.
If the command encountered a problem during its execution, its status code will have a non-zero value. There are many reasons for this: lack of access rights, missing file, incorrect input, etc.
You should refer to the manual of the man command to know the different values of the return code provided by the developers.
The return code is not visible directly, but is stored in a special variable: $?.
mkdir directory
echo $?
0
mkdir /directory
mkdir: unable to create directory
echo $?
1
command_that_does_not_exist
command_that_does_not_exist: command not found
echo $?
127
Note
The display of the contents of the $? variable with the echo command is done immediately after the command you want to evaluate because this variable is updated after each execution of a command, a command line or a script.
Tip
Since the value of $? changes after each command execution, it is better to put its value in a variable that will be used afterwards, for a test or to display a message.
ls no_file
ls: cannot access 'no_file': No such file or directory
result=$?
echo $?
0
echo $result
2
It is also possible to create return codes in a script.
To do so, you just need to add a numeric argument to the exit command.
bash # to avoid being disconnected after the "exit 2
exit 123
echo $?
123
In addition to the correct execution of a command, the shell offers the possibility to run tests on many patterns:
Files: existence, type, rights, comparison;
Strings: length, comparison;
Numeric integers: value, comparison.
The result of the test:
$?=0 : the test was correctly executed and is true;
$?=1 : the test was correctly executed and is false;
Test if the first number is different from the second
-gt
Test if the first number is greater than the second
-lt
Test if the first number is less than the second
Note
Since numeric values are treated by the shell as regular characters (or strings), a test on a character can return the same result whether it is treated as a numeric or not.
test "1" = "1"
echo $?
0
test "1" -eq "1"
echo $?
0
But the result of the test will not have the same meaning:
In the first case, it will mean that the two characters have the same value in the ASCII table.
In the second case, it will mean that the two numbers are equal.
The combination of tests allows you to perform several tests in one command.
It is possible to test the same argument (file, string or numeric) several times or different arguments.
The expr command performs an operation with numeric integers.
expr num1 [+] [-] [\*] [/] [%] num2
Example:
expr 2 + 2
4
Warning
Be careful to surround the operation sign with a space. You will get an error message if you forget.
In the case of a multiplication, the wildcard character * is preceded by \ to avoid a wrong interpretation.