GOPHERSPACE.DE - P H O X Y
gophering on text.garden
# Python and shell script at the same time

I worked on a Python script recently where I figured out it would be
easier to offload some of the logic to Bash. On the other hand, I
didn't want to have to distribute several scripts. Previously I've
used here-documents piped into python for this, but this doesn't
work if you want to pipe data into your script.

I came up with this silly hack:

```
#!/bin/bash

vv=""""

# shell code!
python $0 # run the python code
# ...
exit $?

#"""

# python code!
print("Hello")
# ...
```

To the shell, this looks like a shell script that initially assigns
an empty string to vv, followed by the code, the exit and some
garbage at the end. To Python, it looks like vv being assigned a
multi-line string (containing the shell script) followed by the rest
of the program.

I just recalled that there is a similar trick for C code, to create
"executable" C code:

```
#ifdef 0
gcc -o a.tmp $0 && ./a.tmp
rm a.tmp
exit 0
#endif

int main(int argc, char **argv) {
// ...
}
```

These tricks are sure to make the doctors hate you.