[Nottingham] Bash: test "file exists" - gotme again

Johannes Kling jok at printk.net
Thu Oct 14 14:47:01 BST 2004


Hello,

> while [[ -a "$filename" ]]; do
>            filename=$basefilename$file_no.tif
>            echo "processing file: "$filename
>            file_no=$(($file_no + 1))  
> done

On each run of the while loop, you're checking the filename of the
previous execution of the loop, as far as I can see.

It's a bit like:
while ( c < 5 ) { c++; } /* At the end, c will be 5, not 4 */

I don't know of a "do while" construct in bash, and I don't think the
for loop is useful here either, so try "break":

----
while :; do
	filename=$basefilename$file_no.tif

	test -a "$filename" || break; # exit the loop if the file
                                      # doesn't exist

	echo "processing file: "$filename
	file_no=$(($file_no + 1))
done
----

It's not a nice solution; someone with a bit more bash knowledge than
me could probably come up with something more readable :-).

Regards,
  Jo

-- 
"It's not peer pressure, it's just your turn!"



More information about the Nottingham mailing list