LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 03-10-2009, 02:34 PM   #1
klss
LQ Newbie
 
Registered: Oct 2008
Distribution: Mint
Posts: 22

Rep: Reputation: 15
Scripting: How to verifiy if input matches a range of numbers?


Hi folks.

I am still trying to get myself into bash scripting and programming.
I guess below problem is a pretty simple issue for an experienced programmer.

How can I solve following issue? I'd like to check if an input fits
into a range of numbers, which are set by variables? I just couldn't figure it out by myself.

Below of course won't work. It's supposed to visualize what I would like to do.

THX


Code:
#!/bin/bash

number=0
rangestart=0
rangeend=100

while : ; do
read -p "Enter number: " number ; echo "$number"

case $number in
   [$rangestart-$rangeend]     ) echo "Hit"   ;;
                     *         ) echo "Blank" ;;
esac

done

exit 0
 
Old 03-10-2009, 02:44 PM   #2
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 121Reputation: 121
There are a few different ways to do this, but this seems easiest to me:
Code:
#!/bin/sh

typeset -i num
rangestart=0
rangeend=100

while [ 1 ]; do
   read -p "Enter number: " num ; echo "$num"

   if [ $num -ge $rangestart ] && [ $num -le $rangeend  ]; then
     echo "Hit"
   else
     echo "miss"
   fi
done
 
Old 03-11-2009, 03:44 AM   #3
klss
LQ Newbie
 
Registered: Oct 2008
Distribution: Mint
Posts: 22

Original Poster
Rep: Reputation: 15
Well - your answer implies that it's not possible to apply a "case" construct for above example.

I hoped for a different solution.

THX
 
Old 03-11-2009, 05:57 AM   #4
ak99505
Member
 
Registered: Feb 2005
Location: Missouri
Distribution: Archlinux
Posts: 85

Rep: Reputation: 17
Quote:
Originally Posted by klss View Post
Well - your answer implies that it's not possible to apply a "case" construct for above example.

I hoped for a different solution.

THX
in this example a case statement really wouldn't work, I suppose you could make it work, but that would
be a lot of effort.
 
  


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
To get a range of prime numbers som_kurian Programming 12 11-18-2009 05:15 AM
BASH : how to act based on test against a range of numbers hollywoodb Programming 4 07-06-2006 07:09 PM
Shell scripting - Random numbers within a range felixc Linux - Newbie 2 10-09-2005 06:41 PM
bash: routine outputting both matches and non-matches separately??? Bebo Programming 8 07-19-2004 07:52 AM
input signal out of range tadspurgeon Linux - Software 4 01-30-2004 12:50 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 03:25 AM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration