The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
parameters jaay Shell Programming and Scripting 4 04-14-2008 01:10 AM
sed and parameters scotty_123 Shell Programming and Scripting 7 03-26-2007 01:22 AM
Max NO of parameters Shivdatta Shell Programming and Scripting 1 07-24-2006 05:11 AM
Need Parameters Help. james2006 Shell Programming and Scripting 3 06-08-2006 07:46 AM
tar parameters kmar UNIX for Advanced & Expert Users 4 10-23-2001 12:03 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 07-09-2006
Registered User
 

Join Date: Jul 2006
Posts: 9
parameters

I have a script that needs to check if the given parameters are a combination of 0123456789 and not a word or another irelevant character.please help
Reply With Quote
Forum Sponsor
  #2  
Old 07-09-2006
Hitori's Avatar
Registered User
 

Join Date: Jun 2006
Posts: 360
Code:
#!/bin/sh

for vi in "$@"
do
    if [ "`echo $vi | sed 's/^[0-9]*$//'`" != "" ]; then
        echo "ERROR: invalid parameter: "$vi;
    fi
done
Reply With Quote
  #3  
Old 07-09-2006
reborg's Avatar
Administrator
 

Join Date: Mar 2005
Location: Ireland
Posts: 3,638
bash/ksh

Code:
a="$*"
if [[ ${#a} -eq $(expr "$a" : '[0-9 ]*') ]] ; then
        echo numbers
else
        echo non-number
fi
Reply With Quote
  #4  
Old 07-10-2006
aigles's Avatar
Registered User
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,212
Another solution (for KSH)

Code:
for arg in "$@"
do
   if [[ "$arg" = +([0-9]) ]]
   then
      echo "Valid parameter <$arg>"
   else
      echo "Invalid parameter <$arg>"
   fi
done

Jean-Pierre.
Reply With Quote
  #5  
Old 07-10-2006
Registered User
 

Join Date: Jul 2006
Posts: 9
is there anything for tcsh please?
Reply With Quote
  #6  
Old 07-10-2006
reborg's Avatar
Administrator
 

Join Date: Mar 2005
Location: Ireland
Posts: 3,638
I'm really not a fan of tcsh, but anyway:

Code:
#!/bin/tcsh

set a = "$*"
if ( `expr "$a" : '.*'` == `expr "$a" : '[0-9 ]*'` ) then
    echo numbers
else
    echo non-number
endif
Reply With Quote
  #7  
Old 07-10-2006
Registered User
 

Join Date: Jun 2006
Location: Delhi, India
Posts: 88
Quote:
Originally Posted by aekaramg20
I have a script that needs to check if the given parameters are a combination of 0123456789 and not a word or another irelevant character.please help
Another method(using awk):

for var in $@; do
if [[ `echo $var | awk -v var=${var} 'BEGIN{if(var!~/[[:digit:]]/)print 1;}'` -eq 1 ]]; then
echo "Not a number"
exit 0
fi
done
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 12:22 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0