2

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!

13
  • Can you show us the output of ls -l out or stat out when 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. Commented Jul 30, 2014 at 16:04
  • 1
    ...by the way, [ is the command also known as test (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 in man test as test -s... making more of this a generic-UNIX-tools question than a bash question as such. Commented Jul 30, 2014 at 16:06
  • 1
    Anyhow -- cannot reproduce. rm out; touch out; if [ -s out ]; then xargs -n1 basename <out; else echo "NO FILES EXIST"; fi correctly emits NO FILES EXIST. If you can provide a standalone reproducer that actually, well, reproduces the problem, that might be helpful. Commented Jul 30, 2014 at 16:18
  • 2
    Are you literally using the word out in the [ test? Or are you using a variable (like $outputfile)? Commented Jul 30, 2014 at 16:23
  • 1
    @EtanReisner has a very good clue, there -- if you were using, say, $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. Commented Jul 30, 2014 at 16:29

2 Answers 2

1

Can't reproduce it. Can you follow these steps:

touch test.txt
[ -s "test.txt" ] && echo "empty"

?


Btw, this might be more a question for clearance than an answer and could be a comment, I just wanted to have both commands as readable as possible.

Sign up to request clarification or add additional context in comments.

Comments

1
-s FILE:         True if file exists and is not empty

You mentioned the file exists, and that its size is zero, so the test should be True.

Try using the full path to the file, not just out. e.g.>

if [ -s /full/path/to/out ]; then
   xargs -n1 basename < out
else
   echo "NO FILES EXIST"
fi

I think the problem is not the test itself, but the path where the script was ran.

1 Comment

...and if that's the case, the question this is in answer to lacks a MCVE / SSCCE and should be closed (for not containing enough information or code to reproduce the problem).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.