[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: bash: problem with [sets]



On Mon, 2003-06-23 at 13:57, David selby wrote:
> My bash masterpeice is almost complete ... I have one last problem that 
> no matter how many "" or '' I use I cannot get around.
> 
> I need to use an if...then construct to check that my 4 digit date field 
> is 4 numerical digits.This is for error checking.  I thought [sets] was 
> the way to go ....
> 
> if [ $fourdig = [0-9][0-9][0-9][0-9] ]; then ......
> 
> I have tried '[0-9]' "[0-9]", straight [0-9]. Interestingly no matter 
> what I do in [sets] as a comaparison it is invalid in the if...then 
> construct. It has occured to me that perhaps [sets] are not allowed as a 
> conditional test.
> 
> If this is the case, how can I test a string ?

for your enjoyment:

#!/bin/bash
 
if echo $fourdig | grep ^[0-9]*$ > /dev/null 2>&1; then
        echo "yes, it is a number"
else
        echo "no, it is not a number"
fi

-matt zagrabelny



Reply to: