Wednesday, 22 July 2015

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





No comments:

Post a Comment