You are on page 1of 8

C. U.

Shah College Of Engineering & Technology


B.E- IVth CE/IT (Operating System)(140702)
PRACTICAL_SOLUTION_2010

2. Write a shell script to generate desktop calculator.

echo "enter the first number"


read n1
echo "enter the second number"
read n2
echo "menu
1)add
2)sub
3)mul
4)div
5)mod."

echo "enter the choice"


read ch
case $ch in
1)n3=`expr $n1 + $n2`;;
2)n3=`expr $n1 - $n2`;;
3)n3=`expr $n1 \* $n2`;;
4)if [ $n2 -eq 0 ]
then
echo"div by zreo error"
exit
else
n3=`expr $n1 / $n2`
fi;;
5)if [ $n2 -eq 0 ]
then
echo divide by zero error
exit
else
n3=`expr $n1 % $n2`
fi;;
*)echo "invalid input"
esac
echo "the result is $n3"
C. U. Shah College Of Engineering & Technology
B.E- IVth CE/IT (Operating System)(140702)
PRACTICAL_SOLUTION_2010

3. Write a shell script which display factorial of given no.

echo "enter the no"


read n1
i=1
while [ $n1 -ne 1 ]
do
i=`expr $n1 \* $i`
n1=`expr $n1 - 1`
done
echo the fact no is:$i

4. Write a shell script to check entered string is palindrome or not using


command line argument.

if [ $# -eq 1 ]
then
echo enter string is $1
str=$1
echo $str > rev1
k=`head rev1 | wc -c`
z=$k
i=1
while [ $i -le $k ]
do
x=`cut -c $z rev1`
if [ $i -eq 1 ]
then
rev=$x
C. U. Shah College Of Engineering & Technology
B.E- IVth CE/IT (Operating System)(140702)
PRACTICAL_SOLUTION_2010

else
rev=$rev$x
fi
i=`expr $i + 1`
z=`expr $z - 1`
done
if [ "$str" = "$rev" ]
then
echo string is palindrome
else
echo string is not
fi
else
echo enter the string
fi

5. Write a shell script to generate prime number between 1 to 100.

echo Enter the Starting point


read n1
echo Enter the Ending Point
read n4
while [ $n1 -le $n4 ]
do
n2=2
flag=0

while [ $n2 -lt $n1 ]


do
n3=`expr $n1 % $n2`
if [ $n3 -eq 0 ]
then
flag=1
fi
n2=`expr $n2 + 1`
done
if [ $flag -eq 0 ]
then
C. U. Shah College Of Engineering & Technology
B.E- IVth CE/IT (Operating System)(140702)
PRACTICAL_SOLUTION_2010

echo -n $n1 "..."


fi
n1=`expr $n1 + 1`
done

6. Write a shell script to generate hailstone series. The series must be


terminate at 1.
If No is odd then
No=3*No+1
If No is even then
No=No/2

echo enter no
read n

while [ $n -ne 1 ]
do
n3=`expr $n % 2`
if [ $n3 -eq 0 ]
then
n=`expr $n / 2`
echo -n $n ","
else
n=`expr $n \* 3 + 1`
echo -n $n ","
fi
done

7. Write a shell script that copy files without copy command.

if [ $# -eq 1 ]
then
echo “1.Append to file ”
echo “2.overwrite file to second file”

echo “enter your choice ”


read a
case $a in
C. U. Shah College Of Engineering & Technology
B.E- IVth CE/IT (Operating System)(140702)
PRACTICAL_SOLUTION_2010

1)
cat $1>>s
echo “file is copy to s with overwrite ”
cat s
;;
2)
cat $1>s
echo “file is copy to s ”
cat s
;;
*) echo “enter only 1 or 2”
;;
esac
else
echo “enter only one argument”

8. Write Shell script that will add two nos, which are supplied as
command line argument, and if this two nos are not given show error
and its usage?

if [ $# -ne 2 ]
then
echo "Usage - $0 x y"
echo " Where x and y are two nos for
which I will print sum"
exit 1
fi
echo "Sum of $1 and $2 is `expr $1 + $2`"
C. U. Shah College Of Engineering & Technology
B.E- IVth CE/IT (Operating System)(140702)
PRACTICAL_SOLUTION_2010

9. Write a shell script to accept a number in command line argument


and display the sum up to that no. by default sum up to 50 should be
displayed.

if [ $# = 1 ]
then
sum=0
n=$1
while [ $n -ne 0 ]
do
sum=$((n+sum))
n=$((n-1))
done
echo "sum=$sum"
else
sum=0
r=50
while [ $r -ne 0 ]
do
sum=$((r+sum))
r=$((r-1))
done
echo "sum=$sum"
fi
C. U. Shah College Of Engineering & Technology
B.E- IVth CE/IT (Operating System)(140702)
PRACTICAL_SOLUTION_2010

10. Write a shell script to accept one or more filename and convert its
content to upper case.

for file in $*
do
tr '[a-z]' '[A-Z]' <$file
done
C. U. Shah College Of Engineering & Technology
B.E- IVth CE/IT (Operating System)(140702)
PRACTICAL_SOLUTION_2010

11. Write script to determine whether given file exist or not, file name is
supplied as command line argument, also check for sufficient number of
command line argument.

if [ $# -ne 1 ]
then
echo "Usage - $0 file-name"
exit 1
fi

if [ -f $1 ]
then
echo "$1 file exist"
else
echo "Sorry, $1 file does not exist"
fi

For deleting Character- ESC+x


For deleting word- ESC+w
For deleting Line- ESC+dd.

You might also like