0% found this document useful (0 votes)
9 views21 pages

Shell Programming

The document outlines various shell programming tasks including swapping values of variables, calculating area and circumference of a circle, computing simple and compound interest, determining odd or even numbers, finding the biggest of three numbers, checking for leap years, grade determination, string comparison, employee pay calculation, Armstrong number verification, reversing a number, generating Fibonacci series, identifying prime numbers, calculating factorials, and computing the sum and average of numbers. Each task includes an aim, algorithm, program code, sample output, and a result indicating successful execution. The document serves as a comprehensive guide for beginners in shell programming.

Uploaded by

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

Shell Programming

The document outlines various shell programming tasks including swapping values of variables, calculating area and circumference of a circle, computing simple and compound interest, determining odd or even numbers, finding the biggest of three numbers, checking for leap years, grade determination, string comparison, employee pay calculation, Armstrong number verification, reversing a number, generating Fibonacci series, identifying prime numbers, calculating factorials, and computing the sum and average of numbers. Each task includes an aim, algorithm, program code, sample output, and a result indicating successful execution. The document serves as a comprehensive guide for beginners in shell programming.

Uploaded by

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

SHELL PROGRAMMING

Swapping values of two variables

Aim:
To swap a values of two variables using shell program.

Algorithm :

Step 1 : Start
Step 2 : Read the values of a and b
Step 3 : Interchange the values of a and b using another variable t as follows:
t=a
a=b
b=t
Step 4 : Print a and b
Step 5 : Stop

Program:
# Swapping values of two variables

echo "Enter value for A : "


read a
echo "Enter value for B : "
read b
t=$a
a=$b
b=$t
echo "Values after Swapping"
echo "A Value is $a"
echo "B Value is $b"

Output
Enter value for A : 12
Enter value for B : 23
Values after SwappingA Value is 23
B Value is 12

Result:
Thus the shell program to swap two variables was executed successfully.

Area and Circumference of Circle


Aim:
To calculate area and circumference of circle using shell program.

Algorithm:
Step 1 : Start
Step 2 : Define constant pi = 3.14
Step 3 : Read the value of radius
Step 4 : Calculate area using formulae: pi × radius2
Step 5 : Calculate circumference using formulae: 2 × pi × radius
Step 6 : Print area and circumferen ce
Step 7 : Stop

Program:
# Area and Circumference of Circle

pi=`expr "scale=2; 22 / 7" | bc`


readonly pi
echo "Enter value for radius : "
read radius
area=`expr "scale=2; $pi * $radius * $radius" | bc`
circum=`expr "scale=2; 2 * $pi * $radius" | bc`
echo "Area : $area"
echo "Circumference : $circum"

Output
Enter value for radius : 12
Area : 452.16
Circumference : 75.36

Result:
Thus the shell program to calculate area and circumference of circle was executed
successfully.

Simple and compound Interest Calculation

Aim:
To calculate simple and compound interest using shell program.

Algorithm:

Step 1 : Start
Step 2 : Read the values principal, rate and years
Step 3 : Compute simpleinterest using the formulae: (principal × rate × years) / 100
Step 4 : Print simpleinterest
Step 5 : Stop

Program:
# Simple and compound Interest Calculation

echo "Enter Principal amount : "


read p
echo "Enter number of years : "
read n
echo "Enter rate of interest : "
read r
echo "Enter no of times the interest is compounded per year : "
read t
si=`expr "scale=2; $p * $n *$r / 100" | bc`
ci=`expr "scale=2; $p * ( 1 + $r / $t ) ^ ( $n * $t) " | bc`
echo "Simple Interest : $si"
echo "Compound Interest : $ci"

Output:

Enter Principal amount : 1285 Enter number of years : 3 Enter rate of interest : 5
Simple Interest : 192.75

Result:
Thus the shell program to calculate simple and compound interest was executed
successfully.

Odd or Even

Aim:
To find odd or even number using shell program.

Algorithm:

Step 1 : Start
Step 2 : Read number
Step 3 : If number divisible by 2 then
Print "Number is Even"
Step 4 : else
Print "Number is Odd"
Step 5 : Stop

