I am new to bash shell writing and I need some help. I need to ask a user a name to search for. From the value the user has entered the information is pulled from a file called phonebook which displays the users name,address and telephone number. Then I have to give the option to give the user to type 'end' to quit. Here is my program can someone help me out.:rolleyes:


#!/bin/bash
#My first script program
phonebook = 0
name = $1

#Enters a name to search for end exits if the user types end
echo "Enter a name to search for (type end to exit)"
read 1

ls- l| grep phonebook "$1"

#This is a loop to make sure a name is entered
if [$1= ]; then
echo "Please enter in a name"
fi

exit 0

Recommended Answers

All 2 Replies

for starters, just do:
echo -n "Please enter the name you are looking for"
read name #this stores the name as the variable $name

then try
grep -i "$name phonebook #this will display the name from the phonebook

that should at least get you going. Ending it shouldn't be a problem.

bash only allows you to start variables with letters. You can have a number in the variable, but not at the begiinging. the varaiable d4='something' will be ok, but 4d='something' will not work. So you numbers 0 and 1 you used will not work

you want to use a while loop, which you have is not a loop. it will just test for something one time. Your if then is also a little off.

you have

if [$1= ]; then
echo "Please enter in a name"
fi

bash is kind of picky. you need to have a space on each side of the bracket. You also need to add quotes like this

if [ "$D" = "" ]; then
echo "Please enter a name"
fi

what you are looking for is a while loop(I think) This will promt for a answer if a name is not set to the variable. It will keep asking until the variable is set. The following code only gets a name from the user and sets it to the variable $NAME

echo "enter a name to search for"
read NAME

while [ "$NAME" = "" ]: do
echo "enter a name to search for"
read NAME
done

I am just a novice myself, but this is a pretty easy assignment you got .You definetly need to brush up on the basics. I would highly reccomend this onlline guide.
http://www.tldp.org/LDP/abs/html/

sections 2-10 will give you a good grasp of wht you need to know. after that this will be much easier for you.

Even if you do not want to read I am willing to help, I have fun doing it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.