如何在PHP中使用shell_exec运行带参数的.sh文件? [重复]

This question already has an answer here:

attempting to run a shell script using PHP. the shell script contains: 'cd /data/nfsshare; python /data/nfsshare/kron_hull.py> /data/nfsshare/log.txt' that is running a python script named kron_hull.py

the php code im using is:

if ($_POST['visual']) {
  shell_exec("sh test.sh");
  echo "working! woot woot";
} else {
  echo "not working";
}

i am using the echo's as a way to tell if its running the if statement correctly, and i am able to successfully echo "working! woot woot", but the shell file does not run, because the script is meant to create a new file on the server, which it does not.

the shell file works when you put it directly in the linux terminal as "sh test.sh" so the issue is not with the shell file.

i also ran shell_exec('whoami'); and shell_exec('ls -lart'); and was able to echo both of those, so i don't believe it is a problem with the shell_exec() command.

my problem is: i dont believe the shell script is even running when put into the shell_exec() command, and I dont know a better way to check besides looking for the new file its supposed to create, which i get none.

any help is appreciated, if you need me to do anything for testing, or need more information, lmk and ill be glad to answer.

i checked the related questions and they did not help my case

</div>

1个回答



检查shell命令是否正确执行的一种方法是使用 exec()</ code>函数。 exec()</ code>使您能够查看变量中的返回值和输出。 </ p>

您可以构建一个错误检查执行,如下所示:</ p>

  if($ _POST ['visual']){
exec( “sh test.sh”,$ output,$ return);

//返回将在出错时返回非零
if(!$ return){
echo“working!woot woot”;
} else {
echo“shell脚本出错”;
}
}
</ code> </ pre>

要查看错误代码,您可以查看此处。</ p>
</ div>

展开原文

原文

One way to check whether the shell command executed correctly is to use the exec() function instead. exec() gives you the ability to see the return value and the output in a variable.

You can build a error checking execution like this:

if ($_POST['visual']) {
    exec("sh test.sh", $output, $return);

    // Return will return non-zero upon an error
    if (!$return) {
        echo "working! woot woot";
    } else {
        echo "Error with shell script";
    }
}

To see error codes you can look here.

doudang4568
doudang4568 那很棒! 你有望找到问题所在。 我不确定255意味着什么。 您是否尝试在命令提示符下单独运行shell脚本?
大约一年之前 回复
dongrong9938
dongrong9938 所以现在我正在使用这段代码我得到了echo语句:“shell脚本出错”,那么我从哪里开始呢? 编辑:我添加了“echo $ return;” 得到255这是“255扩展属性是不一致的。”任何想法?
大约一年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问