Chet Ramey wrote:
On 3/24/18 3:31 PM, L A Walsh wrote:
bash sleep 1
I get:
/usr/bin/sleep: /usr/bin/sleep: cannot execute binary file
???
Isn't it bash that cannot execute the binary file because
it expected a script?
Think about what happens when you run a command like that. Bash sees
that it has an argument, which it assumes to be a script. It sets the
positional parameters and $0 accordingly. Posix and historical behavior
say that $0 gets set from the name of the script.
Then the shell attempts to open the file and read it. It discovers
the file is not a text file, and refuses to execute it, prefixing the
error message with $0 as it usually does.
I pretty much had a feeling as to why it happened, just that it
seems some check is out of order.
I.e. You are saying bash sets '$0' before it validates the the argument
as being a text file that it can execute as a script. Setting '$0' before
actual validation seems like a speculative operation on the presumption
that the file will be an executable script (but before) it's
actual executability as a script is validated.
Shouldn't the validation be done before setting '$0'? Then bash
might give a better error message, along the lines of:
/bin/bash: /usr/bin/sleep: cannot execute binary file as a script
Either that, or, since it is "obvious" from the error message that
bash knows it is a binary file, it could just go ahead and actually
execute it as one (as if '-c' was present).
Technically, file type validation should be done first if you want to
strictly correct. However, since it would result in an error anyway, it
seems that going ahead and executing 'sleep' as a binary command would seem
to be a more "user friendly" or useful thing to do, no?
I admit my original thought was that the error message could be
more clear and likely wasn't because something was being done
'out of order', but doing the more useful action doesn't
seem that problematic...(?)