Executing .sh scripts via PHP

怎甘沉沦 提交于 2019-12-05 17:09:38

You could just use the shell_exec() function in PHP:

http://php.net/manual/en/function.shell-exec.php

shell_exec('sh script.sh');

And if you want to use the variables ($1, $2 etc. in bash) you could just type:

shell_exec('sh script.sh variable1 variable2');

You can use either an absolute path or a relative path. For relative paths, the current directory is usually the PHP files's. If you can't access your scripts even with an absolute path, there could be a problem with filesystem access rights. Keep in mind PHP scripts are typically executed as the web server's (system) user account.

Your shell scripts should be stored somewhere near or inside your application tree, so that you can grant acces to them to your PHP scripts (i.e. to your web server), without compromising too much your file system security.

if you want to check the output of the script on browser than you can simply use

var_dump(shell_exec('sh script.sh'));

or

var_dump(shell_exec('sh script.sh variable1 variable2'));

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