Computing.Net > Forums > Unix > if statement problem

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

if statement problem

Reply to Message Icon

Name: fanco
Date: October 16, 2007 at 12:03:08 Pacific
OS: xp
CPU/Ram: 512
Product: hp
Comment:

can anybody help me to fix my code i am trying to get default out put else "name is NOT listed" but evey time I run the program i cant get it
thank u
============================================


echo "Enter the first letter of the name to look up, or q to quit"
read name

while [ $name != "q" ]

do
echo "Do you want to list all names start with $name {y|n} ?"
read choice

if [ $choice = "y" ]
then
grep -i "^$name" /home/students/atammf/lab1/phonebook.list;

elif [ $choice = "n" ]
then
echo "Enter the full last name to look up: "
read name1
grep -i -w "$name1" /home/students/atammf/lab1/phonebook.list;

else
echo "Name is NOT Listed"

fi

exit 0
done



Sponsored Link
Ads by Google

Response Number 1
Name: fpmurphy
Date: October 16, 2007 at 17:30:47 Pacific
Reply:

Try the following syntax if you are using a
POSIX or Korn shell

if [[ $choice = "y" ]]

i.e. [[ ..]] instead of [ .. ]

If you are using csh/tcsh or similar, try

if [ "$choice" = "y" ]


0

Response Number 2
Name: memyselfandadi
Date: October 29, 2007 at 09:44:52 Pacific
Reply:

Hi Fanco,
One Basic thing I see is,
while comparing strings on UNIX, its always a good idea to cover it with "
like this
while [ "$name" != "q" ]; then...
In the script that you have mentioned, the fact that $name is empty will transfer your script to something like this
while [ != "q" ] and will result in a syntax error.
Hope this helps.
--

MeMyselfAndAdi


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: if statement problem

Test sed output using IF statement www.computing.net/answers/unix/test-sed-output-using-if-statement/7299.html

if statement www.computing.net/answers/unix/if-statement/6676.html

While read is ignoring if statement www.computing.net/answers/unix/while-read-is-ignoring-if-statement/7435.html