Mimic Bash Function Calls from PHP
In Bash, functions look like mini-scripts: they may accept arguments, they can use variables known only in the function's body (using the
local shell built-in), and they can return values to the calling entities. Within functions, arguments are treated in the same manner as arguments passed to the script.
The next application uses a recursive function to calculate a sum of 20 consecutive integer numbers. The Bash script
function.txt containing the recursive function
sum appears below:
#!/bin/bash
# Define the recursive function
sum ()
{
# Declaring local variable
local num=$1;
if [ "$num" = 0 ] ; then
echo 0
return ;
fi;
# The recursive call of the sum function
echo $[ $num + $(sum $[ $num - 1 ])]
}
echo "Sum of 1+2+3+...+20 is ***"
# Call the recursive function sum
sum 20
The PHP script
function.php, which calls the above Bash script (and should look familiar by now), is:
<?php
$result=shell_exec("C:\bash-2.03\bash.exe function.txt");
echo($result);
?>
The output is:
Sum of 1+2+3+...+20 is *** 210
Note that in the previous example, the PHP script calls the Bash script, NOT the Bash function itself. The function gets called from the last line of the Bash script. That's all very well for Bash scripts that contain only one function, but when you have more than one function in the Bash script, you have to use a trick. For example, suppose that you have a Bash script containing two functions. The first function calculates the factorial of a given number and the second is the
sum function from the previous example. The following
functionsCall.txt script shows the code:
#!/bin/bash
fact ()
{
local num=$1;
if [ "$num" = 1 ] ; then
echo 1 # listing fact from 1
return ;
fi;
# The recursive call of the fact function
echo $[ $num * $(fact $[ $num - 1 ])]
}
sum ()
{
# Declaring local variable
local num=$1;
if [ "$num" = 0 ] ; then
echo 0
return ;
fi;
# The recursive call of the sum function
echo $[ $num + $(sum $[ $num - 1 ])]
}
If you want to call only one of the two contained functions, then you must pass the Bash script the desired function name and the appropriate arguments. For example, from PHP you would call the
fact function by passing the function name ("fact") as the first argument, and following that with the arguments required by the function (in this case,
3):
<?php
$result=shell_exec(
"C:\bash-2.03\bash.exe functionsCall.txt fact 3");
echo($result);
?>
You also have to add a chunk of code in the Bash script that's dedicated to parsing the received arguments, extracting the function name that the caller wants to run:
# Check what function was called from PHP
# and call that function
if [ "$1" = fact ] ; then
printf "The function -- $1 -- "
printf "was called from PHP and the result is : "
#Call the fact function using $2 as argument
fact $2
else
printf "The function -- $1 -- "
printf "was called from PHP and the result is : "
#Call the sum function using $2 as argument
sum $2
fi;
The output is:
The function -- fact -- was called
from PHP and the result is : 6
To call the
sum function instead, with the argument
20, all you need to do is replace the line that makes the call with:
$result=shell_exec(
"C:\bash-2.03\bash.exe functionsCall.txt sum 20");
And the result will be:
The function -- sum -- was called
from PHP and the result is: 210
You aren't limited to passing single values as arguments; you can pass arrays to Bash as well.