0% found this document useful (0 votes)
14 views

Shell Scripts

This document contains 16 code snippets demonstrating various programming concepts in shell scripting such as checking if a number is even or odd, finding the factorial of a number, comparing files, checking if a file is a directory, generating a telephone bill with different plans, checking if a file is editable, determining if a number is a palindrome, finding the largest of 3 numbers, summing the digits of a number, converting a decimal number to binary, checking for prime numbers, generating the Fibonacci series, and calculating factorials from 1 to 20 using nested loops. Each code snippet is labeled and includes comments to explain its purpose.

Uploaded by

lolcatnew
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Shell Scripts

This document contains 16 code snippets demonstrating various programming concepts in shell scripting such as checking if a number is even or odd, finding the factorial of a number, comparing files, checking if a file is a directory, generating a telephone bill with different plans, checking if a file is editable, determining if a number is a palindrome, finding the largest of 3 numbers, summing the digits of a number, converting a decimal number to binary, checking for prime numbers, generating the Fibonacci series, and calculating factorials from 1 to 20 using nested loops. Each code snippet is labeled and includes comments to explain its purpose.

Uploaded by

lolcatnew
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

# (01) Check a Number Even or Odd

echo "Enter a Number ? "


read num

r=`expr $num % 2`

if test $r = 0
then
echo "The Number is EVEN...!!"
else
echo "The Number is ODD...!!"
fi

# (02) Find Factorial using 'While-Do-Done' Loop

echo "Enter Number ? "


read n

i=1
f=1

while [ $i -le $n ]
do
f=`expr $f \* $i`
i=`expr $i + 1`
done

echo "-------------------------------"
echo ".... Factorial of $n = $f ...."
echo "-------------------------------"

# (03) Compare Two Files and Sort out into Unique Data
echo "Enter First File Name ? "
read f1
echo "Enter Second File Name ? "
read f2
sort $f1 $f2 | uniq -d

# (04) Check whether a given name is a 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
# (05) Telephone Bill using CASE Structure
echo " "
echo "\t * B S N L T A R I F F"
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 "------------------------------------------"
echo "Enter Opening Reading ? "
read opr
echo "Enter Closing Reading ? "
read clr
units=`expr $clr - $opr`
echo "Select Your Tarif Plan 100 or 200 ? "
read tariff
case $tariff in
100)
nunits=`expr $units - 20`
ucharge=`expr $nunits \* 2`;;
200)
nunits=`expr $units - 50`
ucharge=`expr $nunits \* 1`;;
*)
echo ".....Invalid Choice.....!!!"
esac
amount=`expr $tariff + $ucharge`
echo " "
echo " * T E L E P H O N E B I L L *"
echo "------------------------------------------"
echo "Tariff \t Units \t Extra \t Amt \t Bill"
echo "------------------------------------------"
echo "$tariff \t $units \t $nunits \t $ucharge \t Rs. $amount"
echo "------------------------------------------"
echo " "

# (06) Check Whether a File is 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

# (07) Check Whether a given number is Palindrome or Not


echo "Enter a Number ? "
read n
a="$n"
rev=0

while [ $n -gt 0 ]
do
rem=`expr $n '%' 10`
rev=`expr $rev '*' 10 + $rem`
n=`expr $n '/' 10`
done
echo "The Reverse of $a is.. $rev"
if test $rev = $a
then
echo "Yes..!! $a is a Palindrome... :)"
else
echo "No..!! $a is NOT a Palindrome... :("
fi
echo ""

# (08) Find Largest from 3 Numbers


echo "Enter the First Number ? "
read n1
echo "Enter the Second Number ? "
read n2
echo "Enter the Third Number ? "
read n3
if [ $n1 -ge $n2 ] && [ $n1 -ge $n3 ]
then
echo ".....Largest =.....$n1"
elif [ $n2 -ge $n1 -a $n2 -ge $n3 ]
then
echo ".....Largest =.....$n2"
else
echo ".....Largest =.....$n3"
fi

# (09) Sum of Digits of a Five Digit Number


echo 'Enter the Number ? '
read num
sum=0
while [ $num -gt 0 ]
do
digit=`expr $num % 10`
sum=`expr $sum + $digit`
num=`expr $num / 10`
done
echo .......... Sum = $sum ..........

# (10) Convert a Decimal Number into Binary Equivalent


echo "Enter a Decimal Number ? "
read num
bin=""
while [ $num -gt 0 ]
do
rem=`expr $num % 2 `
bin=$rem$bin
num=`expr $num / 2 `
done
echo ".....Binary Equivalent =..... $bin"
# (11) Check Whether a Number is Palindrome or Not
echo '.....Enter the Number..... ? '
read n
num=$n
rev=
while [ $num -gt 0 ]
do
digit=`expr $num % 10`
rev=$rev$digit
num=`expr $num / 10`
done
if [ $rev = $n ]
then
echo ".....The Number is Palindrome....."
else
echo ".....The Number NOT Palindrome....."
fi

# (12) Find Largest from 'n' Numbers


echo ".....Enter the Limit..... ? "
read limit
count=0
large=0
while [ $limit -gt 0 ]
do
echo ".....Enter the Number $count....."
read num
if [ $num -gt $large ]
then
large=$num
fi
limit=`expr $limit - 1`
count=`expr $count + 1`
done
echo ".....Largest from $count Numbers =..... $large"

# (13) Print First 'n' Numbers in Fibnocci Series


echo ".....Enter the Limit..... ? "
read n
a=-1
b=1
echo "..........$n Fibnocci Numbers.........."
while [ $n -gt 0 ]
do
c=`expr $a + $b`
echo "$c"
a=$b
b=$c
n=`expr $n - 1`
done
echo "......................................"
# (14) Check whether a given Number is Prime or Not
echo ".....Enter Number to Check Prime..... ? "
read num
hnum=`expr $num / 2`
i=2
flag=0
while [ $i -le $hnum ]
do
temp=0
temp=`expr $num % $i`
if [ $temp = 0 ]
then
flag=1
fi
i=`expr $i + 1`
done
echo ""
if [ $flag = 1 ]
then
echo ".....$num is NOT a PRIME Number....."
else
echo ".....$num is a PRIME Number....."
fi

# (15) Print Multiplication Table of 'n'


echo ".....Enter a Number..... ? "
read num
i=1
echo ".....Multiplication Table of $num....."
while [ $i -le 10 ]
do
res=`expr $num \* $i`
echo "$i * $num = $res"
i=`expr $i + 1`
done
echo "....................................."

# (16) Print Factorial of 1 to 20 (nested 'while-do' loop)


echo "...............Factorial of 1 to 20..............."
i=0
f=1
while [ $i -le 20 ]
do
count=1
f=1
while [ $count -le $i ]
do
f=`expr $f \* $count`
count=`expr $count + 1`
done
echo ".....Factorial of $i =..... $f"
i=`expr $i + 1`
done

You might also like