douleng3463 2014-05-22 12:22
浏览 29
已采纳

Shell脚本 - PHP - 显示错误

I'm using PHP function shell_exec()to execute Shell Script. When I use this(correct) syntax, it displays output normally

echo shell_exec("ls -l");

But, for some reason, if user enters invalid command like this

echo shell_exec("lsl -l");

by default, it should give error as it gives on terminal. But, it doesn't display anything. is there any way to catch and display errors as well; via PHP?

  • 写回答

2条回答 默认 最新

  • dongzhuoxie1244 2014-05-22 12:26
    关注

    That's because the output of the error goes to STDERR, while shell_exec only reads STDOUT. The simplest solution would probably be to pipe STDERR to STDOUT:

    <?php
    echo shell_exec('lsl -l 2>&1');
    

    If you only want to know if the command was successful, I would use exec rather than shell_exec, as you can get the return value.

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

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器