0% found this document useful (0 votes)
3 views17 pages

Shell Script 1

The document contains a series of practical shell scripting exercises that cover various functionalities such as creating an address book, generating a marksheet, displaying multiplication tables, finding factorials, generating prime and Fibonacci numbers, checking for palindromes, displaying calendars, and validating dates. Each practical includes a description of the task, the solution approach, and the corresponding shell script code. The exercises are designed to enhance understanding of shell scripting and its applications.

Uploaded by

ncharanreddy8228
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)
3 views17 pages

Shell Script 1

The document contains a series of practical shell scripting exercises that cover various functionalities such as creating an address book, generating a marksheet, displaying multiplication tables, finding factorials, generating prime and Fibonacci numbers, checking for palindromes, displaying calendars, and validating dates. Each practical includes a description of the task, the solution approach, and the corresponding shell script code. The exercises are designed to enhance understanding of shell scripting and its applications.

Uploaded by

ncharanreddy8228
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

Practical-3

Write a program to implement an address book with options given below:


a) Create address book. B) View address book. C) Insert a record. D) Delete a record. e)
Modify a record. F) Exit.
Solution:
An address book is a file used to store records like Name , Phone number and Address
In shell scripting, we use:
 touch → to create file
 cat → to display contents
 echo → to insert data
 grep → to search records
 sed → to modify/delete records
Code:
book="[Link]"
while true
do
echo " ADDRESS BOOK MENU "
echo "1. Create Address Book"
echo "2. View Address Book"
echo "3. Insert Record"
echo "4. Delete Record"
echo "5. Modify Record"
echo "6. Exit"
echo "Enter your choice:"
read ch
case $ch in
1)
# Create Address Book
> $book
echo "Address book created."
;;
2)
# View Address Book
if [ -f $book ]
then
echo "----- Address Book -----"
cat $book
else
echo "Address book does not exist."
fi
;;

3)
# Insert Record
echo "Enter Name:"
read name
echo "Enter Phone:"
read phone
echo "Enter Address:"
read addr

echo "$name | $phone | $addr" >> $book


echo "Record inserted."
;;

4)
# Delete Record
echo "Enter name to delete:"
read name
grep -v "$name" $book > [Link]
mv [Link] $book

echo "Record deleted."


;;

5)
# Modify Record
echo "Enter name to modify:"
read name

grep "$name" $book


if [ $? -eq 0 ]
then
echo "Enter new phone:"
read newphone
echo "Enter new address:"
read newaddr

sed -i "/$name/c\\$name | $newphone | $newaddr" $book


echo "Record modified."
else
echo "Record not found."
fi
;;

6)
# Exit
echo "Exiting..."
break
;;

*)
echo "Invalid choice!"
;;
esac
done
Practical-4
Write a shell script to generate marksheet of a student. Take 3 subjects, calculate
and display total marks, percentage and Class obtained by the student
Solution:
The script uses:
 read → to take input
 Arithmetic operations → to calculate total and percentage
 if-elif-else → to determine class
Code:
echo "Enter marks of Subject 1:"
read m1
echo "Enter marks of Subject 2:"
read m2
echo "Enter marks of Subject 3:"
read m3
# Calculate total
total=$((m1 + m2 + m3))

# Calculate percentage
per=$((total / 3))
echo "-------------------------"
echo " MARKSHEET "
echo "-------------------------"
echo "Marks 1: $m1"
echo "Marks 2: $m2"
echo "Marks 3: $m3"
echo "Total Marks: $total"
echo "Percentage: $per %"

# Determine class
if [ $per -ge 70 ]
then
echo "Class: Distinction"
elif [ $per -ge 60 ]
then
echo "Class: First Class"
elif [ $per -ge 50 ]
then
echo "Class: Second Class"
elif [ $per -ge 40 ]
then
echo "Class: Pass Class"
else
echo "Class: Fail"
fi
Practical-5
Write a shell script to
a) display multiplication table of given number
b) find factorial of given number n.
Code:
echo "Enter a number:"
read num

echo "Choose an option:"


echo "1. Multiplication Table"
echo "2. Factorial"
read choice

case $choice in
1)
echo "Multiplication Table of $num"
i=1
while [ $i -le 10 ]
do
result=`expr $num \* $i`
echo "$num x $i = $result"
i=`expr $i + 1`
done
;;
2)
fact=1
i=1
while [ $i -le $num ]
do
fact=`expr $fact \* $i`
i=`expr $i + 1`
done
echo "Factorial of $num = $fact"
;;
*)
echo "Invalid Choice"
;;
esac
Practical-6
Write a shell script which
a) will accept a number b and display first n prime numbers as output.
b) will generate first n Fibonacci numbers like: 1, 1, 2, 3, 5, 13, …
c) will check entered string is palindrome or not.
Code:
echo "Enter value of n:"
read n
echo "Choose an option:"
echo "1. First n Prime Numbers"
echo "2. First n Fibonacci Numbers"
echo "3. Check Palindrome String"
read ch
case $ch in
1)
count=0
num=2
echo "First $n Prime Numbers are:"
while [ $count -lt $n ]
do
i=2
flag=1
while [ $i -lt $num ]
do
rem=`expr $num % $i`
if [ $rem -eq 0 ]
then
flag=0
break
fi
i=`expr $i + 1`
done
if [ $flag -eq 1 ]
then
echo $num
count=`expr $count + 1`
fi
num=`expr $num + 1`
done
;;
2)
a=1
b=1
count=1
echo "First $n Fibonacci Numbers are:"
while [ $count -le $n ]
do
echo -n "$a "
c=`expr $a + $b`
a=$b
b=$c
count=`expr $count + 1`
done
echo
;;
3)
echo "Enter a string:"
read str
rev=""
len=`echo $str | wc -c`
len=`expr $len - 1`
i=$len
while [ $i -gt 0 ]
do
ch=`echo $str | cut -c$i`
rev=$rev$ch
i=`expr $i - 1`
done
if [ "$str" = "$rev" ]
then
echo "String is Palindrome"
else
echo "String is Not Palindrome"
fi
;;
*)
echo "Invalid Choice"
;;
esac
Practical-7
Write a menu driven shell script which will print the following menu and execute the
given task
a) Display calendar of current month
b) Display today’s date and time and a welcome message (like Good Morning
etc.). The time should be displayed with “a.m.” or “p.m.” and not in 24hours
notation.
c) Display usernames those are currently logged in the system
d) Display your name at given x, y position
e) Display your terminal number
Code:
echo "1. Display calendar of current month"
echo "2. Display today's date, time and welcome message"
echo "3. Display usernames currently logged in"
echo "4. Display your name at given x, y position"
echo "5. Display your terminal number"
echo "6. Exit"
echo "Enter your choice:"
read ch
case $ch in
1)
cal
;;
2)
hr=`date +%I`
ampm=`date +%p`
time=`date +"%I:%M:%S"`
if [ $hr -lt 12 ]
then
msg="Good Morning"
elif [ $hr -lt 4 ]
then
msg="Good Afternoon"
elif [ $hr -lt 8 ]
then
msg="Good Evening"
else
msg="Good Night"
fi
echo "Today's Date: `date +"%d-%m-%Y"`"
echo "Time: $time $ampm"
echo "$msg"
;;
3)
who | cut -d " " -f1
;;
4)
echo "Enter x position (row):"
read x
echo "Enter y position (column):"
read y
echo "Enter your name:"
read name
tput cup $x $y
echo "$name"
;;
5)
tty
;;
6)
echo "Exiting..."
;;
*)
echo "Invalid Choice"
;;
esac
Practical-8
Write a shell script to read n numbers as command arguments and sort them in
descending order.
Code:
# Check whether numbers are passed or not
if [ $# -eq 0 ]
then
echo "Usage: $0 num1 num2 num3 ..."
exit
fi

# Bubble sort in descending order


for ((i=1; i<=$#; i++))
do
for ((j=1; j<=$#-$i; j++))
do
a=${!j}
k=$((j+1))
b=${!k}
if [ $a -lt $b ]
then
temp=$a
eval $j=$b
eval $k=$temp
fi
done
done
echo "Numbers in descending order:"
echo "$@"
Practical-9
Write a shell script to validate the entered date

Code:
echo "Enter date in format DD MM YYYY :"
read dd mm yy

# Check valid year


if [ $yy -lt 1 ]
then
echo "Invalid Date"
exit
fi

# Check valid month


if [ $mm -lt 1 ] || [ $mm -gt 12 ]
then
echo "Invalid Date"
exit
fi

# Find days in month


case $mm in
1|3|5|7|8|10|12)
max=31
;;
4|6|9|11)
max=30
;;
2)
# Leap year check
if [ `expr $yy % 400` -eq 0 ] || { [ `expr $yy % 4` -eq 0 ] && [ `expr $yy % 100` -ne 0 ]; }
then
max=29
else
max=28
fi
;;
esac
# Check valid day
if [ $dd -ge 1 ] && [ $dd -le $max ]
then
echo "Valid Date"
else
echo "Invalid Date"
fi

You might also like