LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-05-2006, 06:44 PM   #1
hollywoodb
Member
 
Registered: Aug 2003
Location: Minnesota, U.S.A.
Distribution: Debian, openSUSE
Posts: 400

Rep: Reputation: 30
BASH : how to act based on test against a range of numbers


alright, I can set a file or a variable with a range of numbers...

for example: (either way is fine by me)

Code:
$ cat range.txt
63
88
99

$ echo $RANGE
63 88 99
I have a test number

Code:
$ echo $TEST
77
I want to run a command when the first number in $RANGE is greater than $TEST, and then stop the test on $RANGE. I DO NOT want the command to run on any number in $RANGE other than the first number greater than $TEST.

for example, (using static numbers for example, and keeping in mind that $TEST is 77 in this example)

Quote:
63 > $TEST (false, do nothing)
88 > $TEST (true, run command)
99 > $TEST (true, do nothing, since first correct conditional already meant and command already run)
or

Quote:
63 > $TEST (false, do nothing)
88 > $TEST (true, run command, and stop testing $RANGE vs $TEST)
 
Old 07-05-2006, 07:13 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,457
Blog Entries: 54

Rep: Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897
Code:
test="77"; range=($(cat range.txt)); for i in $(seq 0 $[${#range[@]}-1]); do
 [ "${range[$i]}" -gt "$test" ] && ( echo "running command"; break ) ; done
 
Old 07-06-2006, 03:51 AM   #3
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
Code:
(while read && [[ $REPLY -lt $(( $test + 1 )) ]]; do true; done; echo "\"$REPLY\" is the first number greater than $test or the last line." ) < range.txt
Edit: It seems like the brackets around 'echo "running command"; break' are causing trouble in unSpawn's suggestion (because they are forking a subshell)? See
Code:
while true; do (break); done
But the following works as desired:
Code:
test="77"; range=($(cat range.txt)); for i in $(seq 0 $[${#range[@]}-1]); do
 [ "${range[$i]}" -gt "$test" ] && echo "running command" && break; done

Last edited by spirit receiver; 07-06-2006 at 04:00 AM.
 
Old 07-06-2006, 05:10 AM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,457
Blog Entries: 54

Rep: Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897
Thanks for cleaning up again spirit receiver. I would prefer this below over "&&" since I'm not interested if echo fails or not, but that would be even less then trivial:
Code:
[ "${range[$i]}" -gt "$test" ] && { echo "running command"; break; }; done
 
Old 07-06-2006, 06:09 PM   #5
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
Quote:
Originally Posted by unSpawn
I'm not interested if echo fails or not
You're right, even if "echo" is not likely to fail, it seems reasonable to use your syntax in a situation where "echo" is replaced with a command that might fail.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Script to test dialup to about 100 diferent numbers Xtr3m3 Programming 1 06-06-2006 06:20 PM
Script to test dialup to about 100 diferent numbers Xtr3m3 Linux - Networking 2 06-06-2006 06:18 PM
Decimal numbers in bash script variables? Massif Programming 3 11-07-2005 09:01 PM
Shell scripting - Random numbers within a range felixc Linux - Newbie 2 10-09-2005 05:41 PM
URGENT NEED: floatting numbers in BASH shell grueugene Programming 5 03-12-2004 09:25 AM


All times are GMT -5. The time now is 10:02 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration