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 对于这个问题的代码运行
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败