Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

validate number range

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 02-22-2005
sabina sabina is offline
Registered User
 
Join Date: Feb 2005
Last Activity: 4 March 2005, 3:01 PM EST
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
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
Sponsored Links
    #2  
Old Unix and Linux 02-22-2005
Perderabo's Unix or Linux Image
Perderabo Perderabo is offline Forum Staff  
Unix Daemon (Administrator Emeritus)
 
Join Date: Aug 2001
Last Activity: 27 April 2015, 10:26 AM EDT
Location: Ashburn, Virginia
Posts: 9,931
Thanks: 64
Thanked 449 Times in 264 Posts
You need to tell us which shell and what system. But for ksh this should work

print -n "enter number - "
read val
if [[ $val -ge 1 && $val -le 100 ]] ; then
Sponsored Links
    #3  
Old Unix and Linux 02-22-2005
sabina sabina is offline
Registered User
 
Join Date: Feb 2005
Last Activity: 4 March 2005, 3:01 PM EST
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
hi

I am using bourne shell. I used your script it works as long as I enter a number.

I want it to fail if I enter a string by mistake.

Thanks for your reply.

Sabina
    #4  
Old Unix and Linux 02-22-2005
bhargav's Unix or Linux Image
bhargav bhargav is offline Forum Advisor  
Registered User
 
Join Date: Sep 2004
Last Activity: 26 July 2012, 3:38 PM EDT
Location: USA
Posts: 512
Thanks: 0
Thanked 2 Times in 2 Posts

Code:
print -n "enter number - "
read val

echo $val | grep "[a-zA-Z]"

if test $? -ge 1
then
   if [[ $val -ge 1 && $val -le 100 ]] ; then
     print "OK"
   else
     print "NOT in 1-100 range"
   fi
else
  print "Not a number"

fi

The Following User Says Thank You to bhargav For This Useful Post:
LinuxRacr (05-20-2011)
Sponsored Links
    #5  
Old Unix and Linux 02-22-2005
zazzybob's Unix or Linux Image
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
 
Join Date: Dec 2003
Last Activity: 13 May 2013, 9:02 AM EDT
Location: Melbourne, Australia
Posts: 2,185
Thanks: 3
Thanked 25 Times in 23 Posts
A slight variation on bhargav's script to ensure that only numeric digits are passed, and symbols as well as letters are rejected....

Code:
#!/bin/sh

echo "Enter number"
read val

if echo $val | egrep '^[0-9]+$' >/dev/null 2>&1
then
  if [ $val -ge 1 -a $val -le 100 ]; then
     echo "OK"
  else
     echo "Out of range"
  fi
else
  echo "Not a number"
fi

exit 0

Cheers
ZB
The Following User Says Thank You to zazzybob For This Useful Post:
LinuxRacr (05-20-2011)
Sponsored Links
    #6  
Old Unix and Linux 02-23-2005
sabina sabina is offline
Registered User
 
Join Date: Feb 2005
Last Activity: 4 March 2005, 3:01 PM EST
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
hi Zb and bhargav

Thanks a lot for your time. It was really useful.

Sabina
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how can I detect number series range kocaturk Shell Programming and Scripting 8 12-05-2011 05:18 AM
Number range for SSNs lyoncc Shell Programming and Scripting 1 01-22-2011 11:12 AM
extract number range from a file jimmy_y Shell Programming and Scripting 1 06-03-2010 05:17 AM
PERL: Simple reg expr validate 6 digits number BufferExploder Shell Programming and Scripting 2 09-10-2008 11:15 AM
Cutting number from range in xml file nir_s Shell Programming and Scripting 4 07-25-2005 02:38 AM



All times are GMT -4. The time now is 10:22 AM.