var googletag = googletag || {}; googletag.cmd = googletag.cmd || []; (function() { var gads = document.createElement('script'); gads.async = true; gads.type = 'text/javascript'; var useSSL = 'https:' == document.location.protocol; gads.src = (useSSL ? 'https:' : 'http:') + '//www.googletagservices.com/tag/js/gpt.js'; var node = document.getElementsByTagName('script')[0]; node.parentNode.insertBefore(gads, node); })();

  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.

Check if the input is number or text?

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 11-11-2009
waso's Unix or Linux Image
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 Unix and Linux 11-11-2009
fpmurphy's Unix or Linux Image
fpmurphy fpmurphy is offline Forum Staff  
who?
 
Join Date: Dec 2003
Last Activity: 12 June 2016, 11:03 PM EDT
Location: /dev/ph
Posts: 4,996
Thanks: 73
Thanked 475 Times in 437 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 Unix and Linux 11-11-2009
waso's Unix or Linux Image
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 10:17 AM..
Sponsored Links
Closed

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
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 07:06 AM
check the input Shilpi UNIX for Dummies Questions & Answers 1 03-04-2008 07:26 AM
Check on Input dhananjaysk Shell Programming and Scripting 3 03-13-2006 12:55 PM
What can i do to check that the input is all alphabet.. ? XXXXXXXXXX Shell Programming and Scripting 4 02-19-2002 06:09 PM



All times are GMT -4. The time now is 02:39 AM.