Sunteți pe pagina 1din 4

Shell Scripting Exercise answer :

1.
Q.1. How to 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
#!/bin/bash
if [ $# -ne 2 ]
then
echo " x and y are two nos for which I will print s!"
exit 1
fi
echo ""! of $1 and $2 is #expr $1 $ $2#"
2.
Q.2.Write Script to find out iggest numer from given three nos. !os are supplies as
command line argument. "rint error if sufficient arguments are not supplied
#!/bin/bash
if [ $# -ne % ]
then
echo "$&' n!ber1 n!ber2 n!ber% are not (i)en" *+2
exit 1
fi
n1,$1
n2,$2
n%,$%
if [ $n1 -(t $n2 ] ++ [ $n1 -(t $n% ]
then
echo "$n1 is -i(est n!ber"
elif [ $n2 -(t $n1 ] ++ [ $n2 -(t $n% ]
then
echo "$n2 is -i(est n!ber"
elif [ $n% -(t $n1 ] ++ [ $n% -(t $n2 ]
then
echo "$n% is -i(est n!ber"
elif [ $1 -e. $2 ] ++ [ $1 -e. $% ] ++ [ $2 -e. $% ]
then
echo "/ll the three n!bers are e.al"
else
echo "I can not fi(re ot which n!ber is bi(er"
fi
%.
Q.#.Write script to print nos as $,%,#,2,1 using while loop.
#!/bin/bash
i,0
while test $i !, &
do
echo "$i"
i,#expr $i - 1#
done
1.
Q.%. Write Script, using case statement to perform asic math operation as
follows
& addition
' sutraction
x multiplication
( division
)he name of script must e *+%* which wor,s as follows
- .(+% 2. ( #, /lso chec, for sufficient command line arguments
#!/bin/bash
if test $# , %
then
case $2 in
$2 let 3,$1$$%44
-2 let 3,$1-$%44
/2 let 3,$1/$%44
x562 let 3,$17$%44
72 echo 8arnin( - $2 in)alied operator9 only $9-9x9/ operator allowed
exit44
esac
echo /nswer is $3
else
echo ":sa(e - $& )ale1 operator )ale2"
echo "8here9 )ale1 and )ale2 are n!eric )ales"
echo "operator can be $9-9/9x ;<or =ltiplication2"
fi
0.
Q.$.Write Script to see current date, time, username, and current director0
#!/bin/bash
echo ">ello9 $?@AB/=C"
echo "Drrent date is #date#"
echo ":ser is #who i a!#"
echo "Drrent direcotry #pwd#"
E.
Q.1.Write script to print given numer in reverse order, for eg. 2f no is 12# it must print as
#21.
#!/bin/bash
if [ $# -ne 1 ]
then
echo ":sa(e' $& n!ber"
echo "I will find re)erse of (i)en n!ber"
echo "<or e(. $& 12%9 I will print %21"
exit 1
fi
n,$1
re),&
sd,&
while [ $n -(t & ]
do
sd,#expr $n F 1&#
re),#expr $re) G7 1& $ $sd#
n,#expr $n / 1&#
done
echo "He)erse n!ber is $re)"
I.
Q.3.Write script to print given numers sum of all digit, 4or eg. 2f no is 12# it*s sum of all
digit will e 1&2&# 5 1.
#!/bin/bash
if [ $# -ne 1 ]
then
echo ":sa(e' $& n!ber"
echo "I will find s! of all di(it for (i)en n!ber"
echo "<or e(. $& 12%9 I will print E as s! of all di(it ;1$2$%2"
exit 1
fi
n,$1
s!,&
sd,&
while [ $n -(t & ]
do
sd,#expr $n F 1&#
s!,#expr $s! $ $sd#
n,#expr $n / 1&#
done
echo ""! of di(it for n!ner is $s!"

J.
bc
K.
:se co!!and as 9 $ echo 0.12 $ 2.0 5 bc
1&.
#!/bin/bash
a,0.EE
b,J.EI
c,#echo $a $ $b 5 bc#
echo "$a $ $b , $c"
11.
#!/bin/bash
if [ $# -ne 1 ]
then
echo ":sa(e - $& file-na!e"
exit 1
fi
if [ -f $1 ]
then
echo "$1 file exist"
else
echo ""orry9 $1 file does not exist"
fi

S-ar putea să vă placă și