So I am getting confused by this one and cannot really understand why this is happening. Was hoping someone here could enlighten me!
I have a grep that is pushing its results to a file called out. I then have the following code....
if [ -s out ]; then
xargs -n1 basename < out
else
echo "NO FILES EXIST"
fi
The if path works file but when file size is 0 it will not go down the else path...
I then tried this...
if [ ! -s out ]; then
xargs -n1 basename < out
else
echo "NO FILES EXIST"
fi
And everything works as expected but in reverse (obviously)...
So my question is, what is the -s flag doing behind the scenes that I am not aware of? This completely blew my mind to quite honest. If anyone can shed some light on this it would be much appreciated!
Thanks!
Output from an ls -lrt - file is NOT going down else path here. If I add the ! operator the file will however go down the if path path and then if the file fills it will go down the else path.
0 Jul 30 12:15 out
No luck with the suggestion of making $filename a variable and passing it around that way. I was thinking maybe it has something to do with my grep? The entire code is as follows:
grep -l 'TimeStamp: '$2'/'$3'/'$1 $ARCHIVE/*/* > $filename
if [ -s "$filename" ]; then
xargs -n1 basename < out
else
echo "NO FILES EXIST"
fi
Where $1,$2,$3 is yyyy,mm,dd respectively.
The other thought I had is it might be my machine? I am running Linux Ubuntu 3.2.0-58-generic.
Thanks!
ls -l outorstat outwhen the file is reproducing the unexpected behavior? If it contained only whitespace -- say, a single newline -- that would explain the behavior you're seeing. Granted, you're describing it as 0-byte, but the obvious explanation would be that it's not actually 0-byte even when it appears empty.[is the command also known astest(literally,/bin/[is a symlink to/bin/test, and whereas your shell almost certainly provides a builtin version, that builtin version is required to be compatible), so[ -s out ]is the thing documented inman testastest -s... making more of this a generic-UNIX-tools question than a bash question as such.rm out; touch out; if [ -s out ]; then xargs -n1 basename <out; else echo "NO FILES EXIST"; ficorrectly emitsNO FILES EXIST. If you can provide a standalone reproducer that actually, well, reproduces the problem, that might be helpful.outin the[test? Or are you using a variable (like$outputfile)?$filename, but that variable were empty, bash would be running[ -s ], which is equivalent to[ -n -s ], which is true. All the more reasons to always use quotes;[ -s "$filename" ]wouldn't have that problem.