Simple conditional yields too many responses


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Old 1 Week Ago
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  
Old 1 Week Ago
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; 1 Week Ago at 11:23 AM..
This User Gave Thanks to vbe For This Post:
Xubuntu56 (1 Week Ago)
# 3  
Old 1 Week Ago
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:
Xubuntu56 (1 Week Ago)
# 4  
Old 1 Week Ago
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:
Xubuntu56 (1 Week Ago)
# 5  
Old 1 Week Ago
@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  
Old 1 Week Ago
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:
Xubuntu56 (1 Week Ago)
# 7  
Old 1 Week Ago
Code:
     if [[ "$REPLY" = "$i" ]]; then
             echo "Yes, that is a bird."
             exit
     fi

This User Gave Thanks to vgersh99 For This Post:
Xubuntu56 (1 Week Ago)
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
Searching a file - and assigning responses to variables (or something) sabster Shell Programming and Scripting 6 05-11-2016 02:05 AM
awk output yields error: awk:can't open job_name (Autosys) alexcol Shell Programming and Scripting 18 02-08-2015 03:29 PM
Cp via NFS vs. scp yields unexpected difference crimso UNIX for Dummies Questions & Answers 1 02-05-2015 04:22 PM
DNS: Dig returns different responses... Lost in Cyberia IP Networking 1 01-29-2014 10:29 AM
exec perl in expect script yields "invalid command" dpouliot Shell Programming and Scripting 4 01-12-2012 10:23 AM
SNMP responses failing under high system load Karunamon Infrastructure Monitoring 1 11-03-2011 01:00 PM
Simple awk conditional one-liner palex Shell Programming and Scripting 2 09-19-2011 10:50 AM
Using AWK to Calculate Correct Responses Jahn Shell Programming and Scripting 5 05-12-2011 04:50 AM
Awk Script Counting of Correct vs. Error Responses Jahn Shell Programming and Scripting 6 02-28-2011 05:52 PM
How do I capture responses from the chat command? Bashingaway Linux 8 02-14-2011 11:54 AM
Extracting Latency, Loss and Jitter from PING responses. tony.kandaya Shell Programming and Scripting 5 12-06-2008 08:51 AM
No Responses from ALOM using NET MGNT PORT on SunFire v245 glieu UNIX for Dummies Questions & Answers 1 12-02-2008 12:18 AM
How to automate responses djp Shell Programming and Scripting 2 06-07-2005 05:00 PM
ping to machine and count responses trostycp UNIX for Dummies Questions & Answers 2 02-06-2004 12:54 PM