如何从PHP检查shell命令是否存在 [英] How to check if a shell command exists from PHP

查看:211
本文介绍了如何从PHP检查shell命令是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need something like this in 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');
}

有什么解决办法吗?

推荐答案

在Linux/Mac OS上,请尝试以下操作:

On Linux/Mac OS Try this:

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

然后在代码中使用它:

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

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

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

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

这篇关于如何从PHP检查shell命令是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