On 2012-01-20, crankypuss <n...@email.thanks> wrote:
probably the best you can do is put is use /bin/sh as the interpreter
abd put
[ -e /usr/bin/php ] && exec /usr/bin/php "$0" "$@"
near at top of the script
The next line should check for bash (is it's not itself bash, it
should exec bash)
you're guaranteed /bin/sh (which culd be ash, dash etc for example)
/bin/bash and /usr/bin/php are both optional.
getting bash and php to understand the script to mean the same thing
could be tricky, both are pretty fussy about syntax.
having explained why it's impossible here's how to almost do it!
#!/bin/sh
#<?/* bilingual script bash/php
[ -e /usr/bin/php ] && exec /usr/bin/php "$0" "$@"
[ -z "$BASH_VERSION" -a -e /bin/bash ] && exec /bin/bash - "$0" "$@"
# bash/sh code starts here...
[ -z "$BASH_VERSION" ] && echo "no valid interpreter" >&2 && exit 1
# pure bash code starts here
echo hello from bash, args are "$0" "$*"
exit
exec true <<EOF*/ #PHP starts here
echo "hello from php, args follow.\n";
print_r($argv);
/* end-of-file marker:
EOF*/
you'll note that php emits the '#' from the second line, I've not
found a syntax that can exclude that which will still run with bash.
there are also some restrictions, the bash part can't cointain the string */
and the PHP part can't contain the string EOF*/ at the start of a line.
other than that pretty-much anything is allowed.