Call bash Script Inside PHP Web Page

Thu. July 17, 2008
Categories: Development, Linux
Tags: ,

Other Languages:


Here is a simple way to call a simple bash script inside php script page and have a return value printed to your web page.

Below is a simple bash script called php.sh

1
2
3
#!/bin/bash
echo "Successful call from PHP : "
exit 0

chmod 700 so it would be root executable, and saved it to your apache root folder say /var/www/html/

And here’s a simple browseable php script called test.php

1
2
3
4
5
6
7
8
9
10
$program = "/var/www/html/php.sh";
$ver1 = system($program, $retval);
if ($retval == 0 ) {
echo "Returned 0";
}
else {
echo "Returned not zero";
}
die;
?>

Saved to same root location where your php.sh currently resides.

If you browse the page test.php, it would have a return value spitted out by php.sh bash script into test.php web page.

Comments