I have a csh script that gets its argument from the command line
and I need to check that it is a number (can contain more then one
digit) :
#/usr/bin/csh
if ( $1 contains characters that are not 0-9 ) then
echo Enter a nubmer
exit(1)
endif
...
How do I check the argument validity?
TIA , Eran.
--
---------------------------------------
Eran Harel , VLSI
dept.
Phone : (972)
(0)4-8296-355
Fax : (972)
(0)4-8296-115
Address : IBM Israel Science & Technolgy
LTD.
Matam , Advanced Technology
Center
Haifa 31905 ,
Israel.
E-Mail : ha...@haifasc3.vnet.ibm.com
Pete D. (Just trying to help)
Eran Harel wrote in message <355EFA...@haifasc3.vnet.ibm.com>...
:Hi ,
echo $1 | grep -s '[^0-9]' && echo "Enter a number" && exit(1)
should do the job of all the above code.
There might (and probably are) be other ways that are better.
FWIW, there are C library functions like isalpha() and isdigit()
You could always test that it is within some acceptable range.
if ( $1 > 0 && $1 < 99 ) then ...
This is a more robust approach, IMO, but YMMV. :-)
Personally, I prefer to write my scripts in Bourne shell to
maximize portability, and to use tcsh as my interactive
shell, but to each their own.
Chuck Demas
Needham, Mass.
--
Eat Healthy | _ _ | Nothing would be done at all,
Stay Fit | @ @ | If a man waited to do it so well,
Die Anyway | v | That no one could find fault with it.
de...@tiac.net | \___/ | http://www.tiac.net/users/demas
You shouldn't program in t/csh (but then you know that ;-)
www.perl.com/pub/csh/info/csh.whynot.gz explains why.
How about a ksh solution?
# isnum returns True if its argument is a valid number.
# The first pattern requires a digit before the decimal
# point, and the second after the decimal point.
function isnum # string
{
case $1 in
?([-+])+([0-9])?(.)*([0-9])?([Ee]?([-+])+([0-9])) )
return 0;;
?([-+])*([0-9])?(.)+([0-9])?([Ee]?([-+])+([0-9])) )
return 0;;
*) return 1;;
esac
# Not reached.
}
If you absolutely insist on a solution, I have a 1000+ line csh script
which performs multiple input validation at home. Email me if you desire
this.
-Brian