什么是这个php exec()的字符串与变量结果解释?

Out of curiosity, what is going on here?

Either I'm really tired, or the only difference I can see is that 2nd exec uses a string, and the previous one uses a variable of the same string.

//code

    $command = "sudo /bin/mount /dev/sdd1 /a 2>&1";
    echo "[mountDev] command=$command
";
    exec($command,$ouput,$ret_val);
    debugArr("mountDev",$output,$ret_val);

    $new_command="sudo /bin/mount /dev/sdd1 /a 2>&1";
    echo "[mountDev] comman2=$new_command
";
    exec("sudo /bin/mount /dev/sdd1 /a 2>&1",$output,$ret_val); 
    // error handling
    debugArr("mountDe2",$output,$ret_val);

//output

[mountDev] device=/dev/sdd1
[mountDev] command=sudo /bin/mount /dev/sdd1 /a 2>&1
[mountDev] output=
[mountDev] retval=32
[mountDev] comman2=sudo /bin/mount /dev/sdd1 /a 2>&1
[mountDe2] output=Array
(
    [0] => mount: you must specify the filesystem type
)
[mountDe2] retval=32

Error is in exec($command,$ouput,$ret_val);. You missed the t of output. :P

You do have 2 options,

  1. Its better to use proc_open than exec. Because you dont have to add those 2>&1 to capture stderr.

  2. Also you are using sudo in exec. That means if the user who is running your php file must be in /etc/sudoers file. Better to use a root privileged daemon that runs commands for you and give you the output. You need to implement interprocess communication.