Results 1 to 4 of 4

Thread: Passing PHP variables to bash script

  1. #1
    Join Date
    Jan 2006
    Beans
    51

    Passing PHP variables to bash script

    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:
    Code:
    <?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:
    Code:
    #!/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
    Last edited by craigp; November 13th, 2008 at 07:41 PM.

  2. #2
    Join Date
    Jan 2006
    Beans
    51

    Re: Passing PHP variables to bash script

    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

  3. #3
    Join Date
    Feb 2008
    Location
    Cape Town, South Africa
    Beans
    Hidden!
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: Passing PHP variables to bash script

    Quote Originally Posted by craigp View Post
    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

  4. #4
    Join Date
    Jan 2006
    Beans
    51

    Re: Passing PHP variables to bash script

    ahh i'll edit that.. thanks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •