Sponsored Content
Top Forums UNIX for Beginners Questions & Answers Simple conditional yields too many responses Post 303030470 by Xubuntu56 on Monday 11th of February 2019 10:14:25 AM
Old 02-11-2019
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
 

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

ping to machine and count responses

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 ... (2 Replies)
Discussion started by: trostycp
2 Replies

2. Shell Programming and Scripting

How to automate responses

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... (2 Replies)
Discussion started by: djp
2 Replies

3. Linux

How do I capture responses from the chat command?

Unfortunately googling the word 'chat' gives you zebedee billion responses that relate to everything and few of them refer to the linux chat command. I've read the man page and found a couple of examples but can't see how to do this. I want to query the modem for it's manufacturer, product id... (8 Replies)
Discussion started by: Bashingaway
8 Replies

4. Shell Programming and Scripting

Using AWK to Calculate Correct Responses

Hello, I am trying to count how many times a subject makes a correct switch or a correct stay response in a simple task. I have data on which condition they were in (here, labeled "IMAGINE" and "RECALL"), as well as whether they made a left or right button response, and whether the outcome was... (5 Replies)
Discussion started by: Jahn
5 Replies

5. Shell Programming and Scripting

Simple awk conditional one-liner

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... (2 Replies)
Discussion started by: palex
2 Replies

6. Infrastructure Monitoring

SNMP responses failing under high system load

Greetings, I've got a Zenoss v2.5 server monitoring a large video encoding farm. Needless to say, these systems are under high bandwidth and CPU utilization the majority of the time. What I'm running into is that, occasionally, these systems will fail to respond to a standard SNMP request,... (1 Reply)
Discussion started by: Karunamon
1 Replies

7. IP Networking

DNS: Dig returns different responses...

Hey everyone, Okay, so I've been having some fun with the dig command, and wanted to dig my old school. Two questions came up from this. So I: dig @8.8.8.8 +recurse njcu.edu ANY and the result is about 8 records, including the SOA record. One of them is this weird TXT record, and the other is... (1 Reply)
Discussion started by: Lost in Cyberia
1 Replies

8. UNIX for Dummies Questions & Answers

Cp via NFS vs. scp yields unexpected difference

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... (1 Reply)
Discussion started by: crimso
1 Replies

9. Shell Programming and Scripting

Searching a file - and assigning responses to variables (or something)

So first: Sorry if the title is confusing... I have a script I'm writing with a file with several names in it (some other info - but it's not really pertinent...) - I want to be allow the user to delete certain records, but I ran into a problem I'm not sure how to go about fixing. If I were... (6 Replies)
Discussion started by: sabster
6 Replies
Sort::Key::Top(3pm)					User Contributed Perl Documentation				       Sort::Key::Top(3pm)

