LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   'advanced' if (https://www.linuxquestions.org/questions/linux-newbie-8/advanced-if-876320/)

brownie_cookie 04-21-2011 06:53 AM

'advanced' if
 
hi all

i've got something to ask you guys, it's maybe a stupid mistake but i can't seem to figger out what's wrong with this :

Code:

if [ $ANYFILES -gt 0 ] && [ [ $VARERR -gt 0 ] || [ $TMP -gt 0 ] ]; then
        echo "test"
        exitstatus=$STATE_WARNING
else
        echo "test1"
        exitstatus=$STATE_OK
fi

(see the bold piece)
the names of the variables are correct, i double checked those

thanks in advance

smallpond 04-21-2011 07:48 AM

Square brackets are not parens. Take a look at a bash manual to see the difference between () [] and [[]]. I think you want:
Code:

if [[ $ANYFILES -gt 0 ]] && ( [[ $VARERR -gt 0 ]] || [[ $TMP -gt 0 ]] ); then

David the H. 04-21-2011 07:54 AM

This is bash pitfall #11:
http://mywiki.wooledge.org/BashPitfa..._.3D_d_.5D_.5D

Use the bash [[ extended test command instead. Or even better, use the arithmetic operator, since this is a numeric test (pitfall #7).
Code:


if (( ANYFILES > 0 )) && (( VARERR > 0 || TMP > 0 )) ; then
    echo "test"
    exitstatus=$STATE_WARNING
else
    echo "test1"
    exitstatus=$STATE_OK
fi

Note that you don't need $ in front of the variable inside ((..)) brackets. They get expanded automatically.

Finally, see pitfall #22 about possible issues with the x && y || z pattern.

brownie_cookie 04-21-2011 07:59 AM

Quote:

Originally Posted by smallpond (Post 4331545)
Square brackets are not parens. Take a look at a bash manual to see the difference between () [] and [[]]. I think you want:
Code:

if [[ $ANYFILES -gt 0 ]] && ( [[ $VARERR -gt 0 ]] || [[ $TMP -gt 0 ]] ); then

we have a winner ;)

Code:

if [ $ANYFILES -gt 0 ] && ( [ $VARERR -gt 0 ] || [ $TMP -gt 0 ] ); then
        echo "Er zijn bestanden die ERROR bevatten of die eindigen op .tmp -> $VARERR erros en $TMP tmps"
        exitstatus=$STATE_WARNING
else
        echo "Gecontroleerd en niets aan de hand"
        exitstatus=$STATE_OK
fi

David the H. thanks for your suggestion but for some reason it didn't work for me... i changed al the [ to ( and deleted the $ and changed the -gt to > but it didn't work
But thanks anyways ;)

David the H. 04-21-2011 08:18 AM

I tested it exactly as I posted it and got the expected behavior.

What do you mean exactly by "it didn't work"? Could you post what you tried?

MTK358 04-21-2011 09:10 AM

Remember that '[' is not interpreted as a paren by bash, it's actually a command (see man test for more info).

And the expression after "if" isn't special, it's just a command. If that command returns 0, then it's "true", otherwise it's "false".

brownie_cookie 04-21-2011 09:23 AM

Quote:

Originally Posted by David the H. (Post 4331575)
I tested it exactly as I posted it and got the expected behavior.

What do you mean exactly by "it didn't work"? Could you post what you tried?

Sorry, i forgot something to delete :doh:

in stead of doing what you said, i did this:
Code:

  if ( ANYFILES > 0 ) && (( VARERR > 0  ) || ( TMP > 0 )); then
my bad ;) it's also working with your solution !!

Thanks everybody !!! :hattip:

grail 04-21-2011 10:00 AM

That won't work either but I am guessing it is just a typo ... your first test requires 2 sets of round brackets also, ie. (()).
Also, no real need to take the tests into separate brackets either:
Code:

if (( ANYFILES > 0 && ( VARERR > 0  ||  TMP > 0 ) )); then

David the H. 04-21-2011 10:02 AM

I expected as much. Discovering just where the syntax errors lie is always the hardest part. :)

If you're satisfied with the solution, please mark the thread as solved.


All times are GMT -5. The time now is 11:32 AM.