How to check if a shell command exists from PHP

后端 未结 7 1000
孤街浪徒
孤街浪徒 2020-12-25 11:07

I need something like this in php:

If (!command_exists(\'makemiracle\')) {
  print \'no miracles\';
  return FALSE;
}
else {
  // safely call the command kno         


        
相关标签:
7条回答
  • 2020-12-25 11:22

    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.

    0 讨论(0)
  • 2020-12-25 11:26

    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;
    }
    
    0 讨论(0)
  • 2020-12-25 11:30

    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.

    0 讨论(0)
  • 2020-12-25 11:41

    On Linux/Mac OS Try this:

    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:

    if (`which makemiracle`) {
        shell_exec('makemiracle');
    }
    
    0 讨论(0)
  • 2020-12-25 11:41

    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.

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

    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;
    }
    
    0 讨论(0)
  • 2020-12-25 11:42

    Based on @jcubic and that 'which' should be avoided, this is the cross platform I came up with:

    function verifyCommand($command) :bool {
      $windows = strpos(PHP_OS, 'WIN') === 0;
      $test = $windows ? 'where' : 'command -v';
      return is_executable(trim(shell_exec("$test $command")));
    }
    
    0 讨论(0)
提交回复
热议问题