Problems with string analysis in Shebang in Ubuntu

What is an acceptable portable way to include interpreter options in a shebang string, i.e. how can i do something like

#!/usr/bin/env python -c 

or (more importantly) something like

 #!/usr/bin/env java -cp "./jars/*:./src" -Xmn1G -Xms1G -server 

and is it processed correctly? Right now, ubuntu seems to be just smoothing it all together, although other systems will parse it without a problem.

http://en.wikipedia.org/wiki/Shebang_%28Unix%29

describes the problem, but does not offer a solution.

+4
source share
1 answer

There is no good solution, since different unions process the verbose word #! lines in different ways. Portable #! use constraints for a maximum of one argument to the interpreter on #! string and spaces in the interpreter or argument.

If the language allows this, you can make a script shell script that takes care of loading the interpreter from any command line that it likes. For example, in Perl from the perl manual:

 #!/bin/sh -- # -*- perl -*- -p eval 'exec perl -wS "$0" ${1+" $@ "}' if $running_under_some_shell; 

The shell stops processing after the second line, and Perl sees lines 2-3 as an instruction that does nothing. Some lisp dialogs / schemas do #! ... !# comment to write

 #!/bin/sh exec guile -s "$0" " $@ " !# ;; scheme code starts here 

In general, the only solutions include two files. You can write #!/usr/bin/env mywrapper where mywrapper is a program (it could be a script) that calls the actual interpreter with whatever argument it wants. Or you can make the executable itself a script wrapper and save the interpreted file separately. The second solution has the advantage of working even if the interpreter does not accept the leading line #! .

+3
source

Source: https://habr.com/ru/post/1315854/


All Articles