bilingual scripts, bash/PHP?
The group you are posting to is a
Usenet group . Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Newsgroups: alt.os.linux
From:
crankypuss <n... @email.thanks>
Date: Fri, 20 Jan 2012 02:48:23 -0700
Local: Fri, Jan 20 2012 4:48 am
Subject: bilingual scripts, bash/PHP?
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?
You must
Sign in before you can post messages.
You do not have the permission required to post.
Newsgroups: alt.os.linux
From:
Richard Kettlewell <r... @greenend.org.uk>
Date: Fri, 20 Jan 2012 10:26:30 +0000
Local: Fri, Jan 20 2012 5:26 am
Subject: Re: bilingual scripts, bash/PHP?
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
fi
# ...rest of bash script...
-- http://www.greenend.org.uk/rjk/
You must
Sign in before you can post messages.
You do not have the permission required to post.
Newsgroups: alt.os.linux
From:
Richard Kettlewell <r... @greenend.org.uk>
Date: Fri, 20 Jan 2012 10:53:56 +0000
Local: Fri, Jan 20 2012 5:53 am
Subject: Re: bilingual scripts, bash/PHP?
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/
You must
Sign in before you can post messages.
You do not have the permission required to post.
Newsgroups: alt.os.linux
From:
crankypuss <n... @email.thanks>
Date: Fri, 20 Jan 2012 05:26:16 -0700
Local: Fri, Jan 20 2012 7:26 am
Subject: Re: bilingual scripts, bash/PHP?
On 01/20/2012 03:53 AM, Richard Kettlewell wrote:
> 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.
Thanks, I thought there might be some funky commenting conventions set
up so that whichever language is there sees only its own code.
You must
Sign in before you can post messages.
You do not have the permission required to post.
Newsgroups: alt.os.linux
From:
Jasen Betts <ja... @xnet.co.nz>
Date: 20 Jan 2012 12:24:33 GMT
Local: Fri, Jan 20 2012 7:24 am
Subject: Re: bilingual scripts, bash/PHP?
On 2012-01-20, crankypuss <n
... @email.thanks> wrote:
> 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?
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 n... @netfront.net ---
You must
Sign in before you can post messages.
You do not have the permission required to post.
Newsgroups: alt.os.linux
From:
Eef Hartman <E.J.M.Hart... @tudelft.nl>
Date: Fri, 20 Jan 2012 14:12:34 +0100
Local: Fri, Jan 20 2012 8:12 am
Subject: Re: bilingual scripts, bash/PHP?
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.Hart
... @tudelft.nl - phone: +31-15-27 82525 **
******************************************************************
You must
Sign in before you can post messages.
You do not have the permission required to post.
Newsgroups: alt.os.linux
From:
Eef Hartman <E.J.M.Hart... @tudelft.nl>
Date: Fri, 20 Jan 2012 14:15:40 +0100
Local: Fri, Jan 20 2012 8:15 am
Subject: Re: bilingual scripts, bash/PHP?
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).
--
******************************************************************
** Eef Hartman, Delft University of Technology, dept. SSC/ICT **
** e-mail: E.J.M.Hart
... @tudelft.nl - phone: +31-15-27 82525 **
******************************************************************
You must
Sign in before you can post messages.
You do not have the permission required to post.
Newsgroups: alt.os.linux
From:
Richard Kettlewell <r... @greenend.org.uk>
Date: Fri, 20 Jan 2012 14:02:36 +0000
Local: Fri, Jan 20 2012 9:02 am
Subject: Re: bilingual scripts, bash/PHP?
Eef Hartman <E.J.M.Hart
... @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/
You must
Sign in before you can post messages.
You do not have the permission required to post.
Newsgroups: alt.os.linux
From:
J G Miller <mil... @yoyo.ORG>
Date: Fri, 20 Jan 2012 15:45:37 +0000 (UTC)
Local: Fri, Jan 20 2012 10:45 am
Subject: Re: bilingual scripts, bash/PHP?
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.
You must
Sign in before you can post messages.
You do not have the permission required to post.
Newsgroups: alt.os.linux
From:
Jasen Betts <ja... @xnet.co.nz>
Date: 20 Jan 2012 20:13:25 GMT
Local: Fri, Jan 20 2012 3:13 pm
Subject: Re: bilingual scripts, bash/PHP?
On 2012-01-20, crankypuss <n
... @email.thanks> wrote:
> 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?
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.
-- ⚂⚃ 100% natural
--- Posted via news://freenews.netfront.net/ - Complaints to n... @netfront.net ---
You must
Sign in before you can post messages.
You do not have the permission required to post.
Newsgroups: alt.os.linux
From:
crankypuss <n... @email.thanks>
Date: Fri, 20 Jan 2012 14:09:18 -0700
Local: Fri, Jan 20 2012 4:09 pm
Subject: Re: bilingual scripts, bash/PHP?
On 01/20/2012 01:13 PM, Jasen Betts wrote:
> On 2012-01-20, crankypuss<n
... @email.thanks> wrote:
>> 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?
> 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.
Thanks.
You must
Sign in before you can post messages.
You do not have the permission required to post.