Skip to main content
14 events
when toggle format what by license comment
Dec 16, 2018 at 0:49 comment added kasperd @UncleBilly Interesting. I learned something new today. It's only the p variants which have this behavior though. So a script without #! still won't be a substitute for an executable if it is called through any of the other functions.
Dec 16, 2018 at 0:00 comment added user313992 @kasperd I won't annoy you with docs and standards; just try it! echo 'int main(int c,char**a){execvp(a[1],a+1);}' | cc -include unistd.h -xc -; echo echo yeah > a.sh; chmod 755 a.sh; ./a.out ./a.sh; PATH=`pwd` ./a.out a.sh
Dec 15, 2018 at 21:45 comment added kasperd @UncleBilly And on which systems will execvp or execlp run a script with out #!? They certainly won't on Linux.
Dec 15, 2018 at 19:48 comment added user313992 @kasperd if you think that shebangs (#! /bin/cmd) are in any way standard you're sorely mistaken; using execvp or execlp is the only way to portably run an executable script.
Dec 15, 2018 at 19:39 comment added user313992 @kasperd it is really portable and it is a valid script, that could be invoked either as command from any standard shell or via the execvp and execlp library functions (which means that it could be used with xargs, find, etc). You're either completely missing the point ("a script which is valid as either shell or sql") or have some kind of agenda (you don't think the way execvp works is "right", so any use of it should be "invalid"). It doesn't matter if strace cannot run it directly; neither can gdb, so what?
Dec 15, 2018 at 15:01 comment added ilkkachu @kasperd, well, sure, it won't work if you exec() it directly from something other than a shell. But what would that case be? You might want to run the script from cron or such, but I think it runs everything through a shell anyway, and even if not, it's easy to just spell out db2 -txf /path/to/script in that case, since you only need to do it once. Having the shorthand work is mostly useful on an interactive shell. But sure, a separate wrapper script might be more robust.
Dec 15, 2018 at 14:28 comment added kasperd @ilkkachu But scripts are not always executed from a shell. If you try to use the script in any other context where an executable would work it's going to fail. Moreover shells don't agree on which interpreter to use. So your script will now behave differently or fail altogether depending on which context it is being called from.
Dec 15, 2018 at 14:13 comment added ilkkachu @kasperd, it should still be portable, the shell is supposed to run the script as a shell script if exec() doesn't work on it. "If the execl() function fails due to an error equivalent to the [ENOEXEC] error, the shell shall execute a command equivalent to having a shell invoked with the command name as its first operand, ... " (see pubs.opengroup.org/onlinepubs/9699919799.2018edition/utilities/…)
Dec 15, 2018 at 13:40 comment added kasperd It's still not really portable. It's not a valid script and you are still relying on the calling shell to work around the fact that the kernel will refuse to run the script and returns ENOEXEC if you try to. Try running the script under strace to see what I mean.
Dec 14, 2018 at 23:11 history edited user313992 CC BY-SA 4.0
simpler / safer
Dec 14, 2018 at 23:03 history edited user313992 CC BY-SA 4.0
revert my PS; it seems that zsh is using /bin/sh to exec non-shelbanged scripts
Dec 14, 2018 at 22:44 history edited user313992 CC BY-SA 4.0
added 175 characters in body
Dec 14, 2018 at 22:37 history edited user313992 CC BY-SA 4.0
make it even more ridiculous; explanation
Dec 14, 2018 at 22:31 history answered user313992 CC BY-SA 4.0