Simple conditional yields too many responses


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Simple conditional yields too many responses

In this script:
Code:
#!/bin/bash
# bird
read -p "Enter name of a bird   "
REPLY=$REPLY
birdname="duck sparrow hawk"
for i in $birdname
do
        if [[ "$REPLY" = "$i" ]]
        then
                echo "Yes, that is a bird."
        else
                echo "That is not a bird."
        fi
done

I get this:
Code:
~/bin$ bird
Enter name of a bird   duck
Yes, that is a bird.
That is not a bird.
That is not a bird.

Ubuntu 18.04.2; Xfce 4.12.3; kernel 4.15.0-45-generic; bash 4.4.19(1); Dell Inspiron-518
# 2  
If you find a match to your IF condition, you should exit otherwise you continue in the loop which explains why you have those lines, I let you figure out what to do the the other case, because you could have the case of nothing found and again you would have so many lines...
Hint: Use a variable you set and use it at the end once out of the loop...

Last edited by vbe; 02-11-2019 at 11:23 AM..
This User Gave Thanks to vbe For This Post:
# 3  
Your 'for' is going to create three data passes - one for each element in birdname.
Thus, correct in giving three results.
What were you hoping for, or expecting?
This User Gave Thanks to joeyg For This Post:
# 4  
Can I presume that if you enter hawk then the output you get is:-
Code:
~/bin$ bird
Enter name of a bird   hawk
That is not a bird.
That is not a bird.
Yes, that is a bird.

You code is doing what you are asking it to, but the issue (I presume) is a logical one.

You are checking if your input matches each of the items in turn, but you display the message every time. It would be better to define a flag/variable showing a failure before your loop then set the flag to a success within the loop if you meet the criteria. Then after the loop completes, you can display a message based on the flag.

Does that make sense? Sorry if it rather wordy.
Post your thoughts/next attempts and we can help with adjustments.


Kind regards,
Robin
This User Gave Thanks to rbatte1 For This Post:
# 5  
@joeyg--I wanted a single response, either "Yes, that is a bird." or "That is not a bird."
@vbe--I tried something else, but unfortunately it appears I need a stronger hint! Now, it works correctly for "duck", but does nothing for the other two birds, nor does it do anything for an incorrect response.
Code:
#!/bin/bash
# bird
read -rp "Enter name of a bird   "
REPLY=$REPLY
birdname="duck sparrow hawk"

for i in $birdname
do
     if [[ "$REPLY" = "$i" ]]; then
             echo "Yes, that is a bird."
     fi
     exit
     if [[ ! "$REPLY" = "$i" ]]; then
         echo "That is not a bird."
      fi
done

results:

Code:
:~/bin$ bird
Enter name of a bird   duck
 Yes, that is a bird.


:~/bin$ bird
 Enter name of a bird   sparrow
:~/bin$ 


:~/bin$ bird
  Enter name of a bird   hawk
:~/bin$ 


:~/bin$ bird
 Enter name of a bird    spaniel
:~/bin$

# 6  
Then you want something closer to:

Code:
for i in $birdname
do
        if [[ "$REPLY" = "$i" ]]
        then
                Found = Found + 1
        else
                NotFound = NotFound +1
        fi
done

if $Found > 0
   then 
      echo "Yes, that is a bird."
   else
      echo "No, not a bird."
fi

This User Gave Thanks to joeyg For This Post:
# 7  
Code:
     if [[ "$REPLY" = "$i" ]]; then
             echo "Yes, that is a bird."
             exit
     fi

This User Gave Thanks to vgersh99 For This Post:
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Cp via NFS vs. scp yields unexpected difference
crimso
I have two Linux machines, Linux1 and Linux2. They both have two NFS mounts. We'll call them /scratch1 and /scratch2. And they both reside on the same NetApp filer. If I copy a 512Mb file from /scratch1 to /scratch2 while logged on Linux1 it takes 40s. However if I scp this file from...... UNIX for Dummies Questions & Answers
1
UNIX for Dummies Questions & Answers
Simple awk conditional one-liner
palex
Hello, I'm looking for an awk one-liner that prints the first two data fields, then contains a conditional where if $3>$4, it prints $3-$4. Otherwise, it prints $3. Example: Data file: 123,456,999,888 333,222,444,555 654,543,345,888 444,777,333,111 Output: 123,456,111 333,222,444...... Shell Programming and Scripting
2
Shell Programming and Scripting
How to automate responses
djp
I would have searched for this but I couldn't really think of what to use for the search text... I've got a situation where I need to automate responses to an executable when running it from a script so that it can be made into a job the operators don't have to interact with. When I run it...... Shell Programming and Scripting
2
Shell Programming and Scripting
ping to machine and count responses
trostycp
if i wanted to ping all the machines in a given directory (/etc/hosts) and return a total count of responses how would i go about scripting that? complete newbie...so be gentle if ; then //$1 = /etc/hosts cd "$1" //this puts me into the directory i need...but how do i send ...... UNIX for Dummies Questions & Answers
2
UNIX for Dummies Questions & Answers

Featured Tech Videos