SHELL SCRIPTING || PART 2

FOR PART 1 : CLICK HERE

In the previous part we have seen how to print using echo command and using loops.




COMPARING NUMBERS

If you want to compare between two numbers 

read x
read y
[[ $x -gt $y ]] && echo 'X is greater than Y'
[[ $x -eq $y ]] && echo 'X is equal to Y'
[[ $x -lt $y ]] && echo 'X is less than Y'
exit 0



here -gt compares whether the left hand variable is greater than right hand variable and if condition satisfies it prints the following which is given after echo 

here -eq compares whether the left hand variable is equal to right hand variable and if condition satisfies it prints the following which is given after echo 

here -lt compares whether the left hand variable is less than right hand variable and if condition satisfies it prints the following which is given after echo 


Conditional Statements in Bash.

    In programming language like c , the syntax is as follows

To find the biggest number between the two numbers 

if(x>y)
{
    printf("x is greater than y")
}
else
 {
     printf("y is greater than x")
 }



but in Bash , The syntax is 

read input
if [ $input="y" || $input=="Y" ]
echo "YES"
else  
echo "NO"


To get a better idea see this example of find the type of triangle , whether the triangle is isosceles,equilateral,scalene triangle considering a,b,c are the sides of the triangle.

read a
read b
read c

if [[ $a == $b && $b == $c ]]
then
    echo 'EQUILATERAL';
elif [[ $a == $b || $b == $c || $a == $c ]]
then
    echo 'ISOSCELES';
else
    echo 'SCALENE';
fi




All The commands related to CUT are discussed in the next article.

Thank you 

Please share and follow our blog.




Post a Comment

0 Comments