Integer check (again)...


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Integer check (again)...
# 1  
Old 03-02-2009
Integer check (again)...

I have search the forum for an easier way to write this code. I have two separate 'if' to do this and it works but am wondering if someone knows a quick way to combine them. I want anything between 1 and 100 but not '01' or '005', '0010', etc.

Code:
     

if [[ $myvar != +([0-9]) ]] ||
   [[ $myvar -le 0 || $myvar -gt 100 ]]; then
   echo "Try again"
fi

After that check, I do another check with a grep to make sure it doesn't start with a zero (so this is the 'else' block of the above code):

Code:
if echo $myvar | grep '^[1-9][0-9]*'; then 
   echo "Cool"
fi

Any thoughts?
# 2  
Old 03-02-2009
Try this :

Code:
while read myvar
do
   if [[ $myvar -eq 0 ]] || [[ ${myvar%"${myvar#?}"} != 0 && $myvar -gt 0 && $myvar -le 100 ]]
   then
      echo "Ok"
   else
      echo "Not Ok, try again" 
   fi
done

# 3  
Old 03-02-2009
Code:
#!/bin/ksh

case ${myvar} in
   @(0*|*[!0-9]*)    ) echo 'bad' ;;
   @([0-9]|[0-9][0-9]|100) ) echo 'good' ;;
                    *) echo 'bad' ;;
esac

# 4  
Old 03-02-2009
Thank you. These are simpler than what I have. Both solutions worked. The only caveat is that the first solution can case the program to terminate if a user input happens to include any non-numeric character. If you can control that then it's fine.

The second solution covers all basis as far as my testing went.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Integer Validation

Hi Using the below logic to check whether the value is integer or not and converting the value to decimal if not. int_chck = sprintf(substr($i, 1, 2)) o_cnt = 'if ; then echo $int_chck; else echo $((0x$int_chck)); fi' Thanks, Dines (4 Replies)
Discussion started by: dineshnak
4 Replies

2. Shell Programming and Scripting

While Loop to Check for Integer

Hello All, In ksh I am trying to ensure that user inputs are integers otherwise redisplay prompt with the following code; a=0 b=0 c=0 read b?"Please enter a number: " read c?"Please enter another number: " while ] && ]; do read b?"Not a number. Please enter a number: " read c?"Not... (4 Replies)
Discussion started by: techieg
4 Replies

3. Shell Programming and Scripting

how to compare string integer with an integer?

hi, how to I do this? i="4.000" if ; then echo "smaller" fi how do I convert the "4.000" to 4? Thanks! (4 Replies)
Discussion started by: h0ujun
4 Replies

4. Shell Programming and Scripting

Convert to Integer

Hi fellows!! i'm doing something which is not working out for me properly which i don't understand why nowdate=`date +%s` echo $nowdate now the problem how to convert a date which is stored in a variable mydate="22/Oct/2011" mydate=`date -d '$mydate' +%s` it gives error... (11 Replies)
Discussion started by: me_newbie
11 Replies

5. Shell Programming and Scripting

Alphanumeric to integer

Hi folks, I have a value like A12,i could able to change this into integer using typeset as below typeset -i A12 But, I need your advice to change the values like 1A2 or 12A into integer. Thanks in advance. Thanks, Sathish (3 Replies)
Discussion started by: bsathishmca
3 Replies

6. Shell Programming and Scripting

Regex for for integer from 0-7 except 2

Hi, I am trying to use a regular exp to match any number between 0 and 7 except 2...... I am using: echo $myvar | egrep "{1}{1}" Which isnt working.... Please can someone help? Thanks yonderboy (10 Replies)
Discussion started by: yonderboy
10 Replies

7. UNIX for Dummies Questions & Answers

Check if input is an integer or a floating point?

Hiii I actually intent to check the integer or floating point number input by user i.e. 23, 100, 55.25, 12.50 ..etc. However, when someone input strings or alpha character, my program has to show invalid input.!! Is there any Unix shell script syntax can help me to check ? Thanking you (2 Replies)
Discussion started by: krishnampkkm
2 Replies

8. UNIX for Dummies Questions & Answers

integer to string

Hi all, is there an easy way to convert integer to string in bash? I have numbers like 1, 2, ..., 112, ... and I would like to get 001 002 003 004 ... Thank you, Sarah (4 Replies)
Discussion started by: f_o_555
4 Replies

9. UNIX for Dummies Questions & Answers

Rouding off an integer

HI I want to round off an integer to the next multiple of 10 in shell script. (i.e.,) 91 should be rounded off to 100 and 90 should be rounded off to 90 It would be very helpful, if you can help me in this. Thanks in advance (4 Replies)
Discussion started by: dayamatrix
4 Replies

10. Shell Programming and Scripting

Cannot store integer value

Hi , I have code like below in my ksh script, but getting an error as SP2-0253: data item 1 ("SAMPLE_ID") will not fit on line , pls help me. thanks. if (( CHECKS == 0 )) || (( CHECKS == 1 )) then V_SAMPLE_ID=$( $ORACLE_HOME/bin/sqlplus -S / <<EOF whenever sqlerror exit 1... (5 Replies)
Discussion started by: bennichan
5 Replies
Login or Register to Ask a Question