PDA

View Full Version : Passing PHP variables to bash script



craigp
November 13th, 2008, 07:31 PM
I am trying to pass variables from php to bash and im not sure how to get this to work. this is what i am trying:

PHP:

<?php
echo '<pre>';
$var1="hello";
$var2="joe";
putenv("VAR1=$var1");
$last_line = system('/home/www/public_html/domain.com/script/ls $var1 $var2', $retval);
echo '
</pre>
<hr />Last line of the output: ' . $last_line . '
<hr />Return value: ' . $retval;
?>

BASH:

#!/bin/bash

ls
sudo mkdir test
sudo chown -R www-data:www-data /home/www/public_html/domain.com/script/test
echo -e "var1: $1, var2: $1"

but nothing seems to get passed to bash. I want to be able to do this to create new folders, give ownership and permission changes to those folders, and later create new virtualhosts with a php interface.

Cheers

craigp
November 13th, 2008, 07:45 PM
nevermind i figured it out.. i had to use a diffent type of quotes

$last_line = system("/home/www/public_html/domain.com/script/ls $var1 $var2", $retval);

i'll leave it up for anyone else

drubin
November 13th, 2008, 08:44 PM
nevermind i figured it out.. i had to use a diffent type of quotes

$last_line = system("/home/www/public_html/domain.com/script/ls $var1 $var2", $retval);

i'll leave it up for anyone else

Just so you and any one else understands. Double quotes are parsed as if their contents were php.

Yours could have been written with single quotes using concat.
system('/home/www/public_html/domain.com/script/ls '.$var1.' '.$var2, $retval);
http://www.php.net/types.string

craigp
November 13th, 2008, 09:07 PM
ahh i'll edit that.. thanks