Rather than get in hot (or even slightly-above-comfortable temperature) water for reviving a closed (ca. 2007) thread without moderator permission or acquaintance of the OP personally or by proxy, I'll post this alternate solution (approach?) along with one positive real-world example illustrating how mine is easier to write and is equally reliable, if not altogether better.
The thread is
here. Their solution:
Code:
if [ "$myfilesize" eq "0" ];then echo "$file: the file size is zero"; else echo "$file: the file size is NOT zero"; fi
(and this hangs on the output of a stat -c %s command).
"Hangs" is probably a most appropriate word. The two outputs, using two different stat options, the same file, and three marginally-different command-line syntaxes I just tried gave me these:
First, using their approach, I got this:
Code:
tjsfa070929-094.jpg.error: the file size is NOT zero
Even though stat gave me this
Code:
>> stat -c %s $file
0
I thought, "Does stat really know what it's about?" So I just stat-ed the file for the complete page results on the file and saw "regular empty file". I asked myself, "Okay, what tag or flag represents that attribute?"
Off to the man page:
Quote:
Originally Posted by man stat (on www.bash-linux.com)
%F File type
|
looked good, so I tried
Code:
>> stat -c %F $file
regular empty file
A Match! I rolled that into the myfilesize solution, thusly
Code:
if [[ $myfilesize == *empty* ]];then echo "$file: the file size is zero"; else echo "$file: the file size is NOT zero"; fi
and ran it:
Code:
>> tjsfa070929-094.jpg.error: the file size is zero
From where I sit, getting the file type string instead of an integer, which can be equally (if not often
more) accurate, makes for an easier time of setting the conditional and, maybe only in terms of a few ticks, a faster script from start to finish. I think my method is better, and proven so to the limits of my knowledge and ability here in this OP.
Comments?
BZT