Introduction
Conditional statements are indispensable to any useful scripting and full-featured programming language. On the third part of our Shell Script tutorial, you’ll learn all the “if’s” and “else’s” of bash, and how to use them to your advantage. This article has also been written assuming you have already configured your shell script folder according to the first tutorial.
The “if” command
A conditional statement is used to perform certain actions if a condition is true or false. With shell scripts, this is performed by the “if” command. It’s followed by an expression that’s going to be tested. This expression can also be the exit code of the execution of a command, a mathematical expression, beyond other various things. When working with exit codes, the command is pretty straightforward:
if ls folder then echo "Folder exists" fi
If the folder exists, the echo command will run, because ls will have returned exit code 0, as in successful. Otherwise, if the folder doesn’t exist, the text will not be displayed. All “if” statements need to be followed by the “then” command, and ended with “fi”. If you’re not working with exit codes, and want to test a mathematical expression for example, you’ll need the “test” command. There are the following operators in shell script to compare numbers: Continue reading How To Write a Simple Shell Script on a VPS (Part 3)