Bash Scripting: How to Use if
Over the last few weeks I have gone through what I consider to be key points and rules over scripting in general, and Bash scripting specifically.
This week, I’ve decided to take a look at one of the most common commands you’ll use, ‘if’
‘if’ statements – What?
In bash scripting, as in the real world, ‘if’ is used to ask a question. The ‘if’ command will return a yes or no style answer and you can script the appropriate response.
The general format for an if statement is:
if [something] then do option 1 else do option 2 fi
As you may have noticed, I have added a backwards ‘if’ (a ‘fi’) at the end of my statement. This is to close off the ‘if’ argument and is required. For those familiar with html coding is like opening and closing a bold text section using ‘[b]’ bookended with ‘[/b]’.
Nested ‘if’ statements
In addition to a simple ‘if’ statements, you can do something called nested ‘if’s. As the name implies, you can run a second (or more) ‘if’ argument dependant on the result of a first ‘if’ result. The general format will look similar to the following:
if [something] then if [something] then do option 1a else do option 1b fi else do option 2 fi
What can I ask with ‘if’?
If is another powerful tool in the Bash scripting tool box and has a very large number of uses. You can try to run a ‘man’ (manual) command but you will be redirected to a page about shell built in commands, with almost no information.
Unfortunately the only resources I have been able to find was by Googling “if bach” followed by what I wanted to do. However recently I have found a nice webpage with not just a number of basic arguments you can chuck through ‘if’, but even more advanced usage.
if –a [file] – To test if [file] exists.
if –d [directory] – To test if [directory] (otherwise known as a folder) exists.
if –o [file] – To test if [file] is owned by the user running the script.
Check out the links section below for other websites that have useful information.
Ok, now to the reason why you’re here, examples of ‘if’ statements in use!
if –a "/Users/darren/Desktop/Test_File.txt" then echo "Test_File.txt is present" else echo "Test_File.txt is NOT present" fi
This checks if a file called “Test_File.txt” is present in my Desktop folder. If it is, the phrase “Test_File.txt is present” is displayed in the terminal window. If not, the phrase “Test_File.txt is NOT present” is displayed instead.
if –d "/Users/darren/Desktop/Test_Folder" then echo "Folder is present" else echo "Test_File.txt is NOT present" mkdir "/Users/darren/Desktop/Test_Folder" fi
This checks if a folder called “Test_Folder” is present in my Desktop folder. If it is, the phrase “Test_Folder is present” is displayed in the terminal window. If not, the phrase “Test_Folder is NOT present” is displayed and the folder created.
This example is useful if an application requires a folder but, for some reason, the folder cannot be created automatically.
if [$? = 0] then echo "Fine" else echo "Last command exit code indicated a failure." fi
This checks if the result of the command run (before ‘if’) gave an exit code of 0. If it did, the phrase “Fine” is displayed in the terminal window. If not, the phrase “Last command exit code indicated a failure.” is displayed.
This example is great for error checking within a script. The use of the $? variable is what the system uses to store the exit code of the last run command. So dotting a few of these error checking codes after important commands is a good idea, with the ‘then’ and ‘else’ carrying out appropriate clean up and/or notification services.
And that’s it for this week. This little series seems to be going well, and I think a session on ‘grep’ or a general session on pipping commands and variables might be on the cards for next time. However, as always, I’m on the look out for recommendations for future posts, and other peoples opinions and hints.
If this sounds like you, let us know in the comments below and I’ll try to respond to as many as I can.