Program:

# Odd or even

echo "Enter a non-zero number : "


read num
rem=`expr $num % 2`
if [ $rem -eq 0 ]
then
echo "$num is Even"
else
echo "$num is Odd"
fi
Output:
Enter a non-zero number : 12
12 is Even

Result:
Thus the shell program to find odd or even was executed successfully.

Biggest of three numbers

Aim:
To find the biggest of three numbers using shell program.

Algorithm:

Step 1 : Start
Step 2 : Read values of a, b and c
Step 3 : If a > b and a > c then
Print "A is the biggest"
Step 4 : else if b > c then
Print "B is the biggest "
Step 5 : else
Print "C is the biggest "
Step 6 : Stop

Program:

Biggest of three numbers

echo "Give value for A B and C: "


read a b c
if [ $a -gt $b -a $a -gt $c ]
then
echo "A is the Biggest number"
elif [ $b -gt $c ]
then
echo "B is the Biggest number"
else
echo "C is the Biggest number"
fi

Output:
Give value for A B and C: 4 3 4
C is the Biggest number

Result:
Thus the shell program to find biggest of three numbers was executed
successfully.

Leap year

Aim:
To calculate the given year is leap year using shell program.

Algorithm:

Step 1 : Start
Step 2 : Read the value of year
Step 3 : If year divisible by 400 then
Print "Leap year"
Step 3.1 : else if year divisible by 4 and not divisible b y 100 then
Print "Leap year"
Step 3.2 : else
Print "Not a leap year"
Step 4 : Stop

Program:

# Leap year
echo "Enter a year : "
read year
rem1=`expr $year % 4`
rem2=`expr $year % 100`
rem3=`expr $year % 400`
if [ $rem3 -eq 0 ]
then
echo "$year is a Leap Year"
elif [ $rem2 -ne 0 -a $rem1 -eq 0 ]
then
echo "$year is a Leap Year"
else
echo "$year is Not a leap year"
fi

Output:
Enter a year : 1900
1900 is Not a leap year

Result:
Thus the shell program to calculate the given year is leap year was executed
successfully.

Grade Determination
Aim:
To calculate grade determination for marks using shell program.

Algorithm:

Step 1 : Start
Step 2 : Read mark
Step 3 : If mark > 90 then
Print "S grade"
Step 3.1 : else if mark > 80 then
Print "A grade"
Step 3.2 : else if mark > 70 then
Print "B grade"
Step 3.3 : else if mark > 60 then
Print "C grade"
Step 3.4 : else if mark > 55 then
Print "D grade"
Step 3.5 : else if mark .. 50 then
Print "E grade"
Step 3.6 : else
Print "U grade"
Step 4 : Stop

Program:
# Grade Determination

echo "Enter the mark : "


read mark
if [ $mark -gt 90 ]
then
echo "S Grade"
elif [ $mark -gt 80 ]
then
echo "A Grade"
elif [ $mark -gt 70 ]
then
echo "B Grade"
elif [ $mark -gt 60 ]
then
echo "C Grade"
elif [ $mark -gt 55 ]
then
echo "D Grade"
elif [ $mark -ge 50 ]
then
echo "E Grade"
else
echo "U Grade"
fi
Output: Enter the mark : 65
C Grade
Result:
Thus the shell program to calculate grade determination for marks was executed
successfully.

String comparison
Aim:
To compare the given strings using shell program.

Algorithm:

Step 1 : Start
Step 2 : Read strings str1 and str2
Step 3 : If str1 = str2 then
Print "Strings are the same"
Step 3.1 : else
Print "Strings are distinct"
Step 4 : Stop

Program:
# String comparison

echo "Enter the first string : "


read s1
echo -n "Enter the second string : "
read s2
if [ $s1 == $s2 ]
then
echo "Strings are the same"
else
echo "Strings are distinct"
fi

Output:
Enter the first string : ece-a
Enter the second string : ECE-A
Strings are distinct

Result:
Thus the shell program to compare the given string was executed successfully.

Employee Pay Calculation

Aim:
To calculate employee pay calculation using shell program.
Algorithm:

