
his article discusses how to install and configure three Bash distributions for Windows: the GNU BASH shell for Windows, the Win-Bash Windows port, and the Cygwin tool. It also demonstrates how to execute Bash scripts using PHP, how to send different types of arguments to a Bash script, how to use Bash functions from PHP, and how to save results to a text file. (For information about other Bash distributions, see the sidebar
Bash Availability.)
Download and Configure GNU Bash Shell for Windows
First,
download the GNU Bash shell for Windows. The tool is ready to use immediately after you extract the ZIP archive in your favorite location (for example,
C:\bash-2.03\). To simplify calling Bash, you can add the
bash.exe path to your
CLASSPATH setting.
Here's a standard
HelloWorld Bash script:
#!/bin/bash
echo "Hello World!"
Name this script
test.txt (in Windows it's most convenient to save Bash scripts as simple
.txt files) and then open an MS-DOS command prompt. Navigate to the Bash home folder and type:
>bash ${PATH_TO_SCRIPT}/test.txt
Author's Note: Replace the PATH_TO_SCRIPT with the complete path to hello.txt): |
If you get the "Hello World" message then you have successfully executed your first Bash script. Now you can go further and add PHP into the equation.
Call Bash Scripts from PHP Using the shell_exec Function
To execute a Bash script using PHP use the
shell_exec function, which has the following prototype:
string shell_exec ( string $cmd ): Execute a command through
the shell and return the complete output as a string.
The $cmd parameter represents the command which will be executed.
Author's Note: The shell_exec function is disabled when PHP is running in safe mode. |
Here's a more complete example. This PHP application calls the
HelloWorld Bash script created earlier:
<?php
$result=shell_exec("C:\bash-2.03\bash.exe test.txt");
echo($result);
?>
When you run this, the output is:
Hello World!
Here's a slightly more complex script that passes two integer values to a Bash script, which uses them to do some simple calculations, and then returns the results:
<?php
$result=shell_exec("C:\bash-2.03\bash.exe values.txt 2 4");
echo($result);
?>
The preceding script sends two sample values,
2 and
4, to the Bash script
values.txt, which contains the following code:
#!/bin/bash
a=$1
b=$2
echo 'sum = '$((a+b))
echo 'product = '$((a*b))
echo 'average = '$(((a+b)/2))
The output is:
sum = 6 product = 8 average = 3
You can send as many arguments as you like to a Bash script. Bash extracts them using positional arguments such as
$1,
$2
,
$n. In the preceding example, when the script runs,
$1 = 2 and
$2 = 4.
PHP, HTML, and Bash
Bash scripts may be as complex as you like. For example, the next application generates a simple HTML document using a Bash script called from PHP. Here's the PHP script, called
bashPHP.php:
<?php
$result=shell_exec("
C:\bash-2.03\bash.exe bashHTML.txt");
echo($result);
?>
And here's the Bash script that generates the HTML (
bashHTML.txt):
#!/bin/bash
# our html code
echo "<html>"
echo "<head><title>BASH-HTML</title></head>"
echo "<body>"
echo "<h4>This page comes from Bash</h4>"
echo "Hello Everybody!"
echo "</body>"
echo "</html>"
The output that appears in the browser is:
This page comes from Bash
Hello Everybody!