5 ms·
What's been a problem is not being able to pass more than one argument through hash-bang. Fix that one, and you can use #!/usr/bin/env interpreter --arg --
by kazinator 4y ago
What's been a problem is not being able to pass more than one argument through hash-bang.
Fix that one, and you can use
#!/usr/bin/env interpreter --arg --arg ...
Problem solved; no need to mess with the file system divisions.
For that matter ... the stupid hash bang mechanism itself should do PATH resolution, maybe, you know?
#!/usr/bin/awk -f <-- find awk at that absolute path
#!awk -f <-- Just use the darn PATH, dear hash-bang handling code in the kernel
- nerdponx 4y agoIt's a real problem with interpreted languages in general. PATH lookup is not always desirable. Not to mention library linking and looking for various other data files.
- kazinator 4y agoThe writer of the hash bang script retains the control not to have PATH lookup take place: they write an interpreter name which contains one or more slashes.
- paranoidrobot 4y agoNice, but PATH isn't always set right. I've run into this particular issue with stripped down containers. I run a script as part of the container's CMD/ENTRYPOINT, and it fails. Run it through an interactive shell, and hey-presto PATH is setup and script runs fine.
- mbreese 4y agoI’d suspect the problem is a stripped down container where the profile isn’t setup how you’re expecting. The interactive shell (bash, I assume), will setup the profile/env properly. I’m not sure your entrypoint cmd will setup the environment, as IIRC, the inir process would normally take care of this setup. But an entrypoint in a container has different rules. Perhaps if you started your entrypoint with “/bin/bash cmd”??
- nmz 4y agoQuite frankly if your PATH isn't set up correctly you have bigger problems than #!
- kazinator 4y agoIf PATH isn't set up right then #!/usr/bin/env interpreter -S arg, which everyone uses, also won't work. It doesn't amount to a good argument why #!interpreter arg shouldn't be able to do that path search without the env shim or its need for the -S mechanism.
- anderskaseorg 4y agoThis problem has been solved on at least Linux (GNU coreutils ≥ 8.30, 2018-07-01), FreeBSD (≥ 6.0, 2005-11-04), and macOS with the env -S option: #!/usr/bin/env -S interpreter --arg --arg https://www.gnu.org/software/coreutils/manual/html_node/env-invocation.html#g_t_002dS_002f_002d_002dsplit_002dstring-syntax https://www.gnu.org/software/coreutils/manual/html_node/env-... https://www.freebsd.org/cgi/man.cgi?env https://www.freebsd.org/cgi/man.cgi?env https://www.unix.com/man-page/mojave/1/env/ https://www.unix.com/man-page/mojave/1/env/
- kazinator 4y agoI know about this, and in fact I wrote an alternative solution for GNU Coreutils env in 2017: a complete patch with updated documentation and test cases. Here is a problem: interpreters take arguments such as options ... but, separately, so do interpreted programs. Sometimes, you'd like to control both. I made an extension to env whereby you could do: #!/usr/bin/env :interp:--foo:{}:--bar This would find the "interp" interpreter using a PATH search and then pass it the arguments --foo <scriptname> --bar, followed by arguments that came from the invocation of the hash-bang script. If the special argument {} does not appear, then interp will be invoked with --foo --bar <scriptname>: the script name is not relocated into the arguments. However, the maintainer expressed a preference for compatibility with FreeBSD -S. I took a look at the requirements and saw that FreeBSD's env -S was doing a whole lot of stuff: interpreting C-like character escape sequences like \n, and interpolating dollar-sign-sigiled variables like $USER. In spite of all that, didn't see the feature of being able to insert the script name into the middle of the arguments, like in my solution. It amounted to throwing away my patch and implementing some FreeBSD stuff I had no interest in and a lot of which I thought was a bad idea, while failing to achieve my original functionality. So, I just did the first part: throwing away my patch.
- anderskaseorg 4y agoThe typical use case for env -S is a script passing arguments to the interpreter (#!/usr/bin/env -S interp --foo → interp --foo ./scriptname), and the user can always pass arguments to the script (./scriptname --bar → interp --foo ./scriptname --bar). But why would a script ever need to use a shebang to pass an argument like --bar to itself? Can’t it just modify its own argv, or act as if it had? (I found your message at https://lists.gnu.org/archive/html/coreutils/2017-05/msg00021.html https://lists.gnu.org/archive/html/coreutils/2017-05/msg0002... that claims > It is useful because it allows the hash bang to specify some arguments after the script (which could be arguments belonging to the script rather than to the interpreter, for instance). but doesn’t really explain why the script itself would want to specify that.) In any case, compatibility is important here, in order for it to be possible to write cross-platform scripts.
- nmz 4y agoYou could always fixup all of your scripts so they point to the correct point in PATH. this also has the benefit of notifying you if a program doesn't exist. Do note that env is unportable (freebsd has -P I think), which makes parsing it a PITA, identifying where the program actually is, is slightly challenging. If you ask me its better to throw the program to sh and let it eval to whatever. sh is the true portable launcher. I wrote [0] in lua a while back (mind the comments), didn't destroy anything I think, I wrote [1] just now in gawk, so not as recommended. [0]: https://termbin.com/ijsu https://termbin.com/ijsu [1]: https://termbin.com/rx0e https://termbin.com/rx0e FWIW the correct solution, is the plan9 one, just union mount/bind all paths to /bin.
- xani_ 4y agoIt's funny that you mention that because ZSH does exactly that -> ᛯ cat /tmp/1.sh #!echo ble bla bla bla -n -e -> ᛯ /tmp/1.sh ble bla bla bla -n -e /tmp/1.sh It's the best solution but it will take decades for that to migrate to other shells
- kazinator 4y agoI'm guessing that Zsh is doing this in its fallback strategy for when the exec fails. The exec fails when a shell tries to run an executable file that has no hash bang header, but also in a case like this when the header doesn't give a path that resolves to an interpreter. Without the #!echo, I'm guessing Zsh would have interpreted the file as as Zsh script.