php 如何从php运行.sh文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7397672/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to run a .sh file from php?
提问by NidhinRaj
I am trying to run a shell script using php
我正在尝试使用 php 运行 shell 脚本
shell script ( /home/scripts/fix-perm.sh ) is in the same server
shell 脚本( /home/scripts/fix-perm.sh )在同一台服务器上
this is the code that i am trying
这是我正在尝试的代码
<?php
echo shell_exec('/home/scripts/fix-perm.sh');
?>
the above code is not working
上面的代码不起作用
am using linux server
我正在使用 linux 服务器
can anybody please help me?
有人可以帮我吗?
回答by hoppa
Shell exec takes a string which needs to be an actual command. You are now passing it a filepath. This is not interpreted as "execute the file at this path". You could do several things.
Shell exec 需要一个字符串,它需要是一个实际的命令。您现在正在向它传递文件路径。这不会被解释为“在此路径上执行文件”。你可以做几件事。
What you need to do is call the file with a program. Call it with bash or sh as suggested in the comment:
您需要做的是使用程序调用该文件。按照评论中的建议使用 bash 或 sh 调用它:
echo shell_exec('sh /home/scripts/fix-perm.sh');
Another option could be:
另一种选择可能是:
$contents = file_get_contents('/home/scripts/fix-perm.sh');
echo shell_exec($contents);
I think the first option would be better however.
不过,我认为第一种选择会更好。
It is important to note that all commands for executing external programs expect actual commands and not a filepath or something else. This goes for shell_exec, exec, passthruand others.
需要注意的是,所有用于执行外部程序的命令都需要实际命令,而不是文件路径或其他东西。这适用于shell_exec、exec、passthru等。