SO Stinks
SO Stinks

Reputation: 3406

How to validate numeric input arguments in TCSH script?

How can I validate numeric input arguments to a tcsh script?

#!/usr/bin/tcsh

if ( $1 < 0.0 ) then
    echo "ERROR: first input argument is less than zero."
    exit 1
endif

The above snippet shows what I'm trying to do but doesn't work. I have tried MANY combinations based on using the expr command or the @ operator to no avail. The man page and the web have turned up nothing yet. No matter what I try I keep getting errors like "Badly formed number" or "set: Variable name must begin with a letter".

Is there a tcsh-ish way of doing this? I could certainly hack something up using awk or watever but that seems kind of silly.

Upvotes: 2

Views: 2580

Answers (1)

Dennis Williamson
Dennis Williamson

Reputation: 360485

Tcsh doesn't do floats. You can use bc or awk:

#!/usr/bin/tcsh
if ( `echo "$1 < 0.0" | bc` == 1 ) then
    echo "ERROR: first input argument is less than zero."
    exit 1
endif

or

#!/usr/bin/tcsh
if ( `awk -v "val=$1" 'BEGIN {print val < 0.0}'` == 1 ) then
    echo "ERROR: first input argument is less than zero."
    exit 1
endif

Upvotes: 2

Related Questions