1833847 Members
2344 Online
110063 Solutions
New Discussion

validate script args

 
SOLVED
Go to solution
Greg Stark_1
Frequent Advisor

validate script args

I have a script which takes an argument that represents the number of hours to run a process for. I want to make sure the arg is between 1 and 24. I know I could do some if statements but I was wondering if there was a better way, maybe with RE's, to do this.

Thanks,
Greg
8 REPLIES 8
Craig Rants
Honored Contributor
Solution

Re: validate script args

!/bin/sh
if [ $VAR -ge 1 -a $VAR -le 24 ]
then
echo "good"
else
echo "no good"
fi

GL,
C
"In theory, there is no difference between theory and practice. But, in practice, there is. " Jan L.A. van de Snepscheut
Rodney Hills
Honored Contributor

Re: validate script args

Using > and < to compare will always be faster than whatever regular expression could be generated.

If you really wanted a reg express, then
[1-9]|1[0-9]|2[0-4]

could be used in an "extend reg expressoin", like-
case "11" in [1-9]|1[0-9]|2[0-4])e cho "yes" ;;
*) echo "no" ;;
esac

-- Rod Hills
There be dragons...
A. Clay Stephenson
Acclaimed Contributor

Re: validate script args

iHere's is one approach using command-line perl RE's:

#!/usr/bin/sh

STAT=1
until [ ${STAT} -eq 0 ]
do
echo "Enter Hour: \c"
read HR
perl -e 'if ($ARGV[0] =~ /^\d+$/) { if ($ARGV[0] >= 0 && $ARGV[0] <= 24) {exit(0)}}; exit(1);' ${HR}
STAT=$?
if [ ${STAT} -ne 0 ]
then
echo "\07BAD INPUT"
fi
done

If I didn't make a typo that should be a complete solution.
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: validate script args

I'm an idiot. Change the $ARGV[0] >= 0 to $ARGV[0] > 0.
If it ain't broke, I can fix that.
Martin Johnson
Honored Contributor

Re: validate script args

Clay,

You're being too hard on yourself. Idiots don't get to be Olympians!



:-)
Marty
David Totsch
Valued Contributor

Re: validate script args

You must all be SysAdmins -- you are making this way too difficult. Korn/POSIX will accept:

if (( ${1} > 0 && ${1} < 25 ))
then
echo $I is OK
else
echo $I is wrong
fi

Yes, those are double-parenthesis. They are also good for incrementing variables like this:

(( VAR += 1 ))

Be careful preceeding them with "$" because they are arithmetic evaluations.

Enjoy,
-dlt-

BTW: [ ] causes the shell to load test(1) to make the decision; using [[ ]] will avoid the overhead of loading test(1) and put the burden on the quite capable shell.
A. Clay Stephenson
Acclaimed Contributor

Re: validate script args

Now hold on a minute, David. I will certainly concede that your approach will work but if and only if, it's given a valid numeric operand. A far safer approach is to make sure that the user supplied numeric data before doing any numeric comparisons. Craig's solution suffers from the same weakness.

Your point about [[ in lieu of [ is well taken.

Regards, Clay
If it ain't broke, I can fix that.
Rodney Hills
Honored Contributor

Re: validate script args

He also stated he was interested in a version that had a regular expression. The great thing about the forum is we get to see more than one way to do it...

-- Rod Hills
There be dragons...