We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 6
Experiment - 3
3. Shell Programming: Shell script based on control structure- If-then-
fi, if-thenelse-if, nested if-else, to find: 3.1 Greatest among three numbers. #!/bin/bash echo "Enter Num1" read num1 echo "Enter Num2" read num2 echo "Enter Num3" read num3 if [ $num1 -gt $num2 ] && [ $num1 -gt $num3 ] then echo $num1 elif [ $num2 -gt $num1 ] && [ $num2 -gt $num3 ] then echo $num2 else echo $num3 fi 3.2 To find a year is leap year or not. #!/bin/bash echo "enter the year :" read y a=`expr $y % 4` b=`expr $y % 100` c=`expr $y % 400` # -eq is for equal to #-ne is for not equal to if [ $a -eq 0 -a $b -ne 0 -o $c -eq 0 ] then echo "$y is leap year" else echo "$y is not leap year" fi 3.3 To input angles of a triangle and find out whether it is valid triangle or not. #!/bin/bash echo "enter angle A" read A echo "enter angle B" read B echo "enter angle C" read C # sum all three angles d=$((A+B+C)) if [ $A -eq 0 -o $B -eq 0 -o $C -eq 0 ] then echo "Enter angles greater than zero" else if [ $d == 180 ]; then echo "It is a triangle" else echo "It is not a triangle" fi fi 3.4 To check whether a character is alphabet, digit or special character. #!/bin/bash echo "enter a char" read c #set specified is [a-z], which is a shorthand way of typing [abcdefghijklmnopqrstuvwxyz] #set specified is [0-9], which is a shorthand way of typing [0123456789] If [[ $c == [A-Z] ]]; then echo "you have entered an alphabet" elif [[ $c == [a-z] ]]; then echo "you have entered an alphabet" elif [[ $c == [0-9] ]]; then echo "you have entered is a digit" else echo "you have entered a special symbols!" fi 3.5 To calculate profit or loss. #!/bin/bash echo "input the cost price of an item" read cp echo "input the selling price of the item" read sp if [ $cp -eq $sp ] then echo "no profit or no gain" fi if [ $cp -gt $sp ] then s=$((cp - sp)) echo "loss of Rs: $s" else s=$((sp - cp)) echo "profit of Rs: $s" fi