如何检查PHP中是否存在shell命令

时间:2021-01-06 01:13:54

I need something like this in php:

在php中我需要这样的东西:

If (!command_exists('makemiracle')) {
  print 'no miracles';
  return FALSE;
}
else {
  // safely call the command knowing that it exists in the host system
  shell_exec('makemiracle');
}

Are there any solutions?

有什么解决方案吗?

6 个解决方案

#1


36  

On Linux/Mac OS Try this:

在Linux/Mac操作系统上试试这个:

function command_exist($cmd) {
    $return = shell_exec(sprintf("which %s", escapeshellarg($cmd)));
    return !empty($return);
}

Then use it in code:

然后在代码中使用:

if (!command_exist('makemiracle')) {
    print 'no miracles';
} else {
    shell_exec('makemiracle');
}

Update: As suggested by @camilo-martin you could simply use:

更新:正如@camilo-martin建议的,您可以使用:

if (`which makemiracle`) {
    shell_exec('makemiracle');
}

#2


11  

Windows uses where, UNIX systems which to allow to localize a command. Both will return an empty string in STDOUT if the command isn't found.

Windows使用的是UNIX系统,允许对命令进行本地化。如果没有找到该命令,两者都将返回STDOUT中的空字符串。

PHP_OS is currently WINNT for every supported Windows version by PHP.

PHP_OS目前是由PHP支持的所有Windows版本的WINNT。

So here a portable solution:

这里有一个便携式解决方案:

/**
 * Determines if a command exists on the current environment
 *
 * @param string $command The command to check
 * @return bool True if the command has been found ; otherwise, false.
 */
function command_exists ($command) {
  $whereIsCommand = (PHP_OS == 'WINNT') ? 'where' : 'which';

  $process = proc_open(
    "$whereIsCommand $command",
    array(
      0 => array("pipe", "r"), //STDIN
      1 => array("pipe", "w"), //STDOUT
      2 => array("pipe", "w"), //STDERR
    ),
    $pipes
  );
  if ($process !== false) {
    $stdout = stream_get_contents($pipes[1]);
    $stderr = stream_get_contents($pipes[2]);
    fclose($pipes[1]);
    fclose($pipes[2]);
    proc_close($process);

    return $stdout != '';
  }

  return false;
}

#3


3  

You could use is_executable to check whether it is executable, but you need to know the path of the command, which you could use which command to get it.

您可以使用is_executive来检查它是否可执行,但是您需要知道命令的路径,您可以使用哪个命令来获取它。

#4


3  

Platform independent solution:

平*立的解决方案:

function cmd_exists($command)
{
    if (\strtolower(\substr(PHP_OS, 0, 3)) === 'win')
    {
        $fp = \popen("where $command", "r");
        $result = \fgets($fp, 255);
        $exists = ! \preg_match('#Could not find files#', $result);
        \pclose($fp);   
    }
    else # non-Windows
    {
        $fp = \popen("which $command", "r");
        $result = \fgets($fp, 255);
        $exists = ! empty($result);
        \pclose($fp);
    }

    return $exists;
}

#5


0  

function checkIfCommandExists($cmd){
    $prefix = strpos(strtolower(PHP_OS),'win') > -1 ? 'where' : 'which';
    exec("{$prefix} {$cmd}", $output, $returnVal);
    $returnVal !== 0
}

this one is crossplatform solution using the return value of "where" and "which" :)

这个是使用“where”和“which”的返回值来实现的crossplatform解决方案。

#6


-3  

No, there are not.

不,没有。

Even when having direct access to a shell, you do not know if a command exists. There a some tricks like wheris or find / -name yourcommand but that not a 100% guarantee that you can execute the command.

即使直接访问shell,也不知道是否存在命令。有一些技巧,比如where is或find / -name命令,但是不能100%保证您可以执行命令。

#1


36  

On Linux/Mac OS Try this:

在Linux/Mac操作系统上试试这个:

function command_exist($cmd) {
    $return = shell_exec(sprintf("which %s", escapeshellarg($cmd)));
    return !empty($return);
}

Then use it in code:

然后在代码中使用:

if (!command_exist('makemiracle')) {
    print 'no miracles';
} else {
    shell_exec('makemiracle');
}

Update: As suggested by @camilo-martin you could simply use:

更新:正如@camilo-martin建议的,您可以使用:

if (`which makemiracle`) {
    shell_exec('makemiracle');
}

#2


11  

Windows uses where, UNIX systems which to allow to localize a command. Both will return an empty string in STDOUT if the command isn't found.

Windows使用的是UNIX系统,允许对命令进行本地化。如果没有找到该命令,两者都将返回STDOUT中的空字符串。

PHP_OS is currently WINNT for every supported Windows version by PHP.

PHP_OS目前是由PHP支持的所有Windows版本的WINNT。

So here a portable solution:

这里有一个便携式解决方案:

/**
 * Determines if a command exists on the current environment
 *
 * @param string $command The command to check
 * @return bool True if the command has been found ; otherwise, false.
 */
function command_exists ($command) {
  $whereIsCommand = (PHP_OS == 'WINNT') ? 'where' : 'which';

  $process = proc_open(
    "$whereIsCommand $command",
    array(
      0 => array("pipe", "r"), //STDIN
      1 => array("pipe", "w"), //STDOUT
      2 => array("pipe", "w"), //STDERR
    ),
    $pipes
  );
  if ($process !== false) {
    $stdout = stream_get_contents($pipes[1]);
    $stderr = stream_get_contents($pipes[2]);
    fclose($pipes[1]);
    fclose($pipes[2]);
    proc_close($process);

    return $stdout != '';
  }

  return false;
}

#3


3  

You could use is_executable to check whether it is executable, but you need to know the path of the command, which you could use which command to get it.

您可以使用is_executive来检查它是否可执行,但是您需要知道命令的路径,您可以使用哪个命令来获取它。

#4


3  

Platform independent solution:

平*立的解决方案:

function cmd_exists($command)
{
    if (\strtolower(\substr(PHP_OS, 0, 3)) === 'win')
    {
        $fp = \popen("where $command", "r");
        $result = \fgets($fp, 255);
        $exists = ! \preg_match('#Could not find files#', $result);
        \pclose($fp);   
    }
    else # non-Windows
    {
        $fp = \popen("which $command", "r");
        $result = \fgets($fp, 255);
        $exists = ! empty($result);
        \pclose($fp);
    }

    return $exists;
}

#5


0  

function checkIfCommandExists($cmd){
    $prefix = strpos(strtolower(PHP_OS),'win') > -1 ? 'where' : 'which';
    exec("{$prefix} {$cmd}", $output, $returnVal);
    $returnVal !== 0
}

this one is crossplatform solution using the return value of "where" and "which" :)

这个是使用“where”和“which”的返回值来实现的crossplatform解决方案。

#6


-3  

No, there are not.

不,没有。

Even when having direct access to a shell, you do not know if a command exists. There a some tricks like wheris or find / -name yourcommand but that not a 100% guarantee that you can execute the command.

即使直接访问shell,也不知道是否存在命令。有一些技巧,比如where is或find / -name命令,但是不能100%保证您可以执行命令。