17

I have found the following kind of shebang in the RosettaCode page:

--() { :; }; exec db2 -txf "$0"

It works for Db2, and a similar thing for Postgres. However, I do not understand the whole line.

I know the double dash is a comment in SQL, and after that it calls the Db2 executable with some parameters passing the file itself as file. But what about the parenthesis, the curly brakets, the colon and semi-colon, and how can replace a real shebang #! ?

https://rosettacode.org/wiki/Multiline_shebang#PostgreSQL

muru
  • 72,889
AngocA
  • 295

2 Answers2

21

Related: Which shell interpreter runs a script with no shebang?

The script does not have a shebang/hashbang/#! line, simply because a double dash is not #!.

However, the script will be executed by a shell (see above linked question and answers), and in that shell, if - is a valid character in a function name, the line declares a shell function called -- that does nothing (well, it runs :, which does nothing) and which is never called.

The function, in the more common multi-line notation (just to make it more obvious what it looks like, as its odd name kinda obscures the fact that it's in fact a function):

-- () {
  :
}

The sole purpose of the function definition is to have a line that is valid in a shell script and at the same time a valid SQL command (a comment). This sort of code is called a polyglot.

After declaring the bogus shell function, the script, when executed by a shell script interpreter, uses exec to replace the current shell with the process resulting from running db2 -txf "$0", which would be the same as using db2 -txf on the pathname of the script from the command line.

This trick would probably not work reliably on systems where dash or other ash-based shells, yash, the Bourne shell, ksh88 or ksh93 is used as /bin/sh, as these shell do not accept functions whose name contains dashes.

Also related:


I suppose the following would also work (not really tested):

--() { exec db2 -txf "$0"; }; --
Kusalananda
  • 333,661
7

As @Kusalananda has already said, that trick is broken and it won't work in all shells.

Here is my take at doing it portably:

--/.. 2>/dev/null; exec db2 -txf "$0"

The first command should fail even if a file/directory named -- exists in the current directory and any errors will be shut up by the 2>/dev/null; the shell will then proceed with the second command, the exec.

  • 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. – kasperd Dec 15 '18 at 13:40
  • @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 http://pubs.opengroup.org/onlinepubs/9699919799.2018edition/utilities/V3_chap02.html#tag_18_09_01_01) – ilkkachu Dec 15 '18 at 14:13
  • @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. – kasperd Dec 15 '18 at 14:28
  • @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. – ilkkachu Dec 15 '18 at 15:01
  • @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 '18 at 19:39
  • @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 '18 at 19:48
  • @UncleBilly And on which systems will execvp or execlp run a script with out #!? They certainly won't on Linux. – kasperd Dec 15 '18 at 21:45
  • 1
    @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 16 '18 at 00:00
  • @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. – kasperd Dec 16 '18 at 00:49