checking a variable is within range


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting checking a variable is within range
# 1  
Old 08-17-2006
checking a variable is within range

how can i check that a variable is between 0-100, like if i ask a user to input a number between 1-100 and i want to excute commands WHILE that number is between that range or else i will keep asking the user to make another input

here's what i got
Code:
#!/bin/bash
echo "Guess my secret number (0-100): "
number=$((RANDOM%100+0))
echo "$number"
read GUESS

I've tried all sorts of things but I keep getting errors, i tried:
Code:
if echo $GUESS | grep "^[0-9]*$">aux
then
  echo "good"
else
  echo "bad"
fi
rm aux

and i've tried
Code:
if [ -n "$( print - "$GUESS"           |\
            sed 's/^[+-]//;s/[0-9]//g;s/\.//'   \
          )" ] ; then
     echo "good"
else
     echo "bad"
fi

i'm always getting errors like command not found and unexpected end of file, can anyone pls help me?
# 2  
Old 08-17-2006
This will validate a number between 0 and 100 both inclusive.

Code:
[/tmp]$ cat try.sh
#! /bin/sh

echo "Guess my secret number (0-100): "
read GUESS

if [[ $GUESS -ge 0 && $GUESS -le 100 ]] ; then 
  echo "good"
else
  echo "bad"
fi

# 3  
Old 08-17-2006
Thanks ^_^
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed variable expansion fails for substitution in range

I'm trying to change "F" to "G" in lines after the first one: 'FUE.SER' 5 1 1 F0501 F0401 F0502 2 1 F0301 E0501 F0201 E0502 F0302 3 1 F0503 E0503 E0301 E0201 E0302 E0504 F0504 4 1 F0402 F0202 E0202 F0101 E0203 F0203 F0403 5 1 F0505 E0505 E0303 E0204 E0304 E0506... (10 Replies)
Discussion started by: larrl
10 Replies

2. Shell Programming and Scripting

Checking range of ips

Given a range of IPs similar to this: "212.63.183.19","212.63.183.19","3560945427","3560945427","CN","China" "217.7.143.0","217.7.143.0","3641151232","3641151232","CN","China" "218.0.0.0","218.31.255.255","3657433088","3659530239","CN","China"... (13 Replies)
Discussion started by: SkySmart
13 Replies

3. Shell Programming and Scripting

Checking the variable in UNIX

Hi, I have a file abc.txt as ABC,TYU,1.2566 AHG,GJJ,1.898 hgh,FGA,1.854 My program is reading each line and storing the values variables base_cy, quo_cy, ra_amt Need to validate each of them as in: base_cy and quo_cy should be a 3 character alphabet among A-Z, if it is lower case ... (1 Reply)
Discussion started by: infyanurag
1 Replies

4. Shell Programming and Scripting

use variable to set the range of a for loop

Hi; For sure there's an easy answer to this one that I am not finding.. I first set a variable, say b1a:] max=5 then I want to use max to set the range for a for loop like so (it should run for i in 1:5) b1a:] for i in {1..$max}; do echo $i; done {1..5} I would like the output... (2 Replies)
Discussion started by: jbr950
2 Replies

5. UNIX for Dummies Questions & Answers

Checking the language variable

This is what I have just entered and received in return: # echo $LANG LANG: Undefined variable. How can this be? I was expecting something like en_US.utf8. Am using FreeBSD 8.2. (2 Replies)
Discussion started by: figaro
2 Replies

6. Shell Programming and Scripting

checking variable has value - ksh

hi all, i have a shell (ksh) script where i am reading line by line from a grep command and i wanted to check if the grep command actually returned something or was null. i can do this by using -z : if ; then ..... but this forces me to do something when $myVariable is null when i... (3 Replies)
Discussion started by: cesarNZ
3 Replies

7. Shell Programming and Scripting

Perl search and replace in range using variable

Hi. I have a file with asterisk field separators and backslash line terminators. The first field in each line names the line type. I am trying to process each range separately. Here's what the data looks like: BA*DATA\ LS*DATA1*DATA2*00020*\ TA*DATA1*DATA2*DATA3*\ TA*DATA1*DATA2*DATA3*\... (1 Reply)
Discussion started by: yoi2hot4ya
1 Replies

8. Shell Programming and Scripting

checking the value of the variable

Does anyone know the quick way to check if the variable contains only numeric characters, for example: A=123445 - correct B=#f123* - incorrect I am in ksh88i Thanks a lot for help -A (1 Reply)
Discussion started by: aoussenko
1 Replies

9. UNIX for Dummies Questions & Answers

ksh Checking if variable has 5 digits

How could I check if a numeric variable has 5 digits in KSH...I have a zipcode variable that I know will always be 5 digits, and I want to print out an error if it is less or more than 5 digits the problem is that I have it as: if ] but this won't work because the statement doesn't see 0001 as... (3 Replies)
Discussion started by: developncode
3 Replies

10. Shell Programming and Scripting

Eliminate variable checking in a script

RH Linux, $SHELL=/bin/ksh I have a .profile which I source in as such --> . .profile Whats happening is the variables are getting validated and generating errors. for example .profile export foo=/to/the/moon when I . .profile , I get : not foundmyusername/.profile or bad... (8 Replies)
Discussion started by: BMetelsky
8 Replies
Login or Register to Ask a Question