Bash Shell Conditional Expressions
The mostly used bash shell scripting conditional expressions given below, its very useful for who want learn scripting or programming languages. I hope you it useful for you. please fallow my youtube channel for interesting linux or scripting updates.
-eq - is equal to - if [ "$a" -eq "$b" ]
-ne - is not equal to - if [ "$a" -ne "$b" ]
-gt - is greater than - if [ "$a" -gt "$b" ]
-ge - is greater than or equal to - if [ "$a" -ge "$b" ]
-lt - is less than - if [ "$a" -lt "$b" ]
-le - is less than or equal to - if [ "$a" -le "$b" ]
< - is less than - (("$a" < "$b"))
<= - is less than or equal to - (("$a" <= "$b"))
> - is greater than - (("$a" > "$b"))
>= - is greater than or equal to - (("$a" >= "$b"))
#!/bin/bash
#If statemet
#Syntax :
if [ condition ]
then
echo " Condition true"
else
echo " Condition false"
fi
==============================
Example
#! /bin/bash
#1).a=b condition
a=10
b=30
if [ $a == $b ]
then
echo " a is equal to b"
else
echo " a is not equal to b"
fi
==============================
Example
#!/bin/bash
#2).a Greaterthan or eqal b condition
echo "Enter a Value: "
read a
echo "Enter a Value: "
read b
if [ $a -eq $b ]
then
echo " a is greaterthan to b"
else
echo " a is lessthan to b"
fi
================================
#!/bin/bash
#3).a lessthan or eqal b condition
a=10
b=30
if [ $a -el $b ]
then
echo " a is lessthan to b"
else
echo " a is greaterthan to b"
fi
==================================
##!/bin/bash
#Purpose: Find biggest Number among 4 digits
echo -e "Please Enter a Value: \c"
read -r a
echo -e "Please Enter b Value: \c"
read -r b
echo -e "Please Enter c Value: \c"
read -r c
echo -e "Please Enter d Value: \c"
read -r d
if [ $a -gt $b -a $a -gt $c -a $a -gt $d ]; then
echo "$a a is big"
elif [ $b -gt $c -a $b -gt $d ]; then
echo "$b b is big"
elif [ $c -gt $d ]; then
echo "$c c is big"
else
echo "$d d is big"
fi
# END #
========================================
Real time example - CPU Utilization
#!/bin/bash
#Script for CPU Utlization
top
read %cpu
if [ condition ]
then
echo " Condition true"
else
echo " Condition false"
fi
x
x
x
-eq - is equal to - if [ "$a" -eq "$b" ]
-ne - is not equal to - if [ "$a" -ne "$b" ]
-gt - is greater than - if [ "$a" -gt "$b" ]
-ge - is greater than or equal to - if [ "$a" -ge "$b" ]
-lt - is less than - if [ "$a" -lt "$b" ]
-le - is less than or equal to - if [ "$a" -le "$b" ]
< - is less than - (("$a" < "$b"))
<= - is less than or equal to - (("$a" <= "$b"))
> - is greater than - (("$a" > "$b"))
>= - is greater than or equal to - (("$a" >= "$b"))
#!/bin/bash
#If statemet
#Syntax :
if [ condition ]
then
echo " Condition true"
else
echo " Condition false"
fi
==============================
Example
#! /bin/bash
#1).a=b condition
a=10
b=30
if [ $a == $b ]
then
echo " a is equal to b"
else
echo " a is not equal to b"
fi
==============================
Example
#!/bin/bash
#2).a Greaterthan or eqal b condition
echo "Enter a Value: "
read a
echo "Enter a Value: "
read b
if [ $a -eq $b ]
then
echo " a is greaterthan to b"
else
echo " a is lessthan to b"
fi
================================
#!/bin/bash
#3).a lessthan or eqal b condition
a=10
b=30
if [ $a -el $b ]
then
echo " a is lessthan to b"
else
echo " a is greaterthan to b"
fi
==================================
##!/bin/bash
#Purpose: Find biggest Number among 4 digits
echo -e "Please Enter a Value: \c"
read -r a
echo -e "Please Enter b Value: \c"
read -r b
echo -e "Please Enter c Value: \c"
read -r c
echo -e "Please Enter d Value: \c"
read -r d
if [ $a -gt $b -a $a -gt $c -a $a -gt $d ]; then
echo "$a a is big"
elif [ $b -gt $c -a $b -gt $d ]; then
echo "$b b is big"
elif [ $c -gt $d ]; then
echo "$c c is big"
else
echo "$d d is big"
fi
# END #
========================================
Real time example - CPU Utilization
#!/bin/bash
#Script for CPU Utlization
top
read %cpu
if [ condition ]
then
echo " Condition true"
else
echo " Condition false"
fi
x
x
x
Comments
Post a Comment