The only way I could see this 'combined' is by using a here-doc, which basically causes the first script to generate the second, 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
In that example, $1
will expand to the first argument. I have escaped the other variables (that are related only to PHP), which PHP will expand when it runs. $$
in a shell script just expands to the PID of the script, the actual temporary file is going to be something like /tmp/1234.php
. mktemp(1)
is a much safer way to make a temporary file name that is more resistant to link attacks and collisions.
It also saves the exit status of PHP in retval
, which is then returned when the script exits.
The resulting file will look like this (assuming the first argument to the shell script is foo
):
<?php
$string="foo";
echo "\nHello " . $string . "\n";
?>
This is kind of an icky demonstration for how to use bash to write other scripts, but at least demonstrates that its possible. Its the only way I could think of to 'combine' (as you indicated) the two scripts that you posted.