Page 1 of 1

Bash Scripting - Using Conditional Statements how to use statements such as if in your scripts Rate Topic: -----

#1 Anarion   User is offline

  • The Persian Coder
  • member icon

Reputation: 387
  • View blog
  • Posts: 1,663
  • Joined: 16-May 09

Post icon  Posted 25 November 2009 - 12:05 PM

In this tutorial we are going to learn about conditionals and using them in Bash Scripts to control things. However, you have to have a good understanding about using variables and writing simple bash scripts (Covered in previous sections).

OK, now to the conditionals... What do they do? They let you decide whether to perform an action or not, this decision is taken by evaluating an expression.

[if statements]
Conditionals have many forms. The most basic form is: if expression then statement where 'statement' is only executed if 'expression' evaluates to true. For example, '2<1' is an expresion that evaluates to false, while '2>1' evaluates to true.
Conditionals have other forms such as: if expression then statement1 else statement2. Here 'statement1' is executed if 'expression' is true,otherwise 'statement2' is executed.
Yet another form of conditionals is: if expression1 then statement1 else if expression2 then statement2 else statement3. In this form there's added only the "ELSE IF 'expression2' THEN 'statement2'" which makes statement2 being executed if expression2 evaluates to true.

OK, now to the syntax used in Bash:
if [ expression ];
#code goes here to be executed if expression is true
The syntax is quite easy to understand, except the "="... if you worked with other languages, you use "==" to check equality, but this is BASH! it's syntax is a little different.

OK, here's a very simple script to practice an "if...else" condition:
#don't forget the space after '[' and also before ']' !!!
if [ $STR1 = $STR2 ]; then
		echo "$STR1 is equal to $STR2"
		echo "$STR1 is not equal to $STR2"
It's a very stupid example :D but just for practice... the output will be "astring is not equal to anotherstring" as you know :)

Now let me tell you something! all that "[ expression ]" thing is a command in Linux! did you know? it is called "test". Check out it's man page to learn more about it and the conditions you can use inside it.

[for loops]
The for loop is really simple, come on! have a look:
#remember about the note we learned in previous tutorial about $(command) ?
for i in $(seq 1 10); do
		echo $i
don't panic! here's the explanation: in the first line of the loop, it tells that i is looped through 1 to 10, then we used a simple echo command inside the loop to print the value of i. It's quite simple :)

[while loops]
while loops are another type of conditional statements that might come handy. Here's an example of the usage:
while [ $counter -le 10 ]; do
		echo $counter
		let counter+=1
*Note* this line is simple too! don't panic. it just lets us change the value of counter variable, we have to update it in order to continue the loop as we wanted. It just increases the value of counter by one.
here comes the last part:

[until loops]
until loops are another type of conditional loops to perform in bash scripts. The syntax is simply as follows:
until [ $counter -gt 10 ]; do
		echo $counter
		let counter+=1
Basically, the loop continues until counter gets bigger than 10.

Now it's time to fulfill my oath to you! In previous tutorial, I promised to teach a simple way to know if a user has root privileges or not. Here's the script which uses an if statement and an environment variable (a variable controlled by OS and available in all scripts):
if [ $EUID -ne 0 ]; then
		echo "You don't have root privileges..."
		exit 1
*Note: we check the environmental variable "EUID" which is set by system to see if it is equal to 0, if false, then the user doesn't have root privileges. And the "exit 1" line tells the script is completed without success... use 0 for success.

This was the tutorial about using conditional statements and loops in Bash Scripts. Hope it was helpful for you :)

Is This A Good Question/Topic? 0
  • +

Page 1 of 1