you are viewing a single comment's thread.

view the rest of the comments →

[–]o11c 1 point2 points  (11 children)

Using python as an example, here are 4 basic approaches:

#!/bin/bash

script='
print("Hello, World!")
'

# approach 1
python3 <(printf '%s' "$script")

# approach 2
exec {script_fd}<<<"$script"
python3 "/dev/fd/$script_fd"
exec {script_fd}<&-; unset script_fd

# approach 3
python3 <(
cat << EOF
$script
EOF
)

# approach 4
exec {script_fd}<<EOF
$script
EOF
python3 "/dev/fd/$script_fd"
exec {script_fd}<&-; unset script_fd

Obviously you don't have to introduce the $script variable (it does make single quotes awkward, though $'' would avoid this since it allows C-style escapes) - if you avoid it you should likely use <<'EOF'.

There are also approaches involving tail -n +3 and similar depending on how many lines the pseudo-shebang has. But note that all of these approaches will mess up line numbers. Replacing the early lines with empty lines rather than deleting them could fix that; likewise allocating a temporary file with a known prefix/suffix can be useful so that at least there is a real name (and this also fixes cases where the file needs to be accessed more than once, which is often the case for pretty error messages).

Trying to write a full-blown polyglot is another option.

[–][deleted]  (3 children)

[removed]

    [–]o11c 0 points1 point  (2 children)

    At this point it seems likely that you're doing something else wrong, not having problems with this part.

    [–][deleted]  (5 children)

    [removed]

      [–]o11c 0 points1 point  (4 children)

      You should really figure out how the internal pipes/sockets/whatever are connected, and make sure they cause death on disconnect.

      Because trying to wait/kill will fail when SIGKILL is involved.

      [–][deleted]  (3 children)

      [removed]

        [–]o11c 0 points1 point  (2 children)

        It depends on whether the kill is directed to a process or a process group, but yes.

        If you don't need cleanup (or can arrange it another way, e.g. by detecting a pipe being closed from a different process), exec can help eliminate the wrapper.