PHP & bash; Linux; Compile my own function
I would like to make my own program, but I have no idea how ... for example, I want to create a typical Hello $ user program.
So..
├── hi
│ ├── hi.sh
│ ├── hi_to.sh
hi.sh
#!/bin/bash
~/hi/hi_to.sh $1
hi_to.sh
#!/usr/bin/php
<?php
echo "\nHellO ".$argv[1]."\n";
?>
Run it in terminal:
me:~/hi
→ ./hi.sh User
HellO User
and my question is, how do I compile all these files into one bash program?
a source to share
- Make sure the shebang line points to the correct php executable
- You don't need to call script hi.php, just call it
hi
- Create an executable script (e.g. via
chmod u+x path/to/hi
orchmod a+rx path/to/hi
, see http://en.wikipedia.org/wiki/Chmod ) - Make sure the file is in the PATH search for the users / accounts your script should be using (without entering an absolute path)
a source to share
The only way I could see this "combined" is to use here-doc, which basically makes the first script generate the second and then execute it:
#!/bin/sh
cat << EOF > /tmp/$$.php
<?php
\$string="$1";
echo "\nHellO ". \$string ."\n";
?>
EOF
/usr/bin/php -q /tmp/$$.php
retval=$?
rm /tmp/$$.php
exit $retval
This example $1
will expand to the first argument. I have avoided other variables (which are only PHP related) that PHP will expand when it runs. $$
in a shell script just expands to the PID script, the actual temp file would be something like /tmp/1234.php
. mktemp(1)
is a much safer way to create a temporary filename that is more resistant to attacks and link conflicts.
It also stores the PHP exit status in retval
, which is then returned after the script exits.
The resulting file will look like this (if the first argument to the shell script is foo
):
<?php
$string="foo";
echo "\nHello " . $string . "\n";
?>
This is kind of a bad demonstration of how to use bash to write other scripts, but at least it demonstrates that it is possible. This is the only way I could think of to "merge" (as you pointed out) the two scripts you posted.
a source to share