Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Integrating Bash with PHP : Page 4

Bash scripts can simplify many OS-oriented tasks for PHP developers. Even better, you can run Bash scripts directly from PHP, passing arguments, arrays, and receiving output that you can analyze or display.


advertisement

Passing PHP Object Members to Bash

Suppose you have the following SimpleClass PHP class, and you want to pass an instance's members to a Bash script:

<?php class SimpleClass { // member declaration public $phone = 3274927349; public $name = 'El'; public $surname = 'Ramon'; // method declaration public function displayVar() { //... } } $simpleClass = new SimpleClass(); //$simpleClass->displayVar(); ?>

Currently, Bash is not object-oriented, but you can use some tricks to pass PHP object members to Bash. One way is to pass the serialized version of an object, like this (obviously, in Bash you'd need to implement a de-serialize mechanism to extract the member names and values):

... echo "BASH call ...\n"; $result=shell_exec( "C:\bash-2.03\bash.exe objectBash.txt ". serialize($simpleClass); echo($result); ...

Another way is to pass the members explicitly, as shown below (items are separated by a colon):



... echo "BASH call ...\n"; $cmd = "C:\bash-2.03\bash.exe objectBash.txt ". $simpleClass->phone.":". $simpleClass->name.":". $simpleClass->surname; $result=shell_exec($cmd); echo($result); ...

With explicit passing, the objectBash.txt Bash script would look like this:

#!/bin/bash echo $* | ( IFS=: ; read phone name surname; echo "Phone = "$phone)

Using Win-Bash

Win-Bash is a Windows port of the GNU Bash that doesn't need any special environment or DLLs. The latest released version is Win-Bash 0.6. Installing Win-Bash is easy: just download the win-bash_0.6.zip archive and extract it to your hard drive. Then, to run Win-Bash, double-click the win-bash.exe file and you'll see the window in Figure 1.

 
Figure 1. The Win-Bash Command Prompt: Here's a typical Win-Bash command window.
To use Win-Bash into your PHP applications, set the path to the win-bash.exe file like this:

${WIN-BASH_HOME}\win-bash.exe

Then you can call a Win-Bash script as shown below:

<?php $result=shell_exec( "C:\win-bash\win-bash.exe {bash_script_name}.txt"); echo($result); ?>

Author's Note: Replace "bash_script_name" in the preceding code with any Bash script you like.

Using Cygwin

Cygwin is a tool that allows you to use a Linux environment on Windows systems. Cygwin ships with a DLL (cygwin1.dll) that acts as a Linux API emulation layer. It provides substantial Linux API functionality and a collection of tools that provide Linux "look and feel," and that conform to the official specification. When you download Cygwin, you get a setup.exe file that guides you through the installation process. You can choose to install Cygwin directly from the Internet or download it and install it later from a local directory. After you select and install the Cygwin packages you need, you can run Cygwin at the command prompt by double-clicking on the Cygwin.bat file. You'll see a window similar to Figure 2:

 
Figure 2. The Cygwin Command Prompt: When you launch Cygwin, you get a command-prompt window.
You can use Cygwin to run shell command scripts, login remotely to any PC, or fix problems within a Posize/Linux/UNIX shell on any Windows machine. If you like to use sophisticated shell command scripts you may use the standard shells: sed or awk.

To use Cygwin from PHP, simply set the path to the bash.exe file, for example:

${CYGWIN_HOME}\bin\bash.exe

A PHP script that executes a Cygwin Bash script will then look like this:

<?php $result=shell_exec( "C:\Cygwin\bin\bash.exe {bash_script_name}.txt"); echo($result); ?>

Again, remember to replace "bash_script_name" with whatever Bash script you need.

At this point, you should have the basic principles for calling Bash from PHP firmly in hand. You've seen examples showing how to call Bash scripts both with and without arguments of different types, how to call individual Bash functions from PHP, how to generate output in text files or in HTML, and how to parse and interpret Bash output in PHP. All that's left is to use your imagination when adding Bash to your PHP applications.



Octavia Andreea Anghel is a senior PHP developer currently working as a primary trainer for programming teams that participate at national and international software-development contests. She consults on developing educational projects at a national level. She is a coauthor of the book "XML Technologies—XML in Java" (Albastra, ISBN 978-973-650-210-1), for which she wrote the XML portions. In addition to PHP and XML, she's interested in software architecture, web services, UML, and high-performance unit tests.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap