'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 the names of the variables are correct, i double checked those thanks in advance |
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 |
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:
Finally, see pitfall #22 about possible issues with the x && y || z pattern. |
Quote:
Code:
if [ $ANYFILES -gt 0 ] && ( [ $VARERR -gt 0 ] || [ $TMP -gt 0 ] ); then But thanks anyways ;) |
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? |
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". |
Quote:
in stead of doing what you said, i did this: Code:
if ( ANYFILES > 0 ) && (( VARERR > 0 ) || ( TMP > 0 )); then Thanks everybody !!! :hattip: |
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 |
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. |