dcsimg
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Integrating Bash with PHP : Page 3

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 Arrays between PHP and Bash

Bash provides one-dimensional array variables that you declare using the form:

   Array_name=(value1 ... valuen). 
You can then reference any element of an array using the syntax ${name[subscript]}. There is no maximum limit on the size of an array. For example, you can pass a set of arguments to a Bash script that groups them into arrays. The following PHP script (array.php) sends a list of integers:

   <?php
   $result=shell_exec(
      "C:\bash-2.03\bash.exe array.txt 10 2 13 88 76 34");
   echo($result);  
   ?>
The arrayNumber.txt Bash script in Listing 1 extracts the arguments and creates two Bash arrays (the Bash script also provides a few basic array operations):

The code in Listing 1 stores output into an arrayNumberResult.txt file. When you run the example, the output is:

   First numeric array is: 10 2 13
   
   Second numeric array is: 88 76 34 
   
   Length of first array is: 3
   
   Length of second array is: 3
   
   0 ---->     sum[0] = myArray1[0] + myArray2[0] *** 98
       product[0] = myArray1[0] * myArray2[0] *** 880
       average[0] = (myArray1[0] + myArray2[0])/2 *** 49
   ------------ 
   
   1 ---->     sum[1] = myArray1[1] + myArray2[1] *** 78
    product[1] = myArray1[1] * myArray2[1] *** 152
       average[1] = (myArray1[1] + myArray2[1])/2 *** 39
   ------------ 
   
   2 ---->     sum[2] = myArray1[2] + myArray2[2] *** 47
       product[2] = myArray1[2] * myArray2[2] *** 442
       average[2] = (myArray1[2] + myArray2[2])/2 *** 23
   ------------ 
   
   The sum of all elements of the first array is *** 25
   ------------ 
   
   The first array after deleting the second element is *** 10 13
   ------------
Listing 2 contains a similar example that uses string arrays and demonstrates operations over strings (add, paste, display length, and so on). The PHP script (array.php) that runs the Bash script is:

   <?php
   $result=shell_exec(
      "C:\bash-2.03\bash.exe array.txt red black white blue pink");
   echo($result);  
   ?>
When you run array.php script, the result stored in the arrayResult.txt file is:

   Length of first array is 2
   Length of second array is 3
   ------------ 
   red <--> white 
   red <--> blue 
   red <--> pink 
   black <--> white 
   black <--> blue 
   black <--> pink 
   ------------ 
   red 
   black 
   aqua 
   magenta 
   ------------ 
   redwhite 
   blackblue 
   aquapink 
   magenta 
   ------------  

Passing True PHP Arrays

If you want to pass a PHP array to Bash instead of a list of arguments, convert the PHP array into a string and pass it as shown below (you can use any element separator you like):

   
   <?php
   $nr = array("one", "two", "three", "four", "five");
   
   $separated = implode(":", $nr);
   echo 'The PHP array converted in a string: <br />';
   echo $separated."<br /> <br />";
   $result=shell_exec("C:\bash\bash-2.03\bash.exe arrayToBash.txt '".$separated."'");
   &#133;
   
Next, the Bash script will build an array from this string as follows:

   #!/bin/bash
   
   string=$1
   
   until [ "$token" =  "$string" ]
   do
     token=${string%%:*}   
     a[$i]=$token"BASH"          
     string=${string#*:}   
     ((i=i+1))
   done
   
   #echo "a[0]="${a[0]}
   #echo "a[1]="${a[1]}
   #echo "a[2]="${a[2]}
   #echo "a[3]="${a[3]}
   #echo "a[4]="${a[4]}
   
   echo ${a[@]}
PHP may reconstruct the array from the Bash string using code like this:

   //convert from Bash string to PHP array
   $pieces = explode(" ", $result);
   echo "<br /><br /> Convert from Bash string to PHP array: <br />";
   echo "pieces[0]=".$pieces[0]." <br />"; 
   echo "pieces[1]=".$pieces[1]." <br />"; 
   echo "pieces[2]=".$pieces[2]." <br />"; 
   echo "pieces[3]=".$pieces[3]." <br />"; 
   echo "pieces[4]=".$pieces[4]." <br />"; 
   ?>
The output is (notice that Bash script added the "BASH" text termination for every array element—this is just a flag to show that the Bash script worked):

   The PHP array converted in a string: 
   one:two:three:four:five
   
   The Bash array elements: 
   oneBASH twoBASH threeBASH fourBASH fiveBASH 
   
   Convert from Bash string to PHP array: 
   pieces[0]=oneBASH 
   pieces[1]=twoBASH 
   pieces[2]=threeBASH 
   pieces[3]=fourBASH 
   pieces[4]=fiveBASH
   Passing PHP objects to Bash


Thanks for your registration, follow us on our social networks to keep up-to-date