Of course, the standard way of testing if a file is empty is with test -s FILE
, but one of our clients have received a script containing tests like this:
RETVAL=`ls -s ./log/cr_trig.log | awk '{print $1}'`
if test $RETVAL -ne 0
then
echo "Badness: Log not empty"
exit 25
fi
with claims from the supplier that it works in the two environments that they have tested it in. Needless to say, it failed badly on both two places that I tested it.
So, I got curious. When does ls -s
print 0
for empty files?
This is my findings so far:
- GFS on Linux: 4
- ext4 on Linux: 0
- ZFS on Solaris: 1
- UFS on Solaris: 0
- jfs on AIX : 0
- VxFS on HP-UX: 0
- HFS on HP-UX: 0
- HFS on Mac OS X: 0
I haven't examined networked file systems yet.
Question: How can I elegantly explain to the others that their scripts are wrong ?
In my opinion, the "correct" version would be:
if test ! -s ./log/cr_trig.log
then
echo "Badness: Log not empty"
exit 25
fi