OS SHELL SCRIPTING
Write and execute shell scripts for the following (Using if, while, until and break statements):
Input two numbers. Compare them using if statement and output their relative magnitudes.
#!/bin/bash
echo "enter the two numbers"
read num1 num2
if [ $num1 -lt $num2 ]
then
echo "num1 is smaller then num2 by `expr $num2 - $num1`"
elif [ $num1 -gt $num2 ]
then
echo "num2 is greater then num1 by `expr $num1 - $num2`"
fi
Input two numbers. Display all numbers between (including) them using while statement. Calculate their sum using until statement and display it.
#!\bin\bash
echo "enter the two numbers till you want to get addition"
read first_num
read second_num
sum=0
i=$first_num
while [ $i -le $second_num ]
do
echo $i
i=`expr $i + 1`
done
i=0
sum=0
until [ $i -gt $second_num ]
do
sum=`expr $sum + $i`
i=`expr $i + 1`
done
echo "total of given number is $sum"
Write and execute shell scripts for the following (Using case statement):
Input a number. Output whether it is zero or non-zero
#!/bin/bash
echo "enter the number to check wheter it is zero or non-zero"
read num
case $num in [0])
echo "$num is equal to zero"
;;
*)
echo "$num equal to non-zero"
;;
esac
Input a character. Output whether is an upper-case alphabet, a lower-case alphabet, a digit or a special character.
#!/bin/bash
echo "enter input to check upper case or lower case or number or spical charecter"
read num
case $num in [A-Z])
echo "$num is upper case"
;;
[a-z])
echo "$num is lower case"
;;
[0-9])
echo "$num is number"
;;
*)
echo "$num is special case"
;;
esac
Comments
Post a Comment