facebook youtube pinterest twitter reddit whatsapp instagram

Simple Conditional Execution With (Command Lists)

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:

 echo $?

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 pascal@blog.devsrealm.com 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

Related Post(s)

  • Understanding The Four (4) Ways Command-Line Runs Shell Script

    Sometimes we need to think like the command line shell in order to understand how things are done in the background, this would make debugging a breeze. In this guide, you would learn all the variou

  • Test Shell Builtin Command

    The test command can be a shell builtin or file executable (if you specify the full path to the file) that is used for file comparison, it returns a status of 0 (true) or 1 (false) depending on the

  • Creating Conditional Statements Using if And Else (Bash)

    We have recently explored command-line list, we also dived into the test shell builtin command, which can not only be used to check file types but can also be used to compare values. The issue with u

  • Local Variable, Typset and The Declare Command In Bash

    Local variables are private inside of a function and the most important thing about a local variable is that when it is changed in a function, it doesn't affect the variable outside of a function. If