Shell Programming
Shell Programming
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
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.
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
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.
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
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
Result:
Thus the shell program to find odd or even was executed successfully.
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:
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
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
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.
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
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
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
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
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.
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
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.
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:
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.
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.
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:
Output:
Enter filename : samp.c
Regular file
Result:
Thus the shell program to detect a file type was executed successfully.
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.
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:
Result:
Thus the shell program to remove a duplicate file was executed successfully.