dopuzf0898 2016-05-11 16:48
浏览 68
已采纳

从perl / php脚本执行终端命令是一个好习惯吗? [重复]

This question already has an answer here:

Like executing a command using backticks or exec() or system().

</div>
  • 写回答

4条回答 默认 最新

  • douli4852 2016-05-11 17:55
    关注

    Yes, this is bad practice in almost all cases! This is especially true if you have a choice.

    Executing external commands is:

    • Very hard to do correctly (but very easy to get sort-of-working). You can't just escape the shell args and call it a day, you have to account for potentially multiple levels of escaping for potentially different languages.

    • Slow. A fork+exec to e.g. rm is easily a thousand times slower than the corresponding syscall.

    • A rigid, error-prone and inexpressive integration point. You typically have to convert data to flat lists of strings and back. You can't use the language's features like exception handling, nested data structures or callbacks.

    Due to this, the following are BAD reasons to call external commands:

    • Not knowing how to do X in your language, but knowing a shell command for it. A typical example is cp -R foo bar.

    • Not knowing how something works, but knowing a shell oneliner that does it. A typical example is foo *.mp4 > >(tee file).

    • Not wanting to learn a new API for e.g. json or http, and instead using shell tools like jq or curl.

    However, if you are calling a program that does non-trivial things, that doesn't have a native library or bindings, AND that you know how to invoke with execve semantics (NOT system nor perl exec semantics that invoke shells), this is a valuable tool.

    Examples of good uses of executing external commands that follow all the above is invoking make to build a project from an installer, or running java -jar ... to start a Minecraft server.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)