NAME
Sort::Key::Top - select and sort top n elements SYNOPSIS
use Sort::Key::Top (nkeytop top); # select 5 first numbers by absolute value: @top = nkeytop { abs $_ } 5 => 1, 2, 7, 5, 5, 1, 78, 0, -2, -8, 2; # ==> @top = (1, 2, 1, 0, -2) # select 5 first numbers by absolute value and sort accordingly: @top = nkeytopsort { abs $_ } 5 => 1, 2, 7, 5, 5, 1, 78, 0, -2, -8, 2; # ==> @top = (0, 1, 1, 2, -2) # select 5 first words by lexicographic order: @a = qw(cat fish bird leon penguin horse rat elephant squirrel dog); @top = top 5 => @a; # ==> @top = qw(cat fish bird elephant dog); DESCRIPTION
The functions available from this module select the top n elements from a list using several common orderings and custom key extraction procedures. They are all variations around keytopsort { CALC_KEY($_) } $n => @data; In array context, this function calculates the ordering key for every element in @data using the expression inside the block. Then it selects and orders the $n elements with the lower keys when compared lexicographically. It is equivalent to the pure Perl expression: (sort { CALC_KEY($a) cmp CALC_KEY($b) } @data)[0 .. $n-1]; If $n is negative, the last $n elements from the bottom are selected: topsort 3 => qw(foo doom me bar doz hello); # ==> ('bar', 'doz', 'doom') topsort -3 => qw(foo doom me bar doz hello); # ==> ('foo', 'hello', 'me') top 3 => qw(foo doom me bar doz hello); # ==> ('doom', 'bar', 'doz') top -3 => qw(foo doom me bar doz hello); # ==> ('foo', 'me', 'hello') In scalar context, the value returned by the functions on this module is the cutoff value allowing to select nth element from the array. For instance: # n = 5; scalar(topsort 5 => @data) eq (sort @data)[4] # true # n = -5; scalar(topsort -5 => @data) eq (sort @data)[-5] # true Note that on scalar context, the "sort" variations (see below) are usually the right choice: scalar topsort 3 => qw(me foo doz doom me bar hello); # ==> 'doz' scalar top 3 => qw(me foo doz doom me bar hello); # ==> 'bar' Note also, that the index is 1-based (it starts at one instead of at zero). The "atpos" set of functions explained below do the same and are 0-based. Variations allow to: - use the own values as the ordering keys topsort 5 => qw(a b ab t uu g h aa aac); # ==> a aa aac ab b - return the selected values in the original order top 5 => qw(a b ab t uu g h aa aac); # ==> a b ab aa aac - use a different ordering For instance comparing the keys as numbers, using the locale configuration or in reverse order: rnkeytop { length $_ } 3 => qw(a ab aa aac b t uu g h); # ==> ab aa aac rnkeytopsort { length $_ } 3 => qw(a ab aa aac b t uu g h); # ==> aac ab aa A prefix is used to indicate the required ordering: (no prefix) lexicographical ascending order r lexicographical descending order l lexicographical ascending order obeying locale configuration r lexicographical descending order obeying locale configuration n numerical ascending order rn numerical descending order i numerical ascending order but converting the keys to integers first ri numerical descending order but converting the keys to integers first u numerical ascending order but converting the keys to unsigned integers first ru numerical descending order but converting the keys to unsigned integers first - select the head element from the list sorted nhead 6, 7, 3, 8, 9, 9; # ==> 3 nkeyhead { length $_ } qw(a ab aa aac b t uu uiyii) # ==> 'a' - select the tail element from the list sorted tail qw(a ab aa aac b t uu uiyii); # ==> 'uu' nkeytail { length $_ } qw(a ab aa aac b t uu uiyii) # ==> 'uiyii' - select the element at position n from the list sorted atpos 3, qw(a ab aa aac b t uu uiyii); # ==> 'ab'; rnkeyatpos { abs $_ } 2 => -0.3, 1.1, 4, 0.1, 0.9, -2; # ==> 1.1 rnkeyatpos { abs $_ } -2 => -0.3, 1.1, 4, 0.1, 0.9, -2; # ==> -0.3 Note that for the atpos set of functions indexes start at zero. - return a list composed by the elements with the first n ordered keys and then the remaining ones. ikeypart { length $_ } 3 => qw(a bbbb cc ddddd g fd); # ==> a cc g bbbb ddddd fd - return two arrays references, the first array containing the elements with the first n ordered keys and the second with the rest. keypartref { length $_ } 3 => qw(a bbbb cc ddddd g fd); # ==> [a cc g] [bbbb ddddd fd] The full list of available functions is: top ltop ntop itop utop rtop rltop rntop ritop rutop keytop lkeytop nkeytop ikeytop ukeytop rkeytop rlkeytop rnkeytop rikeytop rukeytop topsort ltopsort ntopsort itopsort utopsort rtopsort rltopsort rntopsort ritopsort rutopsort keytopsort lkeytopsort nkeytopsort ikeytopsort ukeytopsort rkeytopsort rlkeytopsort rnkeytopsort rikeytopsort rukeytopsort head lhead nhead ihead uhead rhead rlhead rnhead rihead ruhead keyhead lkeyhead nkeyhead ikeyhead ukeyhead rkeyhead rlkeyhead rnkeyhead rikeyhead rukeyhead tail ltail ntail itail utail rtail rltail rntail ritail rutail keytail lkeytail nkeytail ikeytail ukeytail rkeytail rlkeytail rnkeytail rikeytail rukeytail atpos latpos natpos iatpos uatpos ratpos rlatpos rnatpos riatpos ruatpos keyatpos lkeyatpos nkeyatpos ikeyatpos ukeyatpos rkeyatpos rlkeyatpos rnkeyatpos rikeyatpos rukeyatpos part lpart npart ipart upart rpart rlpart rnpart ripart rupart keypart lkeypart nkeypart ikeypart ukeypart rkeypart rlkeypart rnkeypart rikeypart rukeypart SEE ALSO
Sort::Key, "sort" in perlfunc. The Wikipedia article about selection algorithms <http://en.wikipedia.org/wiki/Selection_algorithm>. COPYRIGHT AND LICENSE
Copyright (C) 2006-2008, 2011 by Salvador Fandin~o (sfandino@yahoo.com). This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available. perl v5.14.2 2011-06-17 Sort::Key::Top(3pm)
All times are GMT -4. The time now is 08:44 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy