We've previously explored a little of a command list in our Getting Started with Bash Guide, where we check if a directory already existed in our user home directory, if it already exists, output “Something“, and if does not already exist, create a folder, here is the code:
test -d $HOME/bin && echo "Exist/Not Creating" || mkdir $HOME/bin
In this guide, we would get into the nitty-gritty of creating a simple conditional execution using the command-line lists. The command line lists are two or more statements that are joined using either the AND or OR notations:
- ||: OR
- &&: AND
With the OR notation, it returns true if any one of conditions or commands returns as true. The first condition is always checked but the second condition is checked only if the first condition is returned false.
When you use the AND notation between two conditions, it returns true only if all the conditions are true, the first condition is checked, and if that is true it checks the second condition if that is also true.
The decision of the success or failure of a command is taken by reading the exit code from the program. Zero represents a successful program completion and anything other than a zero will represent a failure.
To test the success or failure of a program or script, you read the exit status by means of the system variable $?, read more in the bash getting started guide, so, for example you can echo the status using:
Let's see a couple of examples...
Are We in User Home Directory, if Otherwise, Cd Into the User Home Directory
test $PWD == $HOME || cd $HOME
If you run the above command, you'll be taken back to the home directory regardless of the directory you are currently in. Let's get to know what the above command is doing:
We started with the test command which is used for file comparison, the test returns a status of 0 (true) or 1 (false) depending on the evaluation of the conditional expression EXPR. Each part of the expression must be a separate argument.
So, we tested if the current working directory ($PWD) equals the user home directory ($HOME), that is are we in the user home directory? The double vertical bars denote an OR list, if the first statement is not true, the second statement is only executed. if we are not currently in the home directory, then CD into the home directory.
Create a bin Directory if It Doesn't Already Exist
test -d $HOME/bin && echo "Exist/Not Creating" || mkdir $HOME/bin && echo "bin directory created"
What the above command does is to check if a bin directory already existed in your user home directory, if it already exists, output “Exist/Not Creating“, and if does not already exist, create the folder, and output "bin directory created"
Write a Message to A Logged-in User
who | grep pascal &> /dev/null && write pascal < msg.txt
The 'who' prints information about users who are currently logged on, we then piped the list to grep to search for username "pascal", since we are not interested in the output, we redirect all of that to /dev/null which discards anything written to it.
The double ampersand indicates that the second statement in the list runs only if the first returns true, so, if the user 'pascal' is logged in, we use the write command to message the user, the message is in the msg.txt file, this is the output:
Message from firstname.lastname@example.org on pts/2 at 20:03 ... If you are currently logged in, please check the mail log! EOF
If the user you want to write to is logged in on more than one terminal, you can specify which terminal to write to by specifying the terminal name as the second operand to the write command. E.g:
who | grep pascal &> /dev/null && write pascal pts/1 < msg.txt
In future guide, we would build an actual script that makes use of this logic