0% found this document useful (0 votes)
5 views15 pages

She'll Programs

The document provides examples of shell programming using bash shell. It includes programs to calculate area and circumference of a circle, find roots of a quadratic equation, count vowels in a word, check if a year is a leap year, calculate grades based on marks, find the greatest of three numbers, use case statements, programs to find sum of odd and even numbers, check for palindrome, check for Armstrong number, find factorial of a number, generate Fibonacci series, check for prime numbers, find smallest among given numbers, and find smallest digit in a number.
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)
5 views15 pages

She'll Programs

The document provides examples of shell programming using bash shell. It includes programs to calculate area and circumference of a circle, find roots of a quadratic equation, count vowels in a word, check if a year is a leap year, calculate grades based on marks, find the greatest of three numbers, use case statements, programs to find sum of odd and even numbers, check for palindrome, check for Armstrong number, find factorial of a number, generate Fibonacci series, check for prime numbers, find smallest among given numbers, and find smallest digit in a number.
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/ 15

SHELL PROGRAM – SIMPLE PROGRAMS

Ex. No: 4a AREA & CIRCUMFERENCE OF A CIRCLE

SHELL PROGRAM: $vi area.sh


echo "Enter the Number"

read r

area=$(echo “scale=2;3.14*($r*$r)”|bc)

d=$(echo “scale=2;2*$r”|bc)

c=$(echo “scale=2;3.14*$d”|bc)

echo “The area of the circle is $area”

echo “The circumference of the circle is $c”

OUTPUT:
$ sh area.sh
Enter the Number
5
The area of the circle is 78.5
The circumference of the circle is 31.4
Ex. No: 4b ROOTS OF QUADRATIC EQUATION

SHELL PROGRAM:

$vi roots.sh

echo "Enter a value: "


read a
echo "Enter b value: "
read b
echo "Enter c value: "
read c
x1= echo "scale=2; (-$b+sqrt($b^2-4*$a*$c))/(2*$a)"|bc
x2= echo "scale=2; (-$b-sqrt($b^2-4*$a*$c))/(2*$a)"|bc
echo $x1
echo $x2

OUTPUT:

[cs12001@localhost ~]$ sh roots.sh


Enter a value:
2
Enter b value:
5
Enter c value:
1
-.22
-2.28
Ex. No: 4c COUNT THE NUMBER OF VOWELS IN A WORD

SHELL PROGRAM: $vi vowels.sh

echo Enter the word

read word

vowels=$(echo $word | sed 's/[^aeiou]//g')

echo "${#vowels} vowels"

OUTPUT:

[cs12001@localhost ~]$ sh vowels.sh

Enter the word

computer

3 vowels
SHELL PROGRAM – CONDITIONAL STATEMENTS

Ex. No: 5a LEAP YEAR OR NOT

SHELL PROGRAM: $vi leap.sh

echo "Enter a year"

read i

if [ ‘expr $i % 4’ –eq 0 ]

then

echo “ $i is a leap year”

else

echo “ $i is not a leap year”

OUTPUT:

$ sh leap.sh

Enter a year

1999

1999 is not a leap year

$ sh leap.sh

Enter a year

2016

2000 is a leap year


Ex. No: 5b DISPLAY GRADES

SHELL PROGRAM: $ vi grade.sh

sum=0
echo “Enter the five subject marks for the student”
read m1 m2 m3 m4 m5
sum1=`expr $m1 + $m2 + $m3 +$m4 + $m5`
echo “ sum of 5 subjects are: $sum1”
per=`expr $sum1 / 5`
echo “percentage: “ $per
if [ $per –ge 60 ]
then
echo “ you got distinction”
elif [ $per –ge 50 ]
then
echo “ you got second class”
else
echo “ you got fail”
fi
OUTPUT:
$ sh grade.sh
Enter the five subject marks for the student
85 82 81 83 84
sum of 5 subjects are: 415
percentage:83
you got distinction
Ex. No: 5c GREATEST OF THREE NUMBERS

SHELL PROGRAM: $vi big.sh

echo "Enter Three Numbers"


read a b c
if [ $a -gt $b -a $a -gt $c ]
then
echo "$a is Greater"
elif [ $b -gt $c ]
then
echo "$b is Greater"
else
echo "$c is Greater"
fi

OUTPUT:
$ sh big.sh
Enter Three Numbers
586
8 is Greater
Ex. No. 5d CASE STATEMENTS

SHELL PROGRAM: $ vi case.sh

echo "Type one of the following:"


echo " 1 - who am I?"
echo " 2 - who is logged on?"
echo " 3 - date"
echo " 4 - calendar"
read n
case $n in
1) whoami ;;
2) who ;;
3) date ;;
4) cal ;;
esac

OUTPUT:
$sh case.sh
Type one of the following:
1 - who am I?
2 - who is logged on?
3 - date
4 - calendar
3
Thu Mar 14 16:58:23 IST 2016
SHELL PROGRAM – LOOPING STATEMENTS

Ex. No.6a SUM OF ODD AND EVEN NUMBERS

SHELL PROGRAM: $ vi su.sh

odd=0
i=0
even=0
echo "Enter number of elements"
read num
while [ $i -ne $num ]
do
echo "Enter number"
read n
if [ `expr $n % 2` -ne 0 ]
then
odd=`expr $odd + $n`
else
even=`expr $even + $n`
fi
i=`expr $i + 1`
done
echo "Sum of odd numbers $odd"
echo "Sum of even numbers $even"
OUTPUT:
$ sh su.sh
Enter number of elements
4
Enter number
10
Enter number
11
Enter number
12
Enter number
13
Sum of odd numbers 24
Sum of even numbers 22
Ex. No: 6b PALINDROME

SHELL PROGRAM:
$ vi pali.sh

echo "Enter the string"


read a
b=`expr $a | wc -c`
b=`expr $b - 1`
while [ $b -gt 0 ]
do
e=`expr $a | cut -c $b`
d=$d$e
b=`expr $b - 1`
done
if test $a = $d
then
echo "It is a palindrome"
else
echo "It is not a palindrome"
fi

OUTPUT:

$ sh pali.sh
Enter the string
malayalam
It is a palindrome
$ sh pali.sh
Enter the string
unix
It is not a palindrome
Ex.No:6c ARMSTRONG OR NOT

SHELL PROGRAM:
$ vi arm.sh
echo "Enter a Number"
read num
x=$num
sum=0
while [ $num -gt 0 ]
do
y=`expr $num % 10`
z=`expr $y \* $y \* $y`
sum=`expr $sum + $z`
num=`expr $num / 10`
done
if [ $x -eq $sum ]
then
echo "$x is an armstrong Number"
else
echo "$x is not an armstrong Number"
fi
OUTPUT:
$ sh arm.sh
Enter a Number
153
153 is an armstrong Number
$ sh arm.sh
Enter a Number
113
113 is not an armstrong Number
Ex.No:6d FACTORIAL OF A NUMBER

SHELL PROGRAM:
$vi fact.sh
echo "Enter a Number"
read n
i=`expr $n - 1`
p=1
while [ $i -ge 1 ]
do
n=`expr $n \* $i`
i=`expr $i - 1`
done
echo "The Factorial of the given Number is $n"

OUTPUT:
[cs11012@localhost~]$ sh fact.sh
Enter a Number
5
The Factorial of the given Number is 120
Ex.No.6e FIBONACCI SERIES

SHELL PROGRAM:
$ vi fibonnaci.sh
echo "Enter the Limit"
read n
i=2
echo "Fibonacci Series"
echo "----------------"
a=0
b=1
echo $a
echo $b
while [ $i -lt $n ]
do
c=`expr $a + $b`
echo $c
a=$b
b=$c
i=`expr $i + 1`
done
OUTPUT:
[me11012@localhost~]$ sh fibonnaci.sh
Enter the Limit
3
Fibonacci Series
----------------------
0
1
1
2
Ex.No.6f CHECKING FOR PRIME NUMBER
SHELL PROGRAM:
$ vi prime.sh
echo "Enter a Number"
read n
i=`expr $n - 1`
t=0
while [ $i -ge 2 ]
do
p=`expr $n % $i`
if [ $p -eq 0 ]
then
t=`expr $t + 1`
fi
i=`expr $i - 1`
done
if [ $t -gt 0 ]
then
echo "The Number $n is not a Prime Number"
else
echo "The Number $n is a Prime Number"
fi
OUTPUT:
$ sh prime.sh
Enter a Number
2
The Number 2 is a Prime Number
$ sh prime.sh
Enter a Number
9
The Number 9 is not a Prime Number"
Ex.No.6g SMALLEST AMONG ‘N’ NUMBERS

SHELL PROGRAM: $vi small.sh

echo Enter the value of N

read n

i=0

temp=100

echo Enter numbers less than 100

while [ $i -lt $n ]

do

read a

if [ $temp -gt $a ]

then

temp=$a

fi

i=`expr $i + 1`

done

echo The Smallest number is $temp

OUTPUT:

[cs12001@localhost ~]$ sh small.sh

Enter the value of N


4
Enter numbers less than 100
45
12
75
2
The Smallest number is 2
Ex.No.6h SMALLEST DIGIT IN A NUMBER

SHELL PROGRAM:

$vi smdig,sh

echo "Enter the number"

read n

min=9

t=0

while [ $n -ne $t ]

do

rem=`expr $n % 10`

n=`expr $n / 10`

if [ $rem -lt $min ]

then

min=$rem

fi

done

echo " The Smallest digit is $min"

OUTPUT:

$ sh smdig.sh

Enter the number

256

The Smallest digit is 2

You might also like