3

If I have zero byte file and it is executable, it will just do nothing. Can I fail on such files, i.e. return some error message and/or return non-zero exit code?

7
  • 5
    I don't think it is possible, zero byte file is a valid shell script. Commented Jan 30, 2020 at 13:08
  • 2
    ... but you can check its size before executing it and fail if the size is 0. Commented Jan 30, 2020 at 14:13
  • 2
    An empty executable file is a valid implementation of the true command. Do you want sh -c '' to return an error message or a non-zero exit code as well? Commented Jan 30, 2020 at 14:13
  • 1
    Why would you want to do that? Maybe there is another solution. Commented Jan 30, 2020 at 14:34
  • 4
    No. Trying to execute a zero size executable will fail with ENOEXEC.What you're seeing is a feature of your shell or utility, which will try to run an executable by passing it to /bin/sh (or interpreting it itself) in case of ENOEXEC. Of course, you're not telling what system, shell or utility you're using, and what exactly your problem is, because facts and details are too boring. Commented Jan 30, 2020 at 18:22

1 Answer 1

1

So, I think the only answer answer is

if [ -s mybinary ]
then
   mybinary
else
   exit 1
fi

right?

2
  • 2
    what difference does it make if the file is empty or contains just a newline, a zero byte, a comment line or multiple newlines, etc? Commented Feb 13, 2020 at 12:53
  • you are right, hence the answer is incorrect Commented Oct 10, 2020 at 21:01

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.