Re: odd error from bash exec binary on cmd line

2018-04-03 Thread L A Walsh



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...(?)







Re: odd error from bash exec binary on cmd line

2018-03-25 Thread Chet Ramey
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.


-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRUc...@case.eduhttp://tiswww.cwru.edu/~chet/



odd error from bash exec binary on cmd line

2018-03-24 Thread L A Walsh

Using bash-4.4.12...

If I use bash -c, I get a variety of correct messages:


 bash -c xxx

bash: xxx: command not found


 touch xx

Ishtar:law/bin> bash -c xx
bash: ./xx: Permission denied

or when it works, it just does:
bash -c "sleep 1"
(sleeps 1)

but if I leave off the '-c', like:

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?

Shouldn't bash be to the left of the ':' with some
error on the right, like "cannot exec binary file" or
"expected script file"?