Wednesday, 22 July 2015

2.shell script to check whether the given number is odd or even and whether it is positive, negative or zero

echo "Check the number if its EVEN/ODD/POSITIVE/NEGATIVE/ZERO"
echo "*******************************************************"
echo "enter a number"
read num
case $num in
   *[a-z]*)echo "Invalid input"
                        echo "Error!!!"
      exit
      ;;
   *)break
      ;;
esac
op=1
while [ $op -eq 1 ]
do
echo "-----------------------------------------------------------"
echo " 1. POSITIVE/NEGATIVE"
echo " 2. EVEN/ODD"
echo " 3. Zero"
echo "Choose the option you would like to check for your number"
read ch
case $ch in
1)         if [ $num -gt 0 ]
            then
                        echo " The number you have typed is a Positive Number"
            else
                        echo " The number you have typed is a Negative Number"
            fi
            ;;         
2)         if [ `expr $num % 2` -eq 0 ]
            then
                        echo " The number you have typed is an Even number"
            else
                        echo "The number you have typed is an Odd number"
            fi
            ;;
3)         if [  $num -eq 0 ]
            then
                        echo " The number you have typed is a ZERO!"
            else
                        echo "The number you have typed is not Zero!"
            fi
            ;;
*) break
esac
echo "Would you like to check for another option? y=1/n=0"
read op
done



No comments:

Post a Comment