Linux Shell Script 'if' statement - quick question
Discussion
In C/JAVA I'd write
if( ( $STARTYEAR == $STOPYEAR ) && ( $STARTMONTH == $STOPMONTH ) && ( $STARTDAY == STOPDAY ) )
{
// do stuff
}
is there a way of compounding the tests in shell scripting e.g.
if [ ( ( $STARTYEAR == $STOPYEAR ) && ( $STARTMONTH == $STOPMONTH ) && ( $STARTDAY == STOPDAY ) ) ]
then
# do stuff...
fi
Doesn't work - do I need a separate if[ ] for each equality test?
many thanks
Ex
if( ( $STARTYEAR == $STOPYEAR ) && ( $STARTMONTH == $STOPMONTH ) && ( $STARTDAY == STOPDAY ) )
{
// do stuff
}
is there a way of compounding the tests in shell scripting e.g.
if [ ( ( $STARTYEAR == $STOPYEAR ) && ( $STARTMONTH == $STOPMONTH ) && ( $STARTDAY == STOPDAY ) ) ]
then
# do stuff...
fi
Doesn't work - do I need a separate if[ ] for each equality test?
many thanks
Ex
TheExcession said:
In C/JAVA I'd write
if( ( $STARTYEAR == $STOPYEAR ) && ( $STARTMONTH == $STOPMONTH ) && ( $STARTDAY == STOPDAY ) )
{
// do stuff
}
is there a way of compounding the tests in shell scripting
Ex, my 'C' is crap (and I'm assuming bourne sh here), but firstly, I think you've got the test wrong, should be:
if [ "$STARTYEAR" = "$STOPYEAR" ]; then
blah blah
fi
As for multiple tests:
if [ "$STARTYEAR" = "$STOPYEAR" ] && [ "$STARTMONTH" = "$STOPMONTH" ]; then
blah blah
fi
would work I think. Check the manpage for "test" for syntax.
The [ ... ] notation is just a shorthand for the 'test' command, so 'man test' should give you everything you need. Off the top of my head, what you've written looks OK except that the test for string equality is '=' not '=='. If you're comparing numbers, then it's '-eq' (ie [ $NUM1 -eq $NUM2 ])
Edit to say - that's with ksh, at least. Other shells may vary.
>> Edited by mrwomble on Wednesday 29th September 15:59
Edit to say - that's with ksh, at least. Other shells may vary.
>> Edited by mrwomble on Wednesday 29th September 15:59
Thanks guy's - I've sussed it
if [ "$STARTYEAR$STARTMONTH$STARTDAY" == "$STOPYEAR$STOPMONTH$STOPDAY" ]
I keep forgetting about replacements!
Damn powerfull this linux stuff
best
Ex
edit to say - well sussed it one way - Pigeon's stuff looks a bit more 'proper' tho
>> Edited by TheExcession on Wednesday 29th September 16:09
if [ "$STARTYEAR$STARTMONTH$STARTDAY" == "$STOPYEAR$STOPMONTH$STOPDAY" ]
I keep forgetting about replacements!
Damn powerfull this linux stuff
best
Ex
edit to say - well sussed it one way - Pigeon's stuff looks a bit more 'proper' tho
>> Edited by TheExcession on Wednesday 29th September 16:09
Gassing Station | Computers, Gadgets & Stuff | Top of Page | What's New | My Stuff


