You are on page 1of 62

90

SHELL PROGRAMS

EXPERIMENT 1

AIM : To swap two numbers with out using temporary variable

REQUIREMENTS :

HARDWARE : PIII Processor , 128 MB RAM, 10GB


SOFTWARE : OS: LINUX

THEORY :

Shell is a user interface to the operating system built on the programming interface.
The shell executes commands of the form:

CommandName [ argument1]

The shell executes the program CommandName and passes argument on the command line to
it. Then the shell waits for the command to exit.

This program is for swapping two numbers using only two variables

ALGORITHM :

STEP 1. Read two numbers a and b


STEP 2. a=`expr $a + $b`
STEP 3. b=`expr $a -$b`
STEP 4. c=`expr $a-$b`
STEP 5. Echo the swapped values as $a and $b

DEPARTMENT OF COMPUTER SCIENCE


91

PROGRAM:

echo -n "enter two numbers"


read a
read b
echo "Before swapping"
echo " A = $a B = $b"
a=`expr $a + $b`
b=`expr $a - $b`
a=`expr $a - $b`
echo "After swapping "
echo "A = $a B = $b"

OUTPUT :

[balu@krishna-edu shell]$ sh swap.sh


enter two numbers4
5
Before swapping
A=4 B=5
After swapping
A=5B=4

DEPARTMENT OF COMPUTER SCIENCE


92

EXPERIMENT 2

AIM : To Swap two numbers using temporary variable

REQUIREMENTS :

HARDWARE : PIII Processor , 128 MB RAM, 10GB


SOFTWARE : OS: LINUX

THEORY :

Shell is a user interface to the operating system built on the programming interface.
The shell executes commands of the form:

CommandName [ argument1]

The shell executes the program CommandName and passes argument on the command line
to it. Then the shell waits fro the command to exit.
This program is for swapping two numbers using three variables

ALGORITHM:

STEP 1. Read two numbers a and b


STEP 2. Echo the numbers a and b
STEP 3. c=$a
STEP 4. a=$b
STEP 5. b=$c
STEP 6. Echo a and b after swapping

DEPARTMENT OF COMPUTER SCIENCE


93

PROGRAM

echo -n "enter two numbers"


read a
read b
echo "Before swapping"
echo " A = $a B = $b"
c=$a
a=$b
b=$c
echo "After swapping "
echo "A = $a B = $b"

OUTPUT :

enter two numbers4


5
Before swapping
A=4 B=5
After swapping
A=5B=4

DEPARTMENT OF COMPUTER SCIENCE


94

EXPERIMENT 3

AIM : To read a number from the user and to calculate square and cube of the number.

REQUIREMENTS :

HARDWARE : PIII Processor , 128 MB RAM, 10GB


SOFTWARE : OS: LINUX

THEORY :

Shell is a user interface to the operating system built on the programming interface.
The shell executes commands of the form:

CommandName [ argument1]

The shell executes the program CommandName and passes argument on the command line to
it. Then the shell waits fro the command to exit.

This program is for reading a number from the user and to calculate square and cube of the
number.

ALGORITHM :

STEP 1. Read the number n


STEP 2. Calculate the values of square and cube as
sqr=`expr $num \* $num
cube=`expr $sqr \*$num
SETP 3. Echo the value of square and cube

DEPARTMENT OF COMPUTER SCIENCE


95

PROGRAM:

echo -n "enter a number"


read num
sqr=`expr $num \* $num`
cube=`expr $sqr \* $num`
echo "the squre of $num is $sqr"
echo "the cube of $num is $cube"

OUTPUT:

[balu@krishna-edu shell]$ sh sqcu.sh


enter a number 4
the squre of 4 is 16
the cube of 4 is 64
[balu@krishna-edu shell]$

DEPARTMENT OF COMPUTER SCIENCE


96

EXPERIMENT 4

AIM : To check whether a given number is even or odd .

REQUIREMENTS :

HARDWARE : PIII Processor , 128 MB RAM, 10GB


SOFTWARE : OS: LINUX

THEORY:

Shell is a user interface to the operating system built on the programming interface.
The shell executes commands of the form:

CommandName [ argument]

The shell executes the program CommandName and passes argument on the command line to
it. Then the shell waits fro the command to exit.

This program is to check whether a given number is even or odd .

ALGORITHM :

STEP 1. Read the number n


STEP 2. Perform modulo division on n and store it in r
STEP 3. If ‘r’ is equal to zero then
Echo the given number is even
STEP 4. If the condition is not satisfied then
Echo the output is an odd number

DEPARTMENT OF COMPUTER SCIENCE


97

PROGRAM:
clear
echo enter the number
read n
r=1
r=`expr $n % 2`
if [ $r -eq 0 ]
then
echo even
else
echo odd
fi

OUTPUT:

[krishna-edu-balu]$ sh eorodd
enter the number
5
odd
[krishna-edu balu]$

DEPARTMENT OF COMPUTER SCIENCE


98

EXPERIMENT 5

AIM : To find whether the given input name is a file or a directory.

REQUIREMENTS:

HARDWARE : PIII Processor , 128 MB RAM, 10GB


SOFTWARE : OS: LINUX

THEORY:

Shell is a user interface to the operating system built on the programming interface. The shell
executes commands of the form:

CommandName [argument1,…..]
The shell executes the program CommandName and passes argument on the
command line to it. Then the shell waits for the command to exit. Files are the passive
containers of data on disk. Many operating systems name hardware devices in the same way
as files and allow access to devices using the same interface as to files: open, read, write,
close.

This program checks whether the given name is file or directory

ALGORITHM:

STEP 1. Read a file name.


STEP 2. Check for the type of file using –f option.
STEP 3. If condition is satisfied then
Echo file as an ordinary file
Else
Echo file as a directory

DEPARTMENT OF COMPUTER SCIENCE


99

PROGRAM
clear
echo enter a filename
read $fname
if test –f $fname
then
echo file is a ordinary file
else
echo directory
fi

OUTPUT:
[krishna-edu balu]$ sh fd
enter a filename
string
file is a ordinary file
[krishna-edu balu]$

DEPARTMENT OF COMPUTER SCIENCE


100

EXPERIMENT 6

AIM: To check the type of character entered.

REQUIREMENTS:

HARDWARE : PIII Processor , 128 MB RAM, 10GB


SOFTWARE : OS: LINUX

THEORY:
Shell is a user interface to the operating system built on the programming interface. The shell
executes commands of the form:

CommandName [argument1,…..]
The shell executes the program CommandName and passes argument on the
command line to it. Then the shell waits for the command to exit.
This program is to check the type of input character using case statement.

ALGORITHM:

STEP 1. Read a character from the keyboard.


STEP 2. Use case statement to check for the type of character
STEP 3. Echo the character as alphabet or number or special character
STEP 4. End case

DEPARTMENT OF COMPUTER SCIENCE


101

PROGRAM:
clear
echo enter a character from the keyboard
read ch
case $ch in

[a-z]) echo its a alphabet


;;
[0-9]) echo its a number
;;
?) echo it is a special character
;;
*) echo u entered more
;;
esac

OUTPUT:

[krishna-edu balu]$ sh case1


enter a character from the keyboard
a
its a character
[krishna-edu balu]$

DEPARTMENT OF COMPUTER SCIENCE


102

EXPERIMENT 7

AIM: To illustrate the usage of case statement.

REQUIREMENTS:

HARDWARE : PIII Processor , 128 MB RAM, 10GB


SOFTWARE : OS: LINUX

THEORY:

Shell is a user interface to the operating system built on the programming interface. The shell
executes commands of the form:
CommandName [argument1,…..]

The shell executes the program CommandName and passes argument on the
command line to it. Then the shell waits for the command to exit.

This program is an example for using case statement.

ALGORITHM:

STEP 1: Read the choice


STEP 2: If choice is 1 then display the file
STEP 3: If choice is 2 then copy one file to another.
STEP 4: If choice is 3 then create a directory.
STEP 5: End case

DEPARTMENT OF COMPUTER SCIENCE


103

PROGRAM

clear
echo 1.display a file
echo 2.copy a file
echo 3.create a directory
echo enter ur choice
read ch
case $ch in
1) echo enter filename
read fname
cat $fname
;;
2) echo enter two files
read file1 file2
cp $file1 $file2
;;
3) echo enter the directory
read dname
mkdir $dname
;;
*) echo “ invalid option “
esac

OUTPUT:

[krishna-edu balu] sh case


1.print a file
2.copy a file
3.create a directory
enter ur choice
2
enter two files
c2 ch
[krishna-edu balu]$ vi ch
This is unix environment.

DEPARTMENT OF COMPUTER SCIENCE


104

EXPERIMENT 8

AIM: To compare two strings.

REQUIREMENTS:

HARDWARE : PIII Processor , 128 MB RAM, 10GB


SOFTWARE : OS: LINUX

THEORY:

Shell is a user interface to the operating system built on the programming interface. The shell
executes commands of the form:

CommandName [argument1,…..]

The shell executes the program CommandName and passes argument on the
command line to it(via the argv array of strings). Then the shell waits for the command to
exit.

This program is to compare two strings

ALGORITHM:

STEP 1:Read string1.


STEP 2:Read string2.
STEP 3:compare the two strings.
STEP 4:If string is equal to string2 then
Echo strings are equal
Echo strings are not equal

DEPARTMENT OF COMPUTER SCIENCE


105

PROGRAM

clear
echo "enter a string1"
read first
echo "enter a string2"
read second
if [ $first = $second ]
then
echo equal
else
echo unequal
fi

OUTPUT:

[krishna-edu balu]$ sh string


enter a string1
hello
enter a string2
krishna
unequal
[krishna-edu balu]$

DEPARTMENT OF COMPUTER SCIENCE


106

EXPERIMENT 9
AIM : To compare two strings by reading strings from the command line .

REQUIREMENTS :

HARDWARE : PIII Processor , 128 MB RAM, 10GB


SOFTWARE : OS: LINUX

THEORY:

Shell is a user interface to the operating system built on the programming interface.
The shell executes commands of the form:

CommandName [ argument]

The shell executes the program CommandName and passes argument on the command line
to it. Then the shell waits fro the command to exit

This program is for comparing two strings by reading strings from the commandline

ALGORITHM:

STEP 1. Read the value of first string


STEP 2. Read the value of second string
STEP 3. If string1 is equal to string2
Echo strings are equal
STEP 4. Else echo strings as unequal

DEPARTMENT OF COMPUTER SCIENCE


107

PROGRAM:

if [ $1 = $2 ]
then
echo "Strings are equal ...."
else
echo "Strings are not equal....."
fi

[balu@krishna-edu shell]$ sh comparecmd.sh krishna krishna


Strings are equal ....
[balu@krishna-edu shell]$

DEPARTMENT OF COMPUTER SCIENCE


108

EXPERIMENT 10

AIM : To find the length of a string

REQUIREMENTS :

HARDWARE : PIII Processor , 128 MB RAM, 10GB


SOFTWARE : OS: LINUX

THEORY:

Shell is a user interface to the operating system built on the programming interface.
The shell executes commands of the form:

CommandName [ argument]

The shell executes the program CommandName and passes argument on the command line
to it. Then the shell waits fro the command to exit.

This program is for finding the length of the string .

ALGORITHM:

STEP 1. Read the value of the string


STEP 2. Count the number of characters in the string using the wc –c command and store
the result in the variable length
STEP 3. Subtract the new line character
STEP 4. Echo the value of the length of the string

DEPARTMENT OF COMPUTER SCIENCE


109

PROGRAM

echo -n "Enter the string "


read str
len=`echo $str | wc -c`
len=`expr $len - 1`
echo " the length of the string is $len"

OUTPUT

[balu@krishna-edu shell]$ sh length.sh


Enter the string krishna
the length of the string is 6
[balu@krishna-edu shell]$

DEPARTMENT OF COMPUTER SCIENCE


110

EXPERIMENT 11
AIM : To calculate the value of X n
REQUIREMENTS :

HARDWARE : PIII Processor , 128 MB RAM, 10GB


SOFTWARE : OS: LINUX

THEORY:

Shell is a user interface to the operating system built on the programming interface.
The shell executes commands of the form:

CommandName [ argument]

The shell executes the program CommandName and passes argument on the command line
to it. Then the shell waits fro the command to exit.

This program is for finding the value of Xn

ALGORITHM:
STEP 1. Read the values of x and n
STEP 2. Initialize sum and I to 1
STEP 3. Check for the condition [ $I -le $n ]
STEP 4.Repeat the following steps until the above condition becomes false
Sum=`expr $sum \* $x`
i=`expr $i + 1
STEP 5. Echo the value of sum as the value of x power n

DEPARTMENT OF COMPUTER SCIENCE


111

PROGRAM:

echo -n "enter ther value of x "


read x
echo -n "enter the value of 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 power $n is $sum"

OUTPUT:

[balu@krishna-edu shell]$ sh power.sh


enter ther value of x 5
enter the value of n:2
the value of 5 power 2 is 25
[balu@krishna-edu shell]$

DEPARTMENT OF COMPUTER SCIENCE


112

EXPERIMENT 12

AIM : To read 10 numbers from the user and to find the sum and
average of the numbers .

REQUIREMENTS :

HARDWARE : PIII Processor , 128 MB RAM, 10GB


SOFTWARE : OS: LINUX

THEORY:

Shell is a user interface to the operating system built on the programming interface.
The shell executes commands of the form:

CommandName [ argument]

The shell executes the program CommandName and passes argument on the command line to
it.

This is the program for reading 10 numbers from the user and finding the sum and average of
the numbers

ALGORITHM:

STEP 1. Initialize the values n to 1 and sum to 0


STEP 2. Repeat the following steps until the condition [ $n -le 10 ] becomes false
Read the number
Sum=`expr $sum + $num`
Num=0
N=`expr $n + 1`
STEP 3. Calculate the average
avg=`expr $sum / 10`
STEP 4. Echo the average of 10 numbers

DEPARTMENT OF COMPUTER SCIENCE


113

PROGRAM:
n=1
sum=0
while [ $n -le 10 ]
do
echo -n "enter number $n"
read num
sum=`expr $sum + $num`
num=0;
n=`expr $n + 1`
done
avg=`expr $sum / 10`
echo "ther sum of 10 numbers is $sum"
echo "the average of 10 numbers is $avg"

OUTPUT :

[balu@krishna-edu shell]$ sh sumarg.sh


enter number 1 is : 1
enter number 2 is : 2
enter number 3 is : 3
enter number 4 is : 4
enter number 5 is : 5
enter number 6 is : 6
enter number 7 is : 7
enter number 8 is : 8
enter number 9 is : 9
enter number 10 is : 10
ther sum of 10 numbers is 55
the average of 10 numbers is 5
[balu@krishna-edu shell]$

DEPARTMENT OF COMPUTER SCIENCE


114

EXPERIMENT 13

AIM : To accept a number from the user and display the list of even numbers
below that number

REQUIREMENTS :

HARDWARE : PIII Processor , 128 MB RAM, 10GB


SOFTWARE : OS: LINUX

THEORY:
Shell is a user interface to the operating system built on the programming interface.
The shell executes commands of the form:

CommandName [ argument]

The shell executes the program CommandName and passes argument on the command line to
it.

This is the program for accepting numbers from the user and display the list of even numbers
below that number

ALGORITHM:

STEP 1. Read the limit value


STEP 2. Initialize x to 2
STEP 3. Read the number
STEP 4. Repeat the following steps until the condition x<number becomes false
echo $x
x=`expr $x + 2`

DEPARTMENT OF COMPUTER SCIENCE


115

PROGRAM:
echo -n "enter the limit:"
x=2
read num
while [ $x -lt $num ]
do
echo -n "$x \t"
x=`expr $x + 2`
done

OUTPUT :

[balu@krishna-edu shell]$ sh evenbelow.sh


enter the limit:10
2468
[balu@krishna-edu shell]$

DEPARTMENT OF COMPUTER SCIENCE


116

EXPERIMENT 14

AIM : To find the count and sum of even and odd numbers separately

REQUIREMENTS :

HARDWARE : PIII Processor , 128 MB RAM, 10GB


SOFTWARE : OS: LINUX

THEORY:

Shell is a user interface to the operating system built on the programming interface.
The shell executes commands of the form:

CommandName [ argument]

The shell executes the program CommandName and passes argument on the command line to
it.

This is the program for finding the count and sum of even and odd numbers separately

ALGORITHM

STEP 1. Read the limit n


STEP 2. Initialize the values of i ,j ,sumodd and sumeven as 0
STEP 3. calculate modulo division for each number
STEP 4. If the answer is 0 then add the number to sumeven
else add it to sumodd
STEP 5.Echo the values of i, j ,sumeven and sumodd

DEPARTMENT OF COMPUTER SCIENCE


117

PROGRAM:

echo -n "enter the limit"


read n
echo -n "enter the number"
i=0
j=0
k=0
sumodd=0
sumeven=0
while [ $i -lt $n ]
do
read num
if [ `expr $num % 2` -eq 0 ]
then
sumodd=`expr $sumodd + $num`
k=`expr $k + 1`
else
sumeven=`expr $sumeven + $num`
j=`expr $j + 1`
fi

i=`expr $i + 1`

OUTPUT :

[balu@krishna-edu shell]$ sh oddeven.sh


enter the limit5
enter the number1
2
3
4
5
total even number is 2
total odd numbers is 3
the sum of even numbers is 9
the sumof odd number is 6
[balu@krishna-edu shell]$

DEPARTMENT OF COMPUTER SCIENCE


118

EXPERIMENT 15

AIM : To count the number of digits in an integer

REQUIREMENTS :

HARDWARE : PIII Processor , 128 MB RAM, 10GB


SOFTWARE : OS: LINUX

THEORY:

Shell is a user interface to the operating system built on the programming interface.
The shell executes commands of the form:
CommandName [ argument]

The shell executes the program CommandName and passes argument on the command line to
it.

This is the program for counting the number of digits in an integer

ALGORITHM:

STEP 1.Read the number


STEP 2.Initialize digit to 0
STEP 3.Repeat the following steps until the condition num > 0 becomes false
a=`expr $num % 10`
digit=`expr $digit + 1`
num=`expr $num / 10`
STEP 4.Echo the number of digits in the integer

DEPARTMENT OF COMPUTER SCIENCE


119

PROGRAM:
echo -n "Enter an interge:"
read num
digit=0
while [ $num -gt 0 ]
do
a=`expr $num % 10`
digit=`expr $digit + 1`
num=`expr $num / 10`
done
echo "the number of digits in the integer is $digit"

OUTPUT:
[balu@krishna-edu shell]$ sh count.sh
Enter an interge:12345
the number of digits in the integer is 5
[balu@krishna-edu shell]$

DEPARTMENT OF COMPUTER SCIENCE


120

EXPERIMENT 16

AIM : To find the sum of digits in an integer

REQUIREMENTS :

HARDWARE : PIII Processor , 128 MB RAM, 10GB


SOFTWARE : OS: LINUX

THEORY:
Shell is a user interface to the operating system built on the programming interface.
The shell executes commands of the form:
CommandName [ argument]

The shell executes the program CommandName and passes argument on the command line to
it.

This is the program for finding the sum of digits in an integer.

ALGORITHM:

STEP 1.Read the number


STEP 2.Initialize sum to 0
STEP 3.Repeat the following steps until the condition num > 0 becomes false
a=`expr $num % 10`
sum=`expr $sum + a`
num=`expr $num / 10`
STEP 4.Echo the sum of digits in the integer

DEPARTMENT OF COMPUTER SCIENCE


121

PROGRAM:

echo -n "enter an integer"


read num
sum=0
while [ $num -gt 0 ]
do
a=`expr $num % 10`
sum=`expr $sum + $a`
num=`expr $num / 10`
done
echo "the sum of digits in the integer is $sum"

OUTPUT :

[balu@krishna-edu shell]$ sh sumdigit.sh


enter an integer12345
the sum of digits in the integer is 15
[balu@krishna-edu shell]$

DEPARTMENT OF COMPUTER SCIENCE


122

EXPERIMENT 17

AIM : To print the multiplication table of a given number .

REQUIREMENTS :

HARDWARE : PIII Processor , 128 MB RAM, 10GB


SOFTWARE : OS: LINUX

THEORY:

Shell is a user interface to the operating system built on the programming interface.
The shell executes commands of the form:

CommandName [ argument]

The shell executes the program CommandName and passes argument on the command line to
it.

This is the program for printing the multiplication table of the given number

ALOGRITHM :

STEP 1.Read the values of table number and limit as x and n


STEP 2.Initialize the value of I to 1
STEP 3.Repeat the following statements until the condition [ $I –le $n ] becomes false
Sum=`expr $x \* $I`
Echo “$x x $I = $sum”
Increment the value of I

DEPARTMENT OF COMPUTER SCIENCE


123

PROGRAM:

echo -n "Enter the table number"


read x
echo -n "Enter the limit"
read n
echo
echo "The multiplication table for $x is "
echo
i=1
while [ $i -le $n ]
do
sum=`expr $x \* $i`
echo "$x x $i = $sum "
i=`expr $i + 1`
done

OUTPUT :

[balu@krishna-edu shell]$ sh multiply.sh


Enter the table number5
Enter the limit10
The multiplication table for 5 is
5 x1=5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
[balu@krishna-edu shell]$

DEPARTMENT OF COMPUTER SCIENCE


124

EXPERIMENT 18

AIM : To print all the divisors of a given integer.

REQUIREMENTS :

HARDWARE : PIII Processor , 128 MB RAM, 10GB


SOFTWARE : OS: LINUX

THEORY:
Shell is a user interface to the operating system built on the programming interface.
The shell executes commands of the form:
CommandName [ argument]

The shell executes the program CommandName and passes argument on the command line to
it.

This program for printing all the divisors of a given integer.

ALGORITHM:

STEP 1. Read the number


STEP 2. Initialize the value of I to 1
STEP 3.Repeat the following statements until the condition I<num becomes false
Temp=`expr $num % $I`
If temp is equal to 0
Then echo I
Increment the value of I

DEPARTMENT OF COMPUTER SCIENCE


125

PROGRAM :

echo -n "Enter the number"


read num
echo "The divisors are"
i=1
while [ $i -le $num ]
do
temp=`expr $num % $i`
if [ $temp -eq 0 ]
then
echo -n " $i "
fi
i=`expr $i + 1`
done

OUTPUT :

[balu@krishna-edu shell]$ sh divisor.sh


Enter the number5
The divisors are
1 5 [balu@krishna-edu shell]$

DEPARTMENT OF COMPUTER SCIENCE


126

EXPERIMENT 19

AIM : To find the factorial of a give number.

REQUIREMENTS:

HARDWARE: P-IV Processor, 128 MB RAM, 40 GB


SOFTWARE: OS: LINUX

THEORY:
Shell is a user interface to the operating system built on the programming interface. The shell
executes commands of the form:

CommandName [argument1,…..]

The shell executes the program CommandName and passes argument on the
command line to it. Then the shell waits for the command to exit.

This program is to find the factorial of a given number.

ALGORITHM:

STEP 1:Read the number


STEP 2:Initialize res to 1 and i to 1
STEP 3:Repeat the following steps until the condition i<n becomes false
res=`expr $res \* $i`
i=`expr $i + 1`
STEP 4:Echo the value of Factorial

DEPARTMENT OF COMPUTER SCIENCE


127

PROGRAM

clear
echo enter the number
read n
res=1
i=1
while [ $i -le $n ]
do
res=`expr $res \* $i`
i=`expr $i + 1`
done
echo factorial=$res

OUTPUT:

[krishna-edu balu]$ sh fact


enter the number
6
factorial=720
[krishna-edu balu]$

DEPARTMENT OF COMPUTER SCIENCE


128

EXPERIMENT 20

AIM: To generate fibonacci series.

REQUIREMENTS :

HARDWARE : PIII Processor , 128 MB RAM, 10GB


SOFTWARE : OS: LINUX

THEORY :

Shell is a user interface to the operating system built on the programming interface. The shell
executes commands of the form:

CommandName [argument1,…..]

The shell executes the program CommandName and passes argument on the command line to
it. Then the shell waits for the command to exit.

This program is to generate fibonacci series upto a given limit.

ALGORITHM:

STEP 1:Enter a number upto which the series has to be generated.


STEP 2:Initialize a=0,b=1,d=2.
STEP 3:Repeat the following steps until the condition d<n becomes false
c=`expr $a + $b`
echo $c
a=$b
b=$c
d=`expr $d + 1`

DEPARTMENT OF COMPUTER SCIENCE


129

PROGRAM:

clear
echo enter the no. of numbers in the series
read n
a=0
b=1
d=2
echo $a
echo $b
while [ $d -lt $n ]
do
c=`expr $a + $b`
echo $c
a=$b
b=$c
d=`expr $d + 1`
done

OUTPUT:

[krishna-edu balu]$ sh fibo


enter the no. of numbers in the series
5
0
1
1
2
3

DEPARTMENT OF COMPUTER SCIENCE


130

EXPERIMENT 21

AIM: To find the reverse of number.

REQUIREMENTS:

HARDWARE : PIII Processor , 128 MB RAM, 10GB


SOFTWARE : OS: LINUX

THEORY:

Shell is a user interface to the operating system built on the programming interface. The shell
executes commands of the form:

CommandName [argument1,…..]

The shell executes the program CommandName and passes argument on the
command line to it. Then the shell waits for the command to exit.

This program is to find the reverse of a number.

ALGORITHM:

STEP 1:Read the number.


STEP 2:Initialize the value of reverse to 0.
STEP 3:Repeat the following steps until the condition n>0 becomes false
dig=`expr $n % 10`
rev=`expr $rev \* 10 + $dig`
n=`expr $n / 10`
STEP 4:Echo Reverse

DEPARTMENT OF COMPUTER SCIENCE


131

PROGRAM :

clear
echo enter a number
read n
rev=0
while [ $n -gt 0 ]
do
dig=`expr $n % 10`
rev=`expr $rev \* 10 + $dig`
n=`expr $n / 10`
done
echo $rev

OUTPUT:

[krishna-edu balu]$ sh rev


enter a number
1234
4321
krishna-edu balu]$

DEPARTMENT OF COMPUTER SCIENCE


132

EXPERIMENT 22

AIM: To find whether a given number is Armstrong number or not .

REQUIREMENTS:

HARDWARE : PIII Processor , 128 MB RAM, 10GB


SOFTWARE : OS: LINUX

THEORY:

Shell is a user interface to the operating system built on the programming interface. The shell
executes commands of the form:

CommandName [argument1,…..]

The shell executes the program CommandName and passes argument on the
command line to it. Then the shell waits for the command to exit. This program is to find the
reverse of a number.

This program is to find whether a given number is Armstrong number or not .

ALGORITHM:

STEP 1.Read a number


STEP 2.Initialize x to num and sum to 0
STEP 3.Repeat the following statements until the condition [ $num -gt 0 ]
becomes false
y=`expr $num %10`
z=`expr $y \* $y \* $y`
sum=`expr $sum + $z`
num=`expr $num / 10`
STEP 4. If x is equal to sum then echo x as armstrong
Else echo x as not Armstrong

DEPARTMENT OF COMPUTER SCIENCE


133

PROGRAM

echo -n "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 $num + $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:

[balu@krishna-edu balu]$ sh amstrong.sh


enter a number :153
153 is armstrong number
[balu@krishna-edu balu]$

DEPARTMENT OF COMPUTER SCIENCE


134

EXPERIMENT 23

AIM: To check whether a give number is prime or not.

REQUIREMENTS:

HARDWARE : PIII Processor , 128 MB RAM, 10GB


SOFTWARE : OS: LINUX

THEORY:

Shell is a user interface to the operating system built on the programming interface. The shell
executes commands of the form:

CommandName [argument1,…..]

The shell executes the program CommandName and passes argument on the
command line to it. Then the shell waits for the command to exit.

This program is to check whether a given number is prime or not.

ALGORITHM:

STEP 1:Read the value of n.


STEP 2:If the flag is equal to 0
then the echo $n is not a prime.
STEP 3:If the flag is equal to 1
Then the echo $n is prime.
STEP 4:End program.

DEPARTMENT OF COMPUTER SCIENCE


135

PROGRAM:

clear
echo enter a number
read n
i=2
flag=1
m=1
j=`expr $n / 2`
while [ $i -le $j ]
do
m=`expr $n % $i`
if [ $m -eq 0 ]
then
flag=0
break
fi
i=`expr $i +1`
done
if [ $flag -eq 1 ]
then
echo $n is prime
else
echo $n is not a prime
fi

OUTPUT:

[krishna-edu balu]$ sh prime


enter the number
79
79 is prime
[krishna-edu balu]$

DEPARTMENT OF COMPUTER SCIENCE


136

EXPERIMENT 24

AIM: To read numbers from the user from command line and to display
its sum using until statement.

REQUIREMENTS:

HARDWARE : PIII Processor , 128 MB RAM, 10GB


SOFTWARE : OS: LINUX

THEORY:

Shell is a user interface to the operating system built on the programming interface. The shell
executes commands of the form:

CommandName [argument1,…..]

The shell executes the program CommandName and passes argument on the
command line to it. Then the shell waits for the command to exit

This program is for reading a number from the command line and to display its sum using
until statement.

ALGORITHM:

STEP 1. If the count of number of command line arguments is 0


Then echo as invalid numbers
STEP 2. Initialize the values of sum to 0 and I to 1 and n to $#
STEP 3. Repeat the following statements until the condition becomes true
sum=`expr $sum + $1`
Increment the count of command line arguments
STEP 4. .If n!=0 then echo the value of sum.

DEPARTMENT OF COMPUTER SCIENCE


137

PROGRAM :

if [ $# -eq 0 ]
then
echo " Invalid numbers"
fi
sum=0
i=1
n=$#
until [ $# -eq 0 ]
do
sum=`expr $sum + $i`
shift
done

if [ $n -ne 0 ]
then
echo " The sum of $n numbers is $sum"
fi

OUTPUT :

[balu@krishna-edu shell]$ sh until_cmd_sum.sh 3 4


The sum of 2 numbers is 7
[balu@krishna-edu shell]$

DEPARTMENT OF COMPUTER SCIENCE


138

EXPERIMENT 25

AIM:To read n numbers from the user from the command line and to display its
sum using while statement

REQUIREMENTS:

HARDWARE : PIII Processor , 128 MB RAM, 10GB


SOFTWARE : OS: LINUX

THEORY:

Shell is a user interface to the operating system built on the programming interface. The shell
executes commands of the form:

CommandName [argument1,…..]

The shell executes the program CommandName and passes argument on the
command line to it. Then the shell waits for the command to exit

This program is for reading numbers from the user from the command line
and to display its sum using while statement.

ALGORITHM:

STEP 1. If the count of the number of command line arguments is 0 then Echo as Invalid
Numbers
STEP 2. Initialize the values of sum to 0 and n to $#
STEP 3. While $# is not equal to zero repeat the following steps
sum=`expr $sum + $i`
Increment the value of $#
STEP 4. If n is not equal to 0 then echo the value of sum

DEPARTMENT OF COMPUTER SCIENCE


139

PROGRAM:
if [ $# -eq 0 ]
then
echo "Invalid numbers"
fi
sum=0
i=1
n=$#
while [ $# -ne 0 ]
do
sum=`expr $sum + $1`
shift
done
if [ $n -ne 0 ]
then
echo "The sum of $n numbers is $sum"
fi

OUTPUT :

[balu@krishna-edu shell]$ sh while_cmd_sum.sh 4 5


The sum of 2 numbers is 9
balu@krishna-edu shell]$

DEPARTMENT OF COMPUTER SCIENCE


140

EXPERIMENT 26

AIM :-To read a numbers from the user from the command line and to display its
sum using for statement

REQUIREMENTS:

HARDWARE : PIII Processor , 128 MB RAM, 10GB


SOFTWARE : OS: LINUX

THEORY:

Shell is a user interface to the operating system built on the programming interface. The shell
executes commands of the form:

CommandName [argument1,…..]

The shell executes the program CommandName and passes argument on the
command line to it. Then the shell waits for the command to exit

This program is for reading a number from the user from the command line and to display its
sum using for statement

ALGORITHM :

STEP 1. If the count of the number of command line arguments is 0 then


Echo as Invalid numbers
STEP 2. Initialize the values of sum to 0 and n to $#
STEP 3. for I in $*
sum=`expr $sum + $I`
Increment the value of $#
STEP 4. If n is not equal to 0 then echo the value of sum

DEPARTMENT OF COMPUTER SCIENCE


141

PROGRAM:

if [ $# -eq 0 ]
then
echo " Invalid numbers"
fi

sum=0
n=$#
for i in $*
do
sum=`expr $sum + $i`
shift
done
if [ $n -ne 0 ]
then
echo "The sum of $n numbers is $sum"
fi

OUTPUT :

[balu@krishna-edu shell]$ sh for_cmd_sum.sh 4 5


The sum of 2 numbers is 9
[balu@krishna-edu shell]$

DEPARTMENT OF COMPUTER SCIENCE


142

EXPERIMENT 27

AIM : To copy a string into another string, character by character.

REQUIREMENTS :

HARDWARE : PIII Processor , 128 MB RAM, 10GB


SOFTWARE : OS: LINUX

THEORY:

Shell is a user interface to the operating system built on the programming interface.
The shell executes commands of the form:

CommandName [ argument]

The shell executes the program CommandName and passes argument on the command line
to it. Then the shell waits fro the command to exit.

This program is to copy one string to another

ALGORITHM:
STEP 1.Read the value of the string
STEP 2. Find the length of the string
STEP 3. Initialize the value of a to 1
STEP 4.Repeat the following statements until the condition a<len becomes false
Cut the first character of the string and echo rev
Increment the value of a
Store the new string in temp
STEP 6.Echo the copied string

DEPARTMENT OF COMPUTER SCIENCE


143

PROGRAM:

echo -n "Enter the string :"


read str

len=`echo $str | wc -c `
a=1
while [ $a -lt $len ]
do
rev=`echo $str | cut -c$a `
echo $rev
a=`expr $a + 1`
temp=`echo $temp$rev`
done
echo " The copied string is $temp"

[balu@krishna-edu balu]$ sh strcpy.sh


Enter the string :krishna
v
a
s
a
v
i
The copied string is krishna
[balu@krishna-edu balu]$

DEPARTMENT OF COMPUTER SCIENCE


144

EXPERIMENT 28

AIM : To reverse the given string

REQUIREMENTS :

HARDWARE : PIII Processor , 128 MB RAM, 10GB


SOFTWARE : OS: LINUX

THEORY:

Shell is a user interface to the operating system built on the programming interface.
The shell executes commands of the form:

CommandName [ argument]

The shell executes the program CommandName and passes argument on the command line
to it. Then the shell waits fro the command to exit.

This program is for reversing the given string

ALGORITHM:
STEP 1. Read the value of string
STEP 2. Find the length of the string
STEP 3.Initialize the value of a
STEP 4. Repeat the following steps until the condition length > 0 becomes false
rev=`echo $str | cut -c $len`
len=`expr $len - 1`
temp=`echo $temp$rev`
STEP 5.Echo the reversed string

DEPARTMENT OF COMPUTER SCIENCE


145

PROGRAM :
echo -n "Enter the string "
read str
len=`echo $str | wc -c`
while [ $len -gt 0 ]
do
rev=`echo $str | cut -c $len`
len=`expr $len - 1`
temp=`echo $temp$rev`
done

echo "The reversed string is $temp"

[balu@krishna-edu shell]$ sh revers.sh


Enter the string abc
The reversed string is cba
[balu@krishna-edu shell]$

DEPARTMENT OF COMPUTER SCIENCE


146

EXPERIMENT 29
AIM : To convert lowercase letters to uppercase .

REQUIREMENTS :

HARDWARE : PIII Processor , 128 MB RAM, 10GB


SOFTWARE : OS: LINUX

THEORY:

Shell is a user interface to the operating system built on the programming interface.
The shell executes commands of the form:

CommandName [ argument]

The shell executes the program CommandName and passes argument on the command line
to it. Then the shell waits fro the command to exit.

This program converts lowercase letters to uppercase

ALGORITHM:

STEP 1.Read the value of the string


STEP 2.Use tr command to translate from lower letters to upper letters
STEP 3.Echo the changed string

DEPARTMENT OF COMPUTER SCIENCE


147

PROGRAM:

echo -n "Enter a string "


read str
temp=`echo $str | tr a-z A-Z`
echo " The case changed string is $temp"

OUTPUT:

[balu@krishna-edu shell]$ sh lower_upper.sh


Enter a string krishna
The case changed string is KRISHNA
[balu@krishna-edu shell]$

DEPARTMENT OF COMPUTER SCIENCE


148

EXPERIMENT 30
AIM : To count the number of vowels in the string

REQUIREMENTS :

HARDWARE : PIII Processor , 128 MB RAM, 10GB


SOFTWARE : OS: LINUX

THEORY:

Shell is a user interface to the operating system built on the programming interface.
The shell executes commands of the form:

CommandName [ argument]

The shell executes the program CommandName and passes argument on the command line
to it. Then the shell waits fro the command to exit.

This program is for counting the number of vowels in the string

ALGORITHM:

STEP 1. Read the value of the string


STEP 2. count the characters of the string using wc command
STEP 3. caculate the length of the string by subtracting the new line character
STEP 4. Initialize the value of vowel as 0
STEP 5. Repeat the following steps until the condition len>0 becomes false
Cut each character of the string and store in temp
Check if the value of temp is any of the letters a,e,i,o,u
And increment the value of vowel
Decrement the value of length
STEP 6. Echo the number of vowels in the string

DEPARTMENT OF COMPUTER SCIENCE


149

PROGRAM:

echo -n "Enter th string "


read str
len=1echo $str | wc -c`
len=`expr $len - 1`
vowel=0
while [ $len -gt 0 ]
do
temp=`echo $str | cut -c $len`
if [ $temp = a
then
vowel=`expr $vowel + 1`
elif [ $temp = e ]
then
vowel=`expr $vowel + 1`
elif [ $temp = i ]
then
vowel=`expr $vowel + 1`
elif [ $temp = o ]
then
vowel=`expr $vowel + 1`
elif
[ $temp = u ]
then
vowel=`expr $vowel + 1`
fi
len=`expr $len - 1`
done
echo " the number of vowels in the string $str is $vowel"

OUTPUT:

[balu@krishna-edu shell]$ sh vowel.sh


Enter th string aeiou
the number of vowels in the string aeiou is 5
[balu@krishna-edu shell]$

DEPARTMENT OF COMPUTER SCIENCE


150

EXPERIMENT 31

AIM : To replace a particular character in the string

REQUIREMENTS :

HARDWARE : PIII Processor , 128 MB RAM, 10GB


SOFTWARE : OS: LINUX

THEORY:

Shell is a user interface to the operating system built on the programming interface.
The shell executes commands of the form:

CommandName [ argument]

The shell executes the program CommandName and passes argument on the command line
to it. Then the shell waits fro the command to exit.

This program for replacing a particular character in the string

ALGORITHM:

STEP 1.Read the value of string


STEP 2.Read the character to be changed
STEP 3.Read the character to be replaced
STEP 4.find the length of the string
STEP 5.Initialize the value of count to 0 and I to 1
STEP 6.Repeat the following statements until the condition I<len becomes false
cut the first character and store it in temp
compare temp with the character to be replaced
if both are equal then append the character to be replaces to the new variable
else append temp to new variable
Increment the value of i

DEPARTMENT OF COMPUTER SCIENCE


151

PROGRAM:

echo -n "Enter the string :"


read str
echo -n "Enter the character to be counted:"
read c
len=`echo $str | wc -c`
len=`expr $len - 1`
count=0
while [ $len -gt 0 ]
do
temp=`echo $str | cut -c $len`
if [ $temp = $c ]
then
count=`expr $count + 1`
fi
len=`expr $len - 1`
done
echo "The number of occurrence of the character $c in the string $str is $count"

OUTPUT:

[balu@krishna-edu last]$ sh count_char.sh


Enter the string :krishna
Enter the character to be counted:v
The number of occurrence of the character v in the string krishna is 2
[balu@krishna-edu last]$

DEPARTMENT OF COMPUTER SCIENCE

You might also like