SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Addict Smola's Avatar
    Join Date
    Mar 2005
    Posts
    260
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy putenv() is saving quotes?

    Hello,

    I am having some very unexpected results while trying to create a custom CLI using PHP. Let's forgo any "Why would you do that?" questions for the moment.

    Code PHP:
    $cmd = 'FOO="bar"';
    putenv($cmd);
     
    //this is actually defined in a class
    function _($cmd, $returnType = RET_OBJECT) {
            $d['output'] = '';
            $d['error'] = '';
            $descriptorspec = array(0 => array('pipe','r'),
                                    1 => array('pipe','w'),
                                    2 => array('pipe','w'));
            $process = proc_open($cmd,$descriptorspec,$pipes);
            if (is_resource($process)) {
     
                //fwrite($pipes[0]);
                fclose($pipes[0]);
     
                $d['output'] = stream_get_contents($pipes[1]);
                fclose($pipes[1]);
     
                $d['error'] = stream_get_contents($pipes[2]);
                fclose($pipes[2]);
     
                proc_close($process);
            } else {
                Er::ror('Could not open process.',0);  //custom error class
            }
            echo 'test output: '.$d['output'];   //just for testing, but returns the same as the returned object
            return Util::convertData($d,$returnType);   //custom data converter
    }
     
    //the problem (don't use return object. instead, utilize the test echo statement):
    _('echo "this is my foo${FOO}"');
    // test output: this is my foo"bar"

    I can't figure out why the quotes are carrying over! I tried changed the double quotes to singles. I have tried double quoting. My last resort is to create a bash script on the fly and import it. I am confused. Any thoughts?
    Humbly,

    Smola

  2. #2
    SitePoint Addict Smola's Avatar
    Join Date
    Mar 2005
    Posts
    260
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Small update after a bit more research. I'm pretty sure this is a fundamental UNIX issue and I'm just not understanding, but I think something is quoting my environment variables automatically. I tried:

    PHP Code:
    //this will output as expected without quotes.
    $cmd 'FOO=bar and bar'

    //if i set up another environment variable and try to use it in another environment variable, it treats it as a string. is this even possible?
    $cmd2 'BAR=you like my ${FOO}'
    // when i echo $BAR it comes out with:  test output: you like my ${FOO} 
    I've tried single/double quoting those strings as well, with/without curly braces to no avail.
    Humbly,

    Smola


Tags for this Thread

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
  •