I'm trying to give two commands on one line to shell_exec however there is no output. However, if passing only one command at a time this works perfectly fine:
$output = shell_exec('whoami');
echo($output); // This works
$output = shell_exec('dir');
echo($output); // This works as well
$output = shell_exec('whoami; dir');
echo($output); // No output...
What am I missing? I'm running a XAMPP (3.2.4.) environment on Windows 10 Build 20H2.
you can use the && sign to join the commands
$output = shell_exec('whoami && dir');
echo($output);
this should work pretty fine
I found the answer. The problem was that using the semicolon ";" between two commands only works on Linux Systems. Because I'm operating on Windows I had to use the "&" instead.
To add on our answer,You can create a workaround for os variance:
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { $commandSeparator='&&'; }
else { $commandSeparator=';'; }
then you can add it to the command.
$commandToExecute=$commandOne.$commandSeparator.$commandTwo;
$output=shell_exec($commandToExecute);
echo($output);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.