0

The code is:

src=/home/Desktop/src
dest=/home/Desktop/dest

if test -s $src/$InputFile ; then
  echo "File is present. :$InputFile" 
else
  echo "File is not present" 
fi

In the above code if I declare variable "InputFile" as InputFile=a.txt then it goes and check for a file a.txt in src directory, if file exists it prints "File is present". If file does not exists then it prints "File is not present".

I have not declared variable "InputFile" in above code. Still it goes to src path and execute if condition and prints "File is present". Can anyone tell me exact behaviour of "test -s" when filename variable is empty?

ziga
  • 109
  • 10

2 Answers2

2

You ran:

test -s /home/Desktop/src/

And this /home/Desktop/src/ is not an empty directory.

Ipor Sircer
  • 3,069
  • 3
  • 10
  • 15
  • Thanks for the clarification. But only on linux it executes if condition. On windows it executes else condition. Is this behaviour changes in windows? – ziga Nov 29 '16 at 09:37
  • Feel free to fill a bugreport. – Ipor Sircer Nov 29 '16 at 09:39
  • @ziga It probably has to do with what files/directories exist on each system where you tested it, not the OS itself. On the Windows system the path /home/Desktop/src/ probably does not exist, so the `test` command will return an error. – augurar Nov 29 '16 at 09:55
  • @augurar The same path exist in windows machine as well. There is no difference in the path. – ziga Nov 29 '16 at 10:37
  • The *path* is the same, but how the local file system implements directories is different. `bash` is just calling `stat` on the path you give it, and checking if the returned size of zero or not. It doesn't care what kind of file it is – chepner Nov 29 '16 at 13:27
0

The misunderstanding arises from the fact that -s works not only on regular files, but on all kinds of files.

So when you make the InputFile variable null, it is checking the existence and size of the directory expanded from $src, and returning $? as 0 as the directory occupies a minimum of block-sized size on the filesystem (assuming the directory exists at the first place).

heemayl
  • 39,294
  • 7
  • 70
  • 76
  • Thanks for the clarification. But only on linux it executes if condition. On windows it executes else condition. Is this behaviour changes in windows? – ziga Nov 29 '16 at 09:38