I'm trying to execute a simple Bash Script
from PHP
script. I collect data from a HTML5
front end page, pass through ajax
to the PHP script, take the variables and then, pass these to the .sh
script, but I've got messages like:
我正在尝试从PHP脚本执行一个简单的Bash脚本。我从HTML5前端页面收集数据,通过ajax传递给PHP脚本,获取变量,然后将这些传递给.sh脚本,但我有以下消息:
./test_bash.sh: line 13: ./test.txt: Permission denied
I tried to change the permissions chmod 777 test_bash.sh
, tried to modify the sudoers.d
file, tried this: shell_exec("echo password_for_the_user | sudo -S command_to_execute");
... but the Bash script
can't write the test.txt
file.
我试图更改权限chmod 777 test_bash.sh,试图修改sudoers.d文件,试过这个:shell_exec(“echo password_for_the_user | sudo -S command_to_execute”); ...但是Bash脚本无法编写test.txt文件。
Here is my basic code, first the PHP code
:
这是我的基本代码,首先是PHP代码:
<?php
$var1 = json_decode($_POST['var1']); //from front-end html5
$var2 = json_decode($_POST['var2']);
$var3 = json_decode($_POST['var3']);
$response = shell_exec("./test_bash.sh $var1 $var2 $var3 2>&1");
echo "$response";
?>
Secondly, the Bash
code:
其次,Bash代码:
#!/bin/bash
var1=$1;
var2=$2;
var3=$3;
echo "$var1";
echo "$var2";
echo "$var3";
echo $var1 $var2 $var3 > ./test.txt
2 个解决方案
#1
2
I believe you have to change the permissions on the txt file also in order for apache ( the user that is actually executing the script ) to be able to write to it.
我相信您还必须更改txt文件的权限,以便apache(实际执行脚本的用户)能够写入它。
Be careful though when using shell_exec()
and changing permissions it is quite easy to pass unwanted variables...
但是在使用shell_exec()并更改权限时要小心,很容易传递不需要的变量......
#2
1
When you are saying
当你说的时候
echo $var1 $var2 $var3 > ./test.txt
You are echo
ing var1, var2 and var3 into the file test.txt
that lies in the same directory as the script that is running it.
您将var1,var2和var3回显到文件test.txt中,该文件与运行它的脚本位于同一目录中。
So if you are in /var/www
, doing echo $var1 $var2 $var3 > ./test.txt
will be the same as saying echo $var1 $var2 $var3 > /var/www/test.txt
.
因此,如果您在/ var / www中,则执行echo $ var1 $ var2 $ var3> ./test.txt将与echo $ var1 $ var2 $ var3> /var/www/test.txt相同。
The problem you are facing consists in this error:
您遇到的问题包括以下错误:
./test_bash.sh: line 13: ./test.txt: Permission denied
./test_bash.sh:line 13:./ test.txt:权限被拒绝
This is telling you that you are not allowed to write into the file /var/www/test.txt
. To be able to do so, change the write permissions to this file so that "others" (that is, user www
or apache
) can write into it:
这告诉您不允许您写入文件/var/www/test.txt。为了能够这样做,请将写入权限更改为此文件,以便“其他人”(即用户www或apache)可以写入其中:
chmod o+w /var/www/test.txt
Or, probably better, write into another directory. For example /tmp
.
或者,可能更好,写入另一个目录。例如/ tmp。
Finally, note that it is recommendable to quote your vars. So better say:
最后,请注意引用你的vars是值得推荐的。所以说得好:
echo "$var1 $var2 $var3" > test.txt
# ^ ^
#1
2
I believe you have to change the permissions on the txt file also in order for apache ( the user that is actually executing the script ) to be able to write to it.
我相信您还必须更改txt文件的权限,以便apache(实际执行脚本的用户)能够写入它。
Be careful though when using shell_exec()
and changing permissions it is quite easy to pass unwanted variables...
但是在使用shell_exec()并更改权限时要小心,很容易传递不需要的变量......
#2
1
When you are saying
当你说的时候
echo $var1 $var2 $var3 > ./test.txt
You are echo
ing var1, var2 and var3 into the file test.txt
that lies in the same directory as the script that is running it.
您将var1,var2和var3回显到文件test.txt中,该文件与运行它的脚本位于同一目录中。
So if you are in /var/www
, doing echo $var1 $var2 $var3 > ./test.txt
will be the same as saying echo $var1 $var2 $var3 > /var/www/test.txt
.
因此,如果您在/ var / www中,则执行echo $ var1 $ var2 $ var3> ./test.txt将与echo $ var1 $ var2 $ var3> /var/www/test.txt相同。
The problem you are facing consists in this error:
您遇到的问题包括以下错误:
./test_bash.sh: line 13: ./test.txt: Permission denied
./test_bash.sh:line 13:./ test.txt:权限被拒绝
This is telling you that you are not allowed to write into the file /var/www/test.txt
. To be able to do so, change the write permissions to this file so that "others" (that is, user www
or apache
) can write into it:
这告诉您不允许您写入文件/var/www/test.txt。为了能够这样做,请将写入权限更改为此文件,以便“其他人”(即用户www或apache)可以写入其中:
chmod o+w /var/www/test.txt
Or, probably better, write into another directory. For example /tmp
.
或者,可能更好,写入另一个目录。例如/ tmp。
Finally, note that it is recommendable to quote your vars. So better say:
最后,请注意引用你的vars是值得推荐的。所以说得好:
echo "$var1 $var2 $var3" > test.txt
# ^ ^