LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Making a phonebook! (https://www.linuxquestions.org/questions/linux-newbie-8/making-a-phonebook-893322/)

Gfaith31 07-23-2011 08:29 AM

Making a phonebook!
 
Nice day!
We have an ongoing assignment, the assignment should states that we'll gonna make a like-phonebook using shell scripting that allows user to add, edit, delete, view(in sorted data) and search.
I already make it but it does not return sorted data and does not allow me to edit....
This is the code:
PHONEBOOK="address-book.txt"
createmenu()
{
echo "~~~~~PHONEBOOK~~~~~"
echo "1.Add"
echo "2.Edit"
echo "3.Delete"
echo "4.View"
echo "5.Exit"
echo "Enter your choice:"
return 1
}
add()
{
echo "~~~~~NEW CONTACT~~~~~"
echo "Contact Name:"
read fname
echo "Enter Number:"
read fnum
echo "Do you want to add contact,`echo $fname`?[y/n]"
read yn
if [ $yn == "y" -o $yn == "Y" ]
then echo $fname
$fnum >> PHONEBOOK;
echo "added `echo $name`";
createmenu
fi
exit 0
}
del()
{
echo "~~~~~DELETE~~~~~"
echo "Enter name to be deleted:"
read fdel
x=`grep $fdel PHONEBOOK`
echo $x
echo "Are you sure you want to delete?, `echo $fdel`?[y/n]"
read yn
if [ yn == "y" -o yn == "Y" ]
then sed '0, $fdel' PHONEBOOK;
else createmenu
fi
}
view()
{
echo "~~~~~CONTACTS~~~~~"
echo "Name Phone Number"
sort -d < PHONEBOOK
}
exit()
{
if $5
then exit 1
}
createmenu
read choice
case $choice in
1) add;read;;
2) echo "OK";read;;
3) del;read;;
4) view;;
5)exit;;
*) echo "aRGH!";read;
esac

Hope you can help me with this.
Thank you and GOD Bless

MTK358 07-23-2011 10:34 AM

First, use code tags to post code.

Quote:

Originally Posted by Gfaith31 (Post 4423179)
$fnum >> PHONEBOOK;

This is wrong. You're running the command stored in "$fnum" and appending its output to the PHONEBOOK file. I assume you want to append the contents of the "$fnum" variable to the file. This is how:

Code:

echo "$fnum" >> PHONEBOOK
if [ yn == "y" -o yn == "Y" ]
then sed '0, $fdel' PHONEBOOK;
else createmenu
fi[/QUOTE]

This is not wrong, but it's quite ugly. It's a good idea to nicely format your code to make it readable:

Code:

if [ yn == "y" -o yn == "Y" ]
then
    sed '0, $fdel' PHONEBOOK;
else
    createmenu
fi

Finally, it looks like this is homework. Are you using bash, or just plain sh? If you're using bash, it has a lot of great syntax to make your script simpler and more readable.


All times are GMT -5. The time now is 05:28 AM.