Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

bilingual scripts, bash/PHP?

24 views
Skip to first unread message

crankypuss

unread,
Jan 20, 2012, 4:48:23 AM1/20/12
to
Okay, a bash script has to start with this:

#!/bin/bash

And a PHP script begins suchly:

#!/usr/bin/php
<?php
// code
?>

The question is, how can one code a script that will run PHP if it's
available and if not fall back to bash?

Richard Kettlewell

unread,
Jan 20, 2012, 5:26:30 AM1/20/12
to
Possibly easier to do it the other way around (certainly so for me since
I don't speak PHP).

#! /bin/bash
set -e
if type php >/dev/null 2>&1; then
exec php /path/to/php/script
fi
# ...rest of bash script...

--
http://www.greenend.org.uk/rjk/

Richard Kettlewell

unread,
Jan 20, 2012, 5:53:56 AM1/20/12
to
Richard Kettlewell <r...@greenend.org.uk> writes:

> crankypuss <n...@email.thanks> writes:
>> Okay, a bash script has to start with this:
>>
>> #!/bin/bash
>>
>> And a PHP script begins suchly:
>>
>> #!/usr/bin/php
>> <?php
>> // code
>> ?>
>>
>> The question is, how can one code a script that will run PHP if it's
>> available and if not fall back to bash?
>
> Possibly easier to do it the other way around (certainly so for me since
> I don't speak PHP).
>
> #! /bin/bash
> set -e
> if type php >/dev/null 2>&1; then
> exec php /path/to/php/script

Should be:
exec php /path/to/php/script "$@"
of course, to be fully general.

--
http://www.greenend.org.uk/rjk/

crankypuss

unread,
Jan 20, 2012, 7:26:16 AM1/20/12
to
Thanks, I thought there might be some funky commenting conventions set
up so that whichever language is there sees only its own code.

Jasen Betts

unread,
Jan 20, 2012, 7:24:33 AM1/20/12
to
no. the kernels's going to try to launch whatever interpreter you name on
the first line.

--
⚂⚃ 100% natural

--- Posted via news://freenews.netfront.net/ - Complaints to ne...@netfront.net ---

Eef Hartman

unread,
Jan 20, 2012, 8:12:34 AM1/20/12
to
crankypuss <n...@email.thanks> wrote:
> Thanks, I thought there might be some funky commenting conventions set
> up so that whichever language is there sees only its own code.

The #! line in a script only tells the running shell (the cli)
_which_ program to start to execute this script.
The default, when no #! first line is present is "a copy of itself",
so bash if your default shell is (ba)sh or tcsh when THAT is your
default shell (or even ksh when you're using that).
That is also why you can also use <?php in .html webpages, as long
as the httpd knows how to find php TO execute it with.
--
******************************************************************
** Eef Hartman, Delft University of Technology, dept. SSC/ICT **
** e-mail: E.J.M....@tudelft.nl - phone: +31-15-27 82525 **
******************************************************************

Eef Hartman

unread,
Jan 20, 2012, 8:15:40 AM1/20/12
to
Jasen Betts <ja...@xnet.co.nz> wrote:
> no. the kernels's going to try to launch whatever interpreter
^^^^^^^
shell, the kernel doesn't start any scripts, can only handle
binary executables (fork/exec calls etc, scripts are handled
through the system call, which will start a shell).

Richard Kettlewell

unread,
Jan 20, 2012, 9:02:36 AM1/20/12
to
Eef Hartman <E.J.M....@tudelft.nl> writes:
> Jasen Betts <ja...@xnet.co.nz> wrote:

>> no. the kernels's going to try to launch whatever interpreter
> ^^^^^^^
> shell, the kernel doesn't start any scripts, can only handle
> binary executables (fork/exec calls etc, scripts are handled
> through the system call, which will start a shell).

That is not correct. #! is implemented by the kernel.

--
http://www.greenend.org.uk/rjk/

J G Miller

unread,
Jan 20, 2012, 10:45:37 AM1/20/12
to
On Friday, January 20th, 2012, 14:02:36h +0000, Richard Kettlewell wrote:

> #! is implemented by the kernel.

So the kernel then calls the appropriate interpreter viz
sh, perl, wish etc, yes?

And if you set up binfmt kernel module appropriately, you
can get Windoze programs to run via wine but without having
to specify the use of Wine.

Jasen Betts

unread,
Jan 20, 2012, 3:13:25 PM1/20/12
to
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.

crankypuss

unread,
Jan 20, 2012, 4:09:18 PM1/20/12
to
> [ -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.

Thanks.

0 new messages