Step 1 : Start
Step 2 : Read basic
Step 3 : If basic > 30000 then
hra is 5% of basic
da is 5% of basic
tax is 10% of basic
Step 3.1 : else if basic > 20000 then
hra is 4% of basic
da is 3% of basic
tax is 8% of basic
Step 3.2 : else
hra is 3% of basic
da is 2% of basic
tax is 5% of basic
Step 4 : Stop

Program:
#Employee Pay Calculation

echo "Enter employee basic pay : "


read basic
if [ $basic -gt 30000 ]
then
hra=`expr 5 \* $basic / 100`
da=`expr 5 \* $basic / 100`
tax=`expr 10 \* $basic / 100`
elif [ $basic -gt 20000 ]
then
hra=`expr 4 \* $basic / 100`
da=`expr 3 \* $basic / 100`
tax=`expr 8 \* $basic / 100`
else
hra=`expr 3 \* $basic / 100`
da=`expr 2 \* $basic / 100`
tax=`expr 5 \* $basic / 100`
fi
gross=`expr $basic + $da + $hra`
netpay=`expr $gross - $tax`
echo "Gross Pay : $gross"
echo "Net Pay : $netpay"

Output:
Enter employee basic pay : 12000
Gross Pay : 12600
Net Pay : 12000

Result:
Thus the shell program to calculate employee pay calculation was executed
successfully.
Armstrong Number

Aim:
To find the given number is Armstrong number using shell program.

Algorithm:

Step 1 : Start
Step 2 : Read number
Step 3 : Initialize 0 to sum and number to num
Step 4 : Extract lastdigit by computing number modulo 10
Step 5 : Cube the lastdigit and add it to sum
Step 6 : Divide number by 10
Step 7: Repeat steps 4–6 until number > 0
Step 8 : If sum = number then
Print “Armstrong number”
Step 8.1 : else
Print “Not an Armstrong number”
Step 9 : Stop

Program:
#Armstrong Number
echo "Enter a number : "
read n
a=$n
s=0
while [ $n -gt 0 ]
do
r=`expr $n % 10`
s=`expr $s + \( $r \* $r \* $r \)`
n=`expr $n / 10`
done
if [ $a -eq $s ]
then
echo "Armstrong Number"
else
echo "Not an Armstrong number"
fi

Output:
Enter a number : 370
Armstrong Number

Result:
Thus the shell program to find the given number is Armstrong number was
executed successfully.
Number Reverse

Aim:
To find the reverse number using shell program.

Algorithm:

Step 1 : Start
Step 2 : Read number
Step 3 : Initialize 0 to reverse
Step 4 : Extract lastdigit b y computing number modulo 10
Step 5 : Compute reverse = reverse10 + lastdigit
Step 6 : Divide number b y 10
Step 7: Repeat steps 4–6 until number > 0
Step 8 : Print reverse
Step 9 : Stop

Program:
#Number Reverse

echo "Enter a number : "


read n
rd=0
while [ $n -gt 0 ]
do
rem=`expr $n % 10`
rd=`expr $rd \* 10 + $rem`
n=`expr $n / 10`
done
echo "Reversed number is $rd"

Output:
Enter a number : 234
Reversed number is 432

Result:
Thus the shell program to find the reverse number was executed successfully.

Fibonacci Series
Aim:
To find the Fibonacci series of a number using shell program.

Algorithm:

Step 1 : Start
Step 2 : Read number of terms as n
Step 3 : Initialize 0 to f1, 1 to f2 and 2 to i
Step 4 : Print initial fibonacci terms f1, f2
Step 5 : Generate next term using the formula f3 = f1 + f2
Step 6 : Print f3
Step 7 : Increment i by 1
Step 8 : Assign f2 to f1
Step 9 : Assign f3 to f2
Step 10 : Repeat steps 5–9 until i .. n
Step 11 : Stop

Program:
#Fibonacci Series

echo "Enter number of terms : "


