PDA

View Full Version : I need a bash script to test multiple conditions in a while loop


Brazen
December 6th, 2006, 06:57 PM
Ok, I have three conditions that I need to test for. Here is a snippet of what I have:

while [ -n $FILE_1 -o -n $FILE_2 -o -n $FILE_3 ]; do
echo "do some stuff here..."
...
done

But this is giving me a "too many arguments error." If you can't tell, I'm using '-n' to test if a file has a non-zero length and using '-o' to test the three files. This isn't working for me and I'm wondering how I can test these three conditions in a while loop.

po0f
December 6th, 2006, 07:19 PM
Brazen,

Use && to test for multiple conditions.
while [ -s $FILE && -s $FILE2 && -s $FILE3]; do
echo "blar"
done

According to ABS (http://tldp.org/LDP/abs/html/), -s (http://tldp.org/LDP/abs/html/fto.html) tests for zero size, -n (http://tldp.org/LDP/abs/html/comparison-ops.html) tests to see if a string is null.

[EDIT]

Depending on what you're trying to accomplish, it might be better to test these files individually instead of all at once.

duff
December 6th, 2006, 09:57 PM
Ok, I have three conditions that I need to test for. Here is a snippet of what I have:



But this is giving me a "too many arguments error." If you can't tell, I'm using '-n' to test if a file has a non-zero length and using '-o' to test the three files. This isn't working for me and I'm wondering how I can test these three conditions in a while loop.

Do any of these filenames have spaces in the path? If so, add double-quotes.