Skip to content

Instantly share code, notes, and snippets.

@Cosmicoppai
Created December 16, 2021 09:00
Show Gist options
  • Save Cosmicoppai/7212ba3450caf05fec15549b147a35e6 to your computer and use it in GitHub Desktop.
Save Cosmicoppai/7212ba3450caf05fec15549b147a35e6 to your computer and use it in GitHub Desktop.
Address Book using shell Script
#!/usr/bin/env sh
# Program to implement an address book with options given below
#1. create the database
#2. View address book
#3. Insert a Record
#4. Delete a Record
#5. Modify the Record
#6. Exit
create () {
touch address.txt
echo "address database created"
}
insert () {
if [[ "$1" == "" ]]; then
echo "Enter the name"
read name
else
name=$1
fi
if [[ "$(grep -i -c $name address.txt)" == 0 ]]; then
echo "Enter the Address"
read address
echo "Enter the Mobile No"
read number
echo "Enter the Email Id"
read emailId
echo "$name $address $number $emailId" >> address.txt
echo "Data Inserted Succesfully"
else
echo "Record for ${name} already exist"
fi
}
view () {
echo "Enter the name of the person to view a single entry or enter all to view the Database"
read input
if [[ "${input,,}" == "all" ]]; then
echo "============================================================================================================="
cat address.txt
echo "============================================================================================================="
else
echo "============================================================================================================="
echo "$(grep -i -c $input test.txt) records found"
echo "--------------------------------------------------------------------------------------------------------------"
grep -i $input address.txt
echo "============================================================================================================="
fi
}
modify () {
echo "enter the name of the person whom record you want to edit"
read name
grep -i -v $name address.txt >> address1.txt
cp address1.txt address.txt
rm address1.txt
insert $name
}
delete () {
echo "Are you sure you want to delete the database: Press Y[Yes]/N[No]"
read confirmation
if [[ "${confirmation,,}" == "y" || "${confirmation,,}" == "yes" ]]; then
rm address.txt
echo "Database Deleted"
elif [[ "${confirmation,,}" == "no" || "${confirmation,,}" == "n" ]]; then
return
else
echo "Invalid Option"
fi
}
while :
do
echo "enter your choice"
echo " 1->create"
echo " 2->insert"
echo " 3->view"
echo " 4->modify"
echo " 5->delete"
echo " 6->exit"
read ch
case $ch in
1)create ;;
2)insert ;;
3)view ;;
4)modify ;;
5)delete ;;
6)exit 0;;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment