If statement test against number range [0-9]


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting If statement test against number range [0-9]
# 1  
Old 03-13-2013
If statement test against number range [0-9]

Is it possible to test against a varible within a ranges in a if statement.

ex.
Code:
 if [ $test != [0-9] ];then
echo "not in range"
else
echo "number within range"
f

i

Last edited by vbe; 03-13-2013 at 01:34 PM..
# 2  
Old 03-13-2013
read about the relational operators and boolean operators in shell ( google it )
This User Gave Thanks to itkamaraj For This Post:
# 3  
Old 03-13-2013
Hi, is this what you need?
Code:
if [[ $test -ge $LOWER ]] && [[ $test -le $UPPER ]]; then echo "in range"; else echo "not in range"; fi

# 4  
Old 03-13-2013
@franzpizzo: if LOWER is less or equal 0, and test is alpha, it will be considered "in range"!
This User Gave Thanks to RudiC For This Post:
# 5  
Old 03-13-2013
@RudiC
Hi, queston is: the request is a validation check or only a check for the range?
This check have not sense for alphanumeric value of test variable.
Thanks
This User Gave Thanks to franzpizzo For This Post:
# 6  
Old 03-13-2013
I'm using it to validate a numer range because it a number is chose outside of the range I and exiting the program
# 7  
Old 03-13-2013
You were pretty close with your original code, just use double [ and ] :

Code:
if [[ "$test" != [0-9] ]];then
   echo "not in range"
else
   echo "number within range"
fi

This User Gave Thanks to Chubler_XL For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Range of number from 0.1 to 10.0

Is there a way to create a loop that will output number starting from 0.1 to 10.0 0.1 0.2 0.3 0.4 0.5 .. ... 10.0 This is what i tried. for i in {1..50}; do printf -v i '%02d' $i ; echo "$i"; done That will print 01 02 03 .. .. 50 (9 Replies)
Discussion started by: vietrice
9 Replies

2. Shell Programming and Scripting

Number range for SSNs

Hi All. I have a file that has an ID Number field....some of the ID Numbers are actual SSNs. ...does anyone know the range that SSNs may be...this is what I have found so far poking around SSN info sites.... greater than 001-01-0000 and less than 770-00-0000. Does anyone know this to be... (1 Reply)
Discussion started by: lyoncc
1 Replies

3. Shell Programming and Scripting

Help with test statement

Hello, I am trying to build a test statement but I can't make it work I want to rearrange some fields, so if my "$cfg" variable contains a string ending with .log (*.log) I want to move it in another field. Any help will be much appreciated! Thank you Shell:sh if then log="${cfg}"... (9 Replies)
Discussion started by: drbiloukos
9 Replies

4. Shell Programming and Scripting

Bash script to test IP range on server

Hello, We have to configure servers with a range of IPs which is in itself a subject for another script assistance request -but- we have run into quite a few IP ranges with routing problems lately. I've been trying to figure out the best way to test a range of IPs, I mean, manually it's not... (4 Replies)
Discussion started by: boxgoboom
4 Replies

5. Shell Programming and Scripting

string test in IF statement

How do I test multiple words in a string test like below: if ] then print "You entered $TBS name.\n" else print "You entered an incorrect response.\n" fi This test does not work. I have tried different syntax versions. How does this work? And is there a better way to do it? ... (10 Replies)
Discussion started by: djehresmann
10 Replies

6. Shell Programming and Scripting

Perl - automating if statement test

Hello all, I'm trying to automate an if statement in my Perl script. The script opens an input file for reading, checks each line in the file for a particular substring, and if it finds the substring, writes it to an output file. There are approximately 200 different input files. Each has... (3 Replies)
Discussion started by: Galt
3 Replies

7. UNIX for Dummies Questions & Answers

Using a case statement with a range of numbers

Hey guys, I'm trying to setup a case statement that checks a value against 5 separate ranges of numbers. Here are the things I've tried with no success. case "$AGE" in "<10") echo "You're in this decade.";; "") echo "You're in this decade.";; "") echo "You're in... (5 Replies)
Discussion started by: fufaso
5 Replies

8. UNIX for Dummies Questions & Answers

if test statement

Can you use an if statement after an else? example if then echo "word" else if then echo "word" (1 Reply)
Discussion started by: skooly5
1 Replies

9. Shell Programming and Scripting

Using grep in a test/if statement

Okay, well this is more or less my first attempt at writing a shell script. Anyways, here's my code: cd ${PATH} if then rm ${FILE} ./anotherScript else exit 1 fi exit 1 Anyways, it's a pretty simple script that is supposed to search for the... (4 Replies)
Discussion started by: cbo0485
4 Replies

10. Shell Programming and Scripting

validate number range

Hi If I want to read user input and want to validate if it is a numeric number in some range of 1-100 what would be the best way? Sabina (5 Replies)
Discussion started by: sabina
5 Replies
Login or Register to Ask a Question