Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Q: Digits only arguments check in a csh script

58 views
Skip to first unread message

Eran Harel

unread,
May 17, 1998, 3:00:00 AM5/17/98
to

Hi ,

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

unread,
May 17, 1998, 3:00:00 AM5/17/98
to

I'm sorry I don't know right off hand, but there is an "if" statement to
check for 'integer'... So, if NOT an 'integer', enter a number... My "if"
notes are at work


Pete D. (Just trying to help)

Eran Harel wrote in message <355EFA...@haifasc3.vnet.ibm.com>...
:Hi ,

Charles Demas

unread,
May 17, 1998, 3:00:00 AM5/17/98
to Eran Harel

In article <355EFA...@haifasc3.vnet.ibm.com>,

Eran Harel <ha...@haifasc3.vnet.ibm.com> wrote:
>
>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
> ...

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

brian hiles

unread,
May 19, 1998, 3:00:00 AM5/19/98
to

Eran Harel (ha...@haifasc3.vnet.ibm.com) wrote:
: 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) :

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

0 new messages