TechnoByte, The Students Forum
   Linux Operating System

1. Write a shell script to accept two numbers from the user and print their sum.

ans:
echo "Enter 1st number:"
read num1
echo "Enter 2nd number:"
read num2
res=`expr $num1 + $num2`
echo "The Sum is $res"


2. Write a shell script to accept three numbers from the user and print their sum.

ans:
echo "Enter 1st Number:"
read num1
echo "Enter 2nd Number:"
read num2
echo "Entr 3rd Number:"
read num3
res=`expr $num1 + $num2 + $num3`
echo "The Sum Is $res"


3. Write a shell script to accept two numbers from the user and find greatest among them.

ans:
echo "enter 1st Number:"
read num1
echo "enter 2nd Number:"
read num2
if test $num1 -gt $num2
then
   echo "$num1 is greatest"
else
   echo "$num2 is greatest"
fi


4. Write a shell script to accept three numbers from the user and find greatest among them.

ans:
echo "Enter 1st Number:"
read num1
echo "Enter 2nd Number:"
read num2
echo "Enter 3rd Number:"
read num3
if test $num1 -gt $num2 -a $num1 -gt $num3
then
   echo "$num1 is greatest"
elif test $num2 -gt $num3
then
   echo "$num2 is greatest"
else
   echo "$num3 is greatest"
fi


5. Write a shell script to print first 10 natural numbers in ascending order.

ans:
num=1
while test $num -le 10
do
   echo $num
   num=`expr $num + 1`
done


6. Write a shell script to print first 20 natural numbers in decending order.

ans:
num=20
while test $num -ge 1
do
   echo $num
   num=`expr $num - 1`
done


7. Write a shell script to print the first 20 numbers of fibonasi series.

ans:
a=0
b=1
i=1
echo "$a"
echo "$b"
while test $i -le 18
do
   c=`expr $a + $b`
   echo "$c"
   a=$b
   b=$c
   i=`expr $i + 1`
done


8. Write a shell script to accept a numbers from the user and test if it is prime or not.

ans:
i=2
flag=0
echo "enter a number"
read num
while test $i -lt $num
do
   rem=`expr $num % $i`
   if test $rem -eq 0
   then
       flag=1
      break
   fi
   i=`expr $i + 1`
done

if test $flag -eq 0
then
   echo "The Number Is Prime"
else
   echo "The Number is Not Prime"
fi


9. Write a shell script to print prime numbers between 1 to 100.

ans:
n=2
while test $n -le 100
do
   flag=0
   i=2
   while test $i -lt $n
   do
      rem=`expr $n % $i`
      if test $rem -eq 0
      then
         flag=1
         break
      fi
      i=`expr $i + 1`
   done
   if test $flag -eq 0
   then
      echo "$n"
   fi
   n=`expr $n + 1`
done


10. Write a shell script to print the first 100 prime numbers.

ans:
count=0
n=2
while test $count -lt 100
do
   flag=0
   i=2
   while test $i -lt $n
   do
      rem=`expr $n % $i`
      if test $rem -eq 0
      then
         flag=1
         break
      fi
      i=`expr $i + 1`
   done
   if test $flag -eq 0
   then
      count=`expr $count + 1`
      echo "$count. $n"
   fi
   n=`expr $n + 1`
done


11. Write a shell script to accept a number from the user and print back the reverse of that number.

ans:
rev=0
echo "enter a number"
read num
while test $num -gt 0
do
   r=`expr $num % 10`
   rev=`expr $rev \* 10`
   rev=`expr $rev + $r`
   num=`expr $num / 10`
done
echo "$rev"


12. Write a shell script to print all the palaindrom numbers between 1 to 500.

ans:
count=1
num=1
while test $count -le 500
do
   n=$num
   rev=0
   while test $n -ne 0
   do
      r=`expr $n % 10`
      rev=`expr $rev \* 10`
      rev=`expr $rev + $r`
      n=`expr $n / 10`
   done
   if test $num -eq $rev
   then
      echo "$count) $num"
      count=`expr $count + 1`
   fi
   num=`expr $num + 1`
done


13. Write a shell script to accept a number from the user and find the binary of that number.

ans:
echo "Enter a Number:"
read n
b=""
while test $n -gt 0
do
   tmp=`expr $n % 2`
   b=$tmp$b
   n=`expr $n / 2`
done
echo $b


14. Write a shell sript to print all the binary numbers from 0 to 500.

ans:
num=0
while test $num -gt 500
do
   echo "$num"
   n=$num
   b=""
   while test $n -gt 0
   do
      temp=`expr $n + 2`
      b=$temp$b
      n=`expr $n / 2`
   done
   echo "$num) $b"
done


15.Write a shell script to accept a number from the user and print its hexa and octal equivalent.

ans:
echo "enter a number"
read n
num=$n;
o=""
while test $num -gt 0
do
   temp=`expr $num % 8`
   o=$temp$o
   num=`expr $num / 8`
done
echo $o
num=$n;
h=""
while test $num -gt 0
do
   temp=`expr $num % 16`
   h=$temp$h
   num=`expr $num / 16`
done
echo $h


16. Write a shell script to print the binary, octal and hexadecimal equivalent of all the numbers from 1 to 500.

ans:
num=0
while test $num -le 500
do
   n=$num
   b=""
   while test $n -gt 0
   do
      temp=`expr $n % 2`
      b=$temp$b
      n=`expr $n / 2`
   done
   n=$num
   o=""
   while test $n -gt 0
   do
      temp=`expr $n % 8`
      o=$temp$o
      n=`expr $n / 8`
   done
   n=$num
   h=""
   while test $n -gt 0
   do
   temp=`expr $n % 16`
   if test $temp -eq 10
   then
      h="A"$h
   elif test $temp -eq 11
   then
      h="B"$h
   elif test $temp -eq 12
   then
      h="C"$h
   elif test $temp -eq 13
   then
      h="D"$h
   elif test $temp -eq 14
   then
      h="E"$h
   elif test $temp -eq 15
   then
      h="F"$h
   else
      h=$temp$h
   fi
   n=`expr $n / 16`
   done
   echo "$num , $b , $o , $h"
   num=`expr $num + 1`
done


17. Write a shell script to print 1st 10 natural numbers using for loop.

ans:
clear
for i in 1 2 3 4 5 6 7 8 9 10
do
   echo $i
done


18. Write a shell script to print the list of all the files and directories.

ans:
clear
for i in 'ls - l'
do
   echo $i
done


19. Using untill loop, write a shell script to print the first 10 natural numbers.

ans:
num=1
untill test $num -gt 10
do
   echo $num
   num='expr $num + 1'
done


20. Write a shell script to print the list of all the files and directories.

clear
for i in `ls`
do
  if test -d $i
  then
      echo "$i is directory"
  elif test -f $i
  then
      echo "$i is file"
  else
      echo "$i is not a file or directory"
  fi
done