How to trick shebang to allow multiple parameters?

Did he know that most shebang implementations will support one parameter, so if you have something like

 #!/usr/bin/env some-tool-accepting-yaml param1 param2 ... (yaml body) 

Now it will work as expected, because it will call the tool with the argument param1 param2 instead of breaking it into two arguments.

One of the workarounds seems to be to use something like:

 #!/bin/sh arbitrary_long_name==0 "exec" "/usr/bin/gawk" "--re-interval" "-f" "$0" " $@ " 

Now this approach will result in the YAML based script being invalidated due to the second line, so the only acceptable workaround would be one that is also a comment, starting with "#".

Is there a way around this problem too?

+5
source share
1 answer

General solution without using polyglot scripts

launcher.sh

 #!/bin/bash # first argument to be split if [[ $- != *f* ]]; then reset=1; fi set -f arg=( $1 ) shift if [[ $reset = 1 ]]; then set +f; fi # other arguments arg+=(" $@ ") # launch command exec "${arg[@]}" 

script

 #!/path/to/launcher.sh interpreter opts 
+3
source

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


All Articles