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



1.shell script to print prime numbers up to a given range using arguments

echo " PRIME NUMBERS"
echo " **************" 
if [ $# -gt 2 -o $# -eq 0 ]
then
    echo -n "Enter 2 input to test: "
    read num1 num2
else
    num1=$1
    num2=$2
fi
case $num1 in
   *[!0-9]*)echo "Invalid input "
                                echo " ERROR!!"
      exit
      ;;
   *)break
      ;;
esac
case $num2 in
   *[!0-9]*)echo "Invalid input "
                                echo " ERROR!!"
      exit
      ;;
   *)break
      ;;
esac
echo " The Prime Numbers between $num1 to $num2  ARe:"
while [ $num1 -le $num2 ]
do
   i=2
   while [ $i -lt $num1 ]
   do
      if [ `expr $num1 % $i` -eq 0 ]
      then
         break
      fi
      i=`expr $i + 1`
   done
               
   if [ `expr $num1 / $i` -eq 1 ]
   then
      echo "$num1"
   fi
   num1=`expr $num1 + 1`
done