multiple expression if statement
Hello All,
I wanted to use multiple expressions in if statements and checking to see if my command syntax is right. condition is if filesize greterthan 3mb and fileid not equal to 1234&2345. fileid is basically file. I wanted to see if any of the file size is more than 3mb except for those two files. command is Quote:
Thanks |
as long as you are on bash, use the [[ notation, not the older, clunkier [, it's better in many many ways:
if [[ 1 -eq 1 && 3 -eq 3 ]]; then echo yes; fi |
What acid_kewpie said. The old brackets are not really designed for complex evaluations. If you must use them, however, use multiple individual tests instead of -a/-o.
Code:
if [ "$size" -ge 3145728 ] && { [ "$fileID" -ne 1234 ] || [ "$fileID" -ne 2345 ]; }; then Code:
if [[ "$size" -ge 3145728 && ( "$fileID" -ne 1234 || "$fileID" -ne 2345 ) ]]; then Code:
if (( size >= 3145728 && ( fileID != 1234 || fileID != 2345 ) )); then |
Quote:
|
True, one of the benefits of the new test is that there's no word-splitting done on variable expansions. But it never hurts to keep up the practice anyway, even when not strictly necessary. Places like this where it's safe to leave them off are relatively rare.
|
All times are GMT -5. The time now is 03:43 PM. |