1.
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
2. 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