0% found this document useful (0 votes)
58 views25 pages

Shell Programs

The document contains code snippets and output for various shell script programs that perform tasks like calculating the area of shapes, swapping variables, calculating gross pay, checking if a year is a leap year, performing arithmetic operations, generating Fibonacci series, checking if a number is a palindrome, checking if a number is prime, calculating powers, swapping without a third variable, checking if a number is an Armstrong number, and reversing a number. The programs demonstrate basic programming concepts like user input, conditional statements, loops, arithmetic and string operations in shell scripting.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views25 pages

Shell Programs

The document contains code snippets and output for various shell script programs that perform tasks like calculating the area of shapes, swapping variables, calculating gross pay, checking if a year is a leap year, performing arithmetic operations, generating Fibonacci series, checking if a number is a palindrome, checking if a number is prime, calculating powers, swapping without a third variable, checking if a number is an Armstrong number, and reversing a number. The programs demonstrate basic programming concepts like user input, conditional statements, loops, arithmetic and string operations in shell scripting.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Area of Different Shapes

Coding :
echo "enter the base "
read b
echo "enter the height
read h
echo "enter the radius "
read r
rarea=`expr $h \* $b`
phi=`expr 22 /7`
d=`expr $r \* $r`
carea=`expr $phi \* $d`
echo "the area of rectangle: $rarea"
echo "the area of circle : $carea"

Output:

[student40@fosslab ~]$ sh high.sh


enter the base
2
enter the height
4
enter the radius
3
the area of rectangle: 8
the area of circle : 27

Swapping using Third Variable


Coding:
echo "swap the value of a and b"
read a b
temp=$a
a=$b
b=$temp
echo "$a $b"

Output:
[student40@fosslab shell]$ sh swap.sh
enter swap the value of a and b
24
42

Calculation Of Gross Pay


Coding:
echo "enter the basic"
read basic
hra=`expr $basic \* 2 / 100`
da=`expr $basic \* 5 / 100`
ta=`expr $basic \* 3 / 100`
gross=`expr $basic \+ $hra \+ $da \+ $ta`
echo "the gross pay is : $gross"

Output:

[student40@fosslab shell]$ sh gross.sh


enter the basic
400
the gross pay is : 440

Check the Year is Leap Year (or) Not

Coding:
echo "enter the year"
read y
if [ ` expr $y % 4 ` -eq 0 ]
then
echo "the year is a large year"
else
echo "the year is not a leap year"
fi

Output:

[student40@fosslab shell]$ sh year.sh


enter the year
1995
the year is not a leap year
enter the year
2012
The year is leap year

Arithmetic operation
Coding:
echo "enter two number"
read a b
echo "what do you want to do?(1to5)"
echo "1) sum"
echo "2) difference"
echo "3) product"
echo "4) quotient"
echo "5)remainder"
echo "enter your choice"
read n
case "$n" in
1) echo "the sum of $a and $b is`expr $a + $b`";;
2)echo "the differnce between $a and $b is`expr $a - $b`";;
3)echo "the product of the $a and $b is`expr $a \* $b`";;
4)echo "the qoutient of $a by $b is`expr $a / $b`";;
5)echo "the remainder of $a by $b is `expr $a % $b`";;
esac

Output:
[student40@fosslab shell]$ sh maths.sh
enter two number
23
what do you want to do?(1to5)
1) sum
2) difference
3) product
4) quotient
5)remainder
enter your choice
1
the sum of 2 and 3 is5
[student40@fosslab shell]$ sh maths.sh
enter two number
23
what do you want to do?(1to5)
1) sum
2) difference
3) product
4) quotient
5)remainder
enter your choice
2
the differnce between 2 and 3 is-1
[student40@fosslab shell]$ sh maths.sh
enter two number
34
what do you want to do?(1to5)
1) sum
2) difference
3) product
4) quotient

5)remainder
enter your choice
3
The product between 3 and 4 is 12

Fibonacci series
Coding:

echo "enter the limit"


read n
echo "the fibonacci series is"
b=0
c=1
i=0
if [ $n -ge 2 ]
then
echo "$b"
echo "$c"
n=`expr $n - 2`
while [ $i -lt $n ]
do
a=`expr $b + $c`
b=$c
c=$a
echo "$c"
i=`expr $i + 1`
done
fi

Output:
[student40@fosslab shell]$ sh fibonacci.sh
enter the limit

8
the fibonacci series is
0
1
1
2
3
5
8
13

Palindrome or Not
Coding:
echo "palindrome"
echo "enter the number"
read x
n=$x
b=0
while [ $x -ne 0 ]
do
a=`expr $x % 10`
b=`expr $b \* 10 + $a`
x=`expr $x / 10`
done
if [ $n -eq $b ]
then
echo "$n is a palindrome"
else
echo "$n is not palindrome"
fi

Output:

[student40@fosslab shell]$ sh palindrome.sh


palindrome
enter the number
7
7 is a palindrome
[student40@fosslab shell]$ sh palindrome.sh
palindrome
enter the number
13
13 is not palindrome

Prime number or Not

Coding:
echo "enter the number"
read n
t=0
i=`expr $n - 1`
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 -eq 0 ]
then
echo "it is a prime number"
else
echo "it is not a prime number"
fi

Output:
[student40@fosslab shell]$ sh prime.sh

enter the number


3
it is a prime number
[student40@fosslab shell]$ sh prime.sh
enter the number
4
it is not a prime number

Calculate the Value of X^n

Coding:
echo "enter x"
read x
echo "enter n"
read n
sum=1
i=1
while [ $i -le $n ]
do
sum=`expr $sum \* $x`
i=`expr $i + 1`
done
echo "the value of x^ n is $sum"

Output:
[student40@fosslab shell]$ sh value.sh
enter x
4
enter n
3
the value of x^ n is 64.

Swapping without using third variable

coding:
echo "enter two numbers"
read a b
a=`expr $a + $b`
b=`expr $a - $b`
a=`expr $a - $b`
echo $a $b

Output:
[student40@fosslab shell]$ sh arithematic.sh
enter two numbers
12 10
10 12

Armstrong Or Not
Coding:
echo "enter the number"
read n
sum=0
x=$n
while [ $n -gt 0 ]
do
y=`expr $n % 10`
z=`expr $y \* $y \* $y`
sum=`expr $sum \+ $z`
n=`expr $n / 10`
done
if [ $x -eq $sum ]
then
echo "it is a armstrong number"
else
echo "it is not a armstrong number "
fi

Output:
[student40@fosslab shell]$ sh armstrong.sh
enter the number
4
it is not a armstrong number
[student40@fosslab shell]$ sh armstrong.sh
enter the number
1
it is a armstrong number

Reverse of a Number
Coding:
echo "enter the number"
read n
r=0
while [ $n -gt 0 ]
do
a=`expr $n % 10`
r=`expr $r \* 10 \+ $a`
n=`expr $n / 10`
done
echo "the reverse is $r"

Output:
[student40@fosslab shell]$ sh reverse.sh
enter the number
23
the reverse is 32

You might also like