Control Mechanisms of the UNIX Shell - The true/false statement
(Page 2 of 4 )
If we want to implement a correct decision making mechanism, first we need to determinate what is true and what is false. Every command you run can either succeed or fail for some reason. The command will signal this to the operating system via a returned value. This is the program's exist status.
This value may also hide the reason for the error. On a command line this will be the value that the last executed command returned. The shell is kind enough to store this value for us in the $? special variable. We can list its value with the help of the echo command.
touch alfa.txt
ls alfa.txt
alfa.txt
echo $?
0
rm alfa.txt
ls alfa.txt
ls: alfa.txt: No such file or directory
echo $?
2
You can see that for a successful run, this is zero. When a problem occurred, we got the value two. This may differ depending on the error; however, it will always be a positive integer number. The error code depends on the command. You can read after this the manual (use the man or help command) for the tool.
If you search in the manual of ls you will see: “Exit status is 0 if OK, 1 if minor problems, 2 if serious trouble." This logic is somehow inverse to other programming languages. In C/C++, C#, Java, and so forth, a failure is zero and any other value is true. There exists a POSIX rule on what the return value of a program should be. The table below will show you this:
$? Value
What does it mean?
0
Success
>0
Error when interpreting the command. Errors on the redirection, replace of shell variables also enter at this category
1-125
The program managed to finish, however it exited with an error
126
The command exists, nevertheless we cannot run it
( no right to do so, invalid binary file, etcetera)
127
The commend does not exist (cannot find it)
>128
The program stopped with a signal we sent
(like Ctrl + Z, Ctrl + C, etcetera)
The &&, || and ! Structures
These are the short circuit operators. You can use these in a single command line also. The && will treat the and case, the || will work for the or case while the ! (Negation) will help in using the first two. They were named short circuit operators because their evaluation will stop once the result is obvious.
Translating, this means that the and will stop after the first false, and the or will stop at the first true result of a command inside a command line structured with these operators. We use them as follows:
ls alfa.txt && echo “The file was found”
ls alfa.txt || ls beta.txt || ls omega.txt
This should illustrate how they work. The first will print out “The file was found” message only if ls managed to find it. The second one will print out the data of either the alfa.txt, beta.txt or omega.txt. However, only the first one of them was found. We can also combine them to create a simple if-then-else structure.
ls alfa.txt && echo “The file was found” || echo “There exists no such file”
The negation mark will negate the result of a command. If that was true, it will turn it to false, and vice versa. The negation will also have as an effect that, inside the $? we will have a negated value. For instance:
! ls alfa.txt
echo $0
1
In the upper scenario, there was an alfa.txt. The ls command returned zero, however it was immediately negated by the exclamation mark and this negated value is inside the $? special variable.