#Assignment 2 : B.
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
address.sh
#-------------------------------------------------------------------------------------------------------------------
#!bin/bash
echo -e "\n\t\tWelcome to our Address Book !\n"
while true
do
echo -e "Select your option : \n1.Enter data \n2.Display data\n3.Search using name\
n4.Delete using name \n5.Modify using name\n6.Exit\n\t\t"
read ch
case $ch in
1)
while true
do
echo -e "\nEnter the record : "
echo "Eno. : "
read eno
echo "Ename : "
read ename
while true
do
echo "Mob no. : "
read mno
len=${#mno}
if test $len = 10
then
break
else
echo -e "\n\t\tInvalid number please enter again ! "
fi
done
echo "City : "
read city
echo -e "$eno\t$ename\t$mno\t$city" >> records.txt
echo -e "\n\nDo you want to enter more records ? [y/n] : "
read cho
if test $cho = n
then
break
fi
done
echo -e "\n\t\t Records added succesfully !\n"
;;
2) if [[ -s records.txt ]]
then
echo -e "\n\n---------Records---------\n"
cat records.txt
echo -e "\n\n"
else
echo -e "\n\t\tFile is empty !\n"
fi
;;
3)
echo -e "\nEnter the name to be searched : "
read s_name
echo
grep -i $s_name records.txt
echo
;;
4)
echo -e "\nEnter the name to be deleted : "
read d_name
#grep -v $d_name records.txt > temp
#mv temp records.txt
sed -i "/$d_name/d" records.txt
echo -e "\n\t\t Record deleted succesfully !\n"
;;
5)
echo -e "\nEnter the name for record to be modified : "
read m_name
no=`grep -n $m_name records.txt | cut -c 1`
echo -e "\nEnter the value that you want to modify :"
read m_value
echo -e "\nEnter the new value : "
read n_value
sed -i "$no s/$m_value/$n_value/" records.txt
echo -e "\n\t\t Record modified successfully !"
;;
*)
echo -e "\nProgram Finished due to exit or wrong option !"
break 2
;;
esac
done
rm records.txt
#Output:
samarth@samarth-OptiPlex-7010:~$ chmod 777 address.sh
samarth@samarth-OptiPlex-7010:~$ bash address.sh
Welcome to our Address Book !
Select your option :
1.Enter data
2.Display data
3.Search using name
4.Delete using name
5.Modify using name
6.Exit
1
Enter the record :
Eno. :
1
Ename :
aaa
Mob no. :
9898989898
City :
pune
Do you want to enter more records ? [y/n] :
y
Enter the record :
Eno. :
2
Ename :
bbb
Mob no. :
9090909090
City :
nagar
Do you want to enter more records ? [y/n] :
y
Enter the record :
Eno. :
3
Ename :
ccc
Mob no. :
9000000000
City :
nashik
Do you want to enter more records ? [y/n] :
n
Records added succesfully !
Select your option :
1.Enter data
2.Display data
3.Search using name
4.Delete using name
5.Modify using name
6.Exit
2
---------Records---------
1 aaa 9898989898 pune
2 bbb 9090909090 nagar
3 ccc 9000000000 nashik
Select your option :
1.Enter data
2.Display data
3.Search using name
4.Delete using name
5.Modify using name
6.Exit
Enter the name to be searched :
bbb
2 bbb 9090909090 nagar
Select your option :
1.Enter data
2.Display data
3.Search using name
4.Delete using name
5.Modify using name
6.Exit
Enter the name to be deleted :
bbb
Record deleted succesfully !
Select your option :
1.Enter data
2.Display data
3.Search using name
4.Delete using name
5.Modify using name
6.Exit
---------Records---------
1 aaa 9898989898 pune
3 ccc 9000000000 nashik
Select your option :
1.Enter data
2.Display data
3.Search using name
4.Delete using name
5.Modify using name
6.Exit
Enter the name for record to be modified :
ccc
Enter the value that you want to modify :
ccc
Enter the new value :
ddd
Record modified successfully !
Select your option :
1.Enter data
2.Display data
3.Search using name
4.Delete using name
5.Modify using name
6.Exit
---------Records---------
1 aaa 9898989898 pune
3 ddd 9000000000 nashik
Select your option :
1.Enter data
2.Display data
3.Search using name
4.Delete using name
5.Modify using name
6.Exit
Program Finished due to exit or wrong option !
samarth@samarth-OptiPlex-7010:~$