Check if the input is number or text? | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Check if the input is number or text?

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-11-2009
waso's Avatar
waso waso is offline
Registered User
 
Join Date: Oct 2009
Last Activity: 2 August 2012, 4:29 AM EDT
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
CPU & Memory Check if the input is number or text?

i have to check if is input number or text, i case that is text then exit script

exaple is hrere, but is not working


Code:
check_it()
{
    if [ "$1" != +([a-zA-Z]) ]
        then        
        if (( $1 > 0 && $1 < 13 )); 
            then
                echo "input number is ok"
        else
                echo "number not ok"
                exit 1
        fi
    else
      echo "just numbers please"
      exit 1
    fi
}

case $1 in
        0*)             echo "sorry, no leading zeroes"
                        exit 1;;
        "")             echo "input parameter is required"
                        exit 1;;
esac

Sponsored Links
    #2  
Old 11-11-2009
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
who?
 
Join Date: Dec 2003
Last Activity: 27 November 2014, 11:18 AM EST
Location: /dev/ph
Posts: 4,837
Thanks: 64
Thanked 434 Times in 402 Posts
Not sure what you were trying to achieve with both the check_it function and the case statement. You can do it all using a case statement as shown in the following example

Code:
#!/usr/bin/bash

shopt -s extglob

INPUT="123456"

case $INPUT in
   ( +([[:digit:]]) )   echo "INPUT is all numbers" ;;
   *)                   echo "INPUT is not all numbers" ;;
esac

Sponsored Links
    #3  
Old 11-11-2009
waso's Avatar
waso waso is offline
Registered User
 
Join Date: Oct 2009
Last Activity: 2 August 2012, 4:29 AM EDT
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by fpmurphy View Post
Not sure what you were trying to achieve with both the check_it function and the case statement. You can do it all using a case statement as shown in the following example

Code:
#!/usr/bin/bash

shopt -s extglob

INPUT="123456"

case $INPUT in
   ( +([[:digit:]]) )   echo "INPUT is all numbers" ;;
   *)                   echo "INPUT is not all numbers" ;;
esac

users execute script like this:

Code:
/app/av/test/test.sh 12

in script i have 2 more script that will execute if the input parameter $1 is correct

first i must check if is $1 number and not text
second $1 must be number betwin 1-12, and cant have leading zeroes for number 1-9

Last edited by waso; 11-11-2009 at 11:17 AM..
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
[KSH] Check input numbers waso Shell Programming and Scripting 6 10-01-2009 05:44 PM
Bash : how do i check the user input and make sure is only chracter or only number ? CheeSen Shell Programming and Scripting 7 09-07-2008 08:06 AM
check the input Shilpi UNIX for Dummies Questions & Answers 1 03-04-2008 08:26 AM
Check on Input dhananjaysk Shell Programming and Scripting 3 03-13-2006 01:55 PM
What can i do to check that the input is all alphabet.. ? XXXXXXXXXX Shell Programming and Scripting 4 02-19-2002 07:09 PM



All times are GMT -4. The time now is 08:07 PM.