read n
echo "Fibonacci Series"
f1=0
f2=1
echo -n "$f1 "
echo -n " $f2 "
i=2
while [ $i -lt $n ]
do
f3=`expr $f1 + $f2`
echo -n " $f3 "
f1=$f2
f2=$f3
i=`expr $i + 1`
done
echo

Output:
Enter number of terms : 8
Fibonacci Series
0 1 1 2 3 5 8 13

Result:
Thus the shell program to find Fibonacci series of a number was executed
successfully.

Prime Number

Aim:
To find the prime number using shell program.

Algorithm:
Step 1 : Start
Step 2 : Read the value of n
Step 3 : Initialize i to 2
Step 4 : If n is divisible by i then
Print “Not Prime” and Stop
Step 5 : Increment i by 1
Step 6 : Repeat steps 4 and 5 until i .. n/2
Step 7 : Print "Prime"
Step 8 : Stop

Program:
#Prime Number

echo "Enter the number : "


read n
i=2
m=`expr $n / 2`
until [ $i -gt $m ]
do
q=`expr $n % $i`
if [ $q -eq 0 ]
then
echo "Not a Prime number"
exit
fi
i=`expr $i + 1`
done
echo "Prime number"

Output:
Enter the number : 17
Prime number

Result:
Thus the shell program to find prime number was executed successfully.

Factorial Value
Aim:
To find the factorial of a number using shell program.

Algorithm:

Step 1 : Start
Step 2 : Read number
Step 3 : Initialize 1 to fact and number to i
Step 4 : fact = fact * i
Step 5 : Decrement i by 1
Step 6: Repeat steps 4–6 until i > 0
Step 7 : Print fact
Step 8 : Stop
Program: #Factorial Value
echo "Enter a positive number : "
read n
f=1
until [ $n -lt 1 ]
do
f=`expr $f \* $n`
n=`expr $n - 1`
done
echo "Factorial value : $f"

Output:
[vijai@localhost loops]$ sh fact.sh
Enter a positive number : 10
Factorial value : 3628800

Result:

Thus the shell program to find factorial of a number was executed successfully.

Sum and Average of N numbers


Aim:
To calculate sum and average of a number using shell program.

Algorithm:

Step 1 : Start
Step 2 : Read n
Step 3 : Initialize 0 to sum and 1 to i
Step 4 : Add i to sum
Step 5 : Increment i by 1
Step 6: Repeat steps 4–6 until i.... n
Step 7: Calculate average of n numbers.
Step 8 : Print sum and average.
Step 9 : Stop

Program:
#Sum and Average of N numbers

echo "Enter N value : "


read n
sum=0
i=1
until [ $i -gt $n ]
do
sum=`expr $sum + $i`
i=`expr $i + 1`
done
avg= `expr $sum / $n`
echo "The sum of n numbers is $sum"
echo "The average of n numbers is $avg"

Output:
Enter N value : 26
The sum of n numbers is 351

Result:
Thus the shell program to calculate sum and average of a number was executed
successfully.

Time based greeting


Aim:
To check the time based greeting using shell program.

Algorithm:

Step 1 : Start
Step 2 : Read the date and time
Step 3 : If it is before 12pm
Print "Good Morning"
Step 3.1 : else if it is 12pm to 2pm then
Print "Good Afternoon"
Step 3.2 : else
Print "Good Evening"
Step 4 : Stop

Program:

#Time based greeting

mrd=`date +%P`
if [ $mrd=='am' ]
then
if [ $x -le 11 ]
then
echo "Good Morning"
fi
else
if [ $x -le 2 ]
then
echo "Good Afternoon"
elif [ $x -le 6 ]
then
echo "Good Evening"
else
echo "Good Night"
fi
fi
Output:
Good Morning

Result:
Thus the shell program to check time based greeting was executed successfully.

Number of days in a month


Aim:
To check the number of days in a month using shell program.

Algorithm:

Step 1 : Start
Step 2 : Read the current date and month
Step 3 : If month is feb
Print "Leap year"
Step 3.1 : else if year divisible by 4 and not divisible b y 100 then
Print "Leap year"
Step 3.2 : else
Print "Not a leap year"
Step 4 : Stop

Program:
#Number of days in a month

mth=`date +%m`
mn=`date +%B`
case $mth in
02)echo "February usually has 28 days"
echo "If leap year, then it has 29 days" ;;
04|06|09|11)echo "The current month $mn has 30 days" ;;
*) echo "The current month $mn has 31 days"
esac

Output:
The current month April has 30 days

Result:
Thus the shell program to check number of days in a month was executed
successfully.

Commands menu
Aim:
To find the commands menu using shell program.
Algorithm:
Step1:start
Step2:read character
Step3:display menu
Step4:read choice
Step5:print command
Step6:stop

Program
#Commands menu

ch='y'
while [ $ch == 'y' ]
do
echo -e "\tMENU
1. List of files
2. Working Directory
3. Date and Time
4. Users of the system
5. Calendar
Enter the option : \c"
read choice
case "$choice" in
1) ls -l ;;
2) pwd ;;
3) date ;;
4) who ;;
5) cal
esac
echo -n "Do you wish to continue (y/n) : "
read ch
done

Output:
MENU
1. List of files
2. Working Directory
3. Date and Time
4. Users of the system
5. Calendar
Enter the option : 4vijai pts/20 Apr 4 13:41 (linux-21.smkfomra.com)
cse1 pts/13 Apr 4 13:43 (scl-58.smkfomra.com)
Do you wish to continue (y/n) : n

Result:
Thus the shell program to check command menu was executed successfully.

Directory Listing
Aim:
To find the directory listing using shell program.

Algorithm:
Step1:start
Step2:read x
Step3:enter the list of directories R
Step4:stop

Program:

#Directory Listing
for x in .
do
ls -R $x
done

Output:
list.sh shellscripts
./shellscripts:
command decision loops patmatch simple
./shellscripts/command:
cmdmenu.sh debug.sh dupremove.sh filetype.sh keystroke.sh
./shellscripts/decision:
big3.sh emppay.sh grade.sh leap.sh oddeven.sh strcomp.sh
./shellscripts/loops:
armstrong.sh datastat.sh fact.sh fibo.sh multable.sh
./shellscripts/patmatch:
calc.sh rental.sh vote.sh vowel.sh
./shellscripts/simple:
circle.sh degconv.sh simpint.sh swap.sh

Result:
Thus the shell program to find directory listing was executed successfully.

Detecting file type


Aim:
To detect the file type using shell program.

Algorithm:
Step1:start
Step2:read filename
Step3:if directory[-e=filename]
Then
if directory [-f=filename]
step4: print regular name
else print directory
else print special file
else print file does not exist
step5:stop

Program:

#Detecting file type

echo "Enter filename : "


read fname
if [ -e $fname ]
then
if [ -f $fname ]
then
echo "Regular file"
elif [ -d $fname ]
then
echo "Directory"
else
echo "Special file"
fi
else
echo "File does not exist"
fi

Output:
Enter filename : samp.c
Regular file

Result:
Thus the shell program to detect a file type was executed successfully.

Detecting user when logged on


Aim:
To detect user logged on using shell program.

Algorithm:
Step1:start
Step2: read user details
Step3:if directory $1>/del/null
Step4:print directory1 is logged in
Else
Display Exit
Step5: stop
Program:
#Detecting user when logged on

while true
do
if who|grep $1>/dev/null
then
echo $1 is logged in
exit
fi
done
Output:
[cse1 is logged in

Result:
Thus the shell program to detect user logged on was executed successfully.

Duplicate file removal


Aim:
To remove the duplicate file using shell program.

Algorithm:
Step1:start
Step2:read two files $1,$2
Step3:compare if $1=$2
Step4:print files are identical
Step4.1:if rm=$2
Step4.2: Print file deleted
Step4.3:else print files are deleted
Step5: stop

Program:

#Duplicate file removal


if cmp $1 $2then
echo "Files $1, $2 are identical"
rm $2
echo "$2 file deleted"
else
echo "Files $1, $2 are distinct"
fi
Output:
samp.c s.c
Files samp.c, s.c are identical
s.c file deleted

Result:
Thus the shell program to remove a duplicate file was executed successfully.

You might also like