Unable to run shell script file using PHP

谁说我不能喝 提交于 2019-12-25 18:19:30

问题


Hi I am unable to run the .sh file using my php code.

 Files:  index.php and .sh files are in the same directory.

What I have tried:

 echo shell_exec('sh shell_file.sh'); //Did not execute
 echo shell_exec('shell_file.sh'); //Did not execute
 echo exec('shell_file.sh'); //Did not execute

But when I run the shell_file.sh file manually it does execute.


回答1:


Try like this:

What you need to do is call the file with a program. Call it with bash or sh as suggested in the comment:

echo shell_exec('sh /shell_file.sh');

Another option could be:

$contents = file_get_contents('/shell_file.sh');
echo shell_exec($contents);

I think the first option would be better however.



来源:https://stackoverflow.com/questions/30507840/unable-to-run-shell-script-file-using-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!