0% found this document useful (0 votes)
44 views5 pages

Sa Lab

The document contains examples of Bash shell scripts that demonstrate various programming concepts like taking user input, conditional statements, loops, functions, file handling and mathematical operations. Some examples include a name greeting program, calculating sum and factorial of numbers, checking if a number is even or odd, comparing files and finding largest of three numbers.

Uploaded by

lolcatnew
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views5 pages

Sa Lab

The document contains examples of Bash shell scripts that demonstrate various programming concepts like taking user input, conditional statements, loops, functions, file handling and mathematical operations. Some examples include a name greeting program, calculating sum and factorial of numbers, checking if a number is even or odd, comparing files and finding largest of three numbers.

Uploaded by

lolcatnew
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

simple program

_______________

echo "enter your name"


read name
echo "hi $name"

----------------------------

sum of 2 numbers
_________________

echo "enter first number"


read n1
echo "enter second number"
read n2
r=$((n1 + n2))
echo "sum= $r"

-------------------------------

odd or even
_____________

echo "enter the number"


read num

if [ $((num%2)) -eq 0 ]
then
echo "even"
else
echo "odd"
fi

----------------------------
mathmatical operation using nested if
_______________________________________

echo "enter first number"


read n1
echo "enter second number"
read n2

echo "---------------------------------"
echo "_______MENU______________________"
echo "1.add"
echo "2.subtract"
echo "3.muliply"
echo "4.divide"
echo "_________________________________"

echo "selelct"
read opt

if [ $opt = 1 ]
then
r=$((n1+n2))
elif [ $opt = 2 ]
then
r=$((n1-n2))
elif [ $opt = 3 ]
then
r=$((n1*n2))
elif [ $opt = 4 ]
then
r=$((n1/n2))
else
echo "invalid choose"
fi

echo "result= $r"

-------------------------------------------------

factorial
_________________________

echo "enter the number"


read n

f=1
i=1

while [ $i -le $n ]
do
f=$((f*i))
i=$((i+1))
done

echo "fact of $n = $f"

-------------------------------------------------
telephone bill
________________________

echo "BSNL TARIFF"


echo "______________________"
echo "plan \t amount \t free \t rate"
echo "______________________"
echo "p-100 \t rs.100 \t 20 \t rs.2"
echo "p-200 \t rs.200 \t 50 \t rs.1"
echo "open reading"
read opr
echo "close reading"
read clr

unit=$((clr-opr))
echo "select your tariff plan 100 or 200"
read tariff

case $tariff in
100)
nunit=$((unit-20))
ucharge=$((nunit*2));;
200)
nunit=$((unit-50))
ucharge=$((nunit*1));;
*)
echo "invalid choice....!!"
esac

amount=$((tariff+ucharge))

echo "BILL"
echo "tariff \t unit \t extra \t amt \t bill"
echo "_____________________"
echo "$tariff \t $unit \t $nunit \t $ucharge \t Rs.$amount"
echo "_____________________"

--------------------------------------------------------------

compare 2 file and sort


_______________________

echo "enter first file name"


read f1
echo "enter second file name"
read f2

sort $f1 $f2 | uniq -d

--------------------------------------------------------------

time and print greeting


____________________________

dt=$(date | cut -c 12-13)


echo "$dt"

if [ $dt -le 12 ]
then
echo "morning"
else
echo "after noon"
fi

---------------------------------------------------------------

directory or not
_________________

echo "enter a filename"


read f1

if [ -d $f1 ]
then
echo "$f1.. is a Directory"
else
echo "$f1.. is NOT a Directory"
fi

-------------------------------------------------------------

file editable or not


_______________________
echo "enter a filename"
read f1
if [ ! -w $f1 ]
then
echo "sorry..!! $f1 is NOT editable"
else
echo "YES..! $f1 is editable "
fi

----------------------------------------------------------

palindrom
_____________

echo "enter a number"


read n
a=$n
rev=0

while [ $n -gt 0 ]
do
rem=$((n%10))
rev=$((rev*10+rem))
n=$((n/10))
done

echo "the reverse of $a is $rev "

if [ $rev = $a ]
then
echo "palindrom"
else
echo " not palindrom"
fi

-------------------------------------------------------------

largest from 3
________________

echo "enter first number"


read n1
echo "enter second number"
read n2
echo "enter third number"
read n3

if [ $n1 -ge $n2 ] && [ $n1 -ge $n3 ]


then
echo "largest= $n1 "
elif [ $n2 -ge $n1 ] && [ $n2 -ge $n3 ]
then
echo "largest= $n2 "
else
echo "largest= $n3 "
fi

-------------------------------------------------------------
sum of digit
____________

echo "enter the number"


read n
s=0

while [ $n -gt 0 ]
do
d=$((n%10))
s=$((s+d))
n=$((n/10))
done
echo "sum= $s "

You might also like