Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trick shebang to allow multiple parameters?

Is it know that most shebang implementations will support a single parameter so if you have something like

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

It will now work as expected because it will call the tool with "param1 param2" argument instead of splitting it into two arguments.

It seems that one workaround practice is to use something like:

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

Now this approach would make YAML-based script invalid due to the 2nd line, so the only acceptable workaround would be one that is also a comment, starting with "#" too.

Is there a way to bypass this issue too?

like image 261
sorin Avatar asked Jul 21 '17 12:07

sorin


People also ask

Can a script have more than one shebang?

We can't have multiple shebang lines - there can only be one and it should always be the first line. If you need to support multiple versions of Python based on OS, it is best to write a small shell wrapper that invokes your python script with the right interpreter, probably with an exec .

Is shebang ignored?

The shebang line is usually ignored by the interpreter, because the "#" character is a comment marker in many scripting languages; some language interpreters that do not use the hash mark to begin comments still may ignore the shebang line in recognition of its purpose.


1 Answers

A 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
like image 51
Nahuel Fouilleul Avatar answered Nov 15 '22 05:11

Nahuel Fouilleul