Bash If Conditional Statement
As in many other programming languages, bash also has a conditional statement that can perform many decision-making tasks during automation in bash scripts. In this action is depending upon success or failure of a command. Bash’s if statement allows us to specify such situation.
In bash, we have same kind of if statement as in other programming language, Let discuss type of statements used in bash
1. if ; then ; fi (simple if statement)
2. if ; then ; else ; fi (if-else statement)
3. if ; then ; elif ; else ; fi (if elseif or elif statement)
4. if ; then ; if ; then ; fi ; fi (nested if else statement)
In this post, we will try to cover various aspects in which we can use if condition statement in bash for scripting. we will cover all mentioned above one by one with basic syntax and some examples.
Syntax if ; then ; fi (simple if statement)
if [ condition expression ]; then commands statements fi
This is simple if statement. This allow us evaluate one condition expression and according to success(zero) and fuiler(non-zero) of that expression and if it success(zero) then we can execute some commands for completing our desired task. There are some keywords if, then and fi (in bash if condition end keyword is fi, which is different then any other programming language). In this conditional if statement, we need to put one ondition expression which decide next coure of action of code. I saw sometime guys use to write then keyword in seocnd line, But i prefer to write it in same line as condtion get finished with one semi-colon.Let’s see some examples for same.
#!/bin/bash read -p "Type a integer bigger than 50 :" number if [ $number -gt 50 ]; then echo Correct fi ===================================== OutPut #./if_fi.sh Type a integer bigger than 50 :52 Correct #./if_fi.sh Type a integer bigger than 50 :49
Syntax if ; then ; else ; fi (if-else statement)
if [ condition expression ]; then commands statements else commands statements fi
In this condition statement, we can even able execute some task in case evaluation condition expression got fail.This is like, in case condition expression pass do this otherwise do this. There are one more keyword intrduce is else.In this conditional statmenet, we used else keyword which means in condition in which our first condtion expression is false we can execute or follow below commands.Let’s see some examples for same.
#!/bin/bash read -p "Type a integer bigger than 50 :" number if [ $number -gt 50 ]; then echo Correct else echo Incorrect fi ===================================== OutPut #./if_else_fi.sh Type a integer bigger than 50 :52 Correct #./if_else_fi.sh Type a integer bigger than 50 :49 Incorrect
Syntax if ; then ; elif ; else ; fi (if elseif or elif statement)
if [ condition expression ]; then commands statements elif [ condition expression ]; then commands statements else commands statements fi
This condition statement is used to evaluate second condition expression inside first one. this allow us to check another value according to which we can execute another set of commands.This bash if statement is used to provide another condition expression which can evaulate in such manner in which we have power to evaualte second condition as well in same code without calling another if conditional statement.Let’s see some examples for same.
#!/bin/bash read -p "Type a integer :" number if [ $number -le 50 ]; then echo "Number is less than or equal to 50" elif [ $number -gt 50 ]; then echo "Number is greater than 50" fi ===================================== OutPut #./if_elif_fi.sh Type a integer :52 Number is greater than 50 #./if_elif_fi.sh Type a integer :49 Number is less than or equal to 50
Syntax if ; then ; if ; then ; fi ; fi (nested if else statement)
if [ condition expression ]; then commands statements elif [ condition expression ]; then commands statements else if [ condition expression ]; then commands statements fi
we can use this nested if condition statements with great power through which we can run things in great flexibility, execute commands and situation on many condition expression and in case it will not qualify we always have option to test another condition expression for new evaluation which could fullill our objective.So basic concepts is that any point of time in code, we could change direction of code for next condition expression.
#!/bin/bash read -p "Type a integer :" number if [[ $number =~ ^[+-]?[0-9]*$ ]];then if [ $number -le 50 ]; then echo "Number is integer and less than or equal to 50" exit else echo "Number is integer and greater than 50" exit fi elif [[ $number =~ ^[+-]?[0-9]+\.?[0-9]*$ ]];then if [ $(echo "$number>50"| bc) -eq 0 ]; then echo "Number is float and less than or equal to 50" else echo "Number is float and greater than 50" fi else echo "Try Once more" fi ===================================== OutPut #./if_else_if_fi.sh Type a integer :23 Number is integer and less than or equal to 50 #./if_else_if_fi.sh Type a integer :56 Number is integer and greater than 50 #./if_else_if_fi.sh Type a integer :34.56 Number is float and less than or equal to 50 #./if_else_if_fi.sh Type a integer :67.78 Number is float and greater than 50
Leave a Reply