简体   繁体   English

我如何从我的PHP脚本传递变量并将其发送到我的bash脚本

[英]how can i pass a variable from my php script and send it to my bash script

okay so heres my bash command i want to send a variable to 好吧,这是我的bash命令,我想将变量发送给

perl -pi -e 's/ : /:/g' /opt/lampp/htdocs/"variable needs to go here"

i then have my variable in php wich is 然后我在php wich中有我的变量是

$filename

how do i edit my bash script to accept the variable and how do i pass the variable and execute the bash from php any help would be appreciated. 我如何编辑我的bash脚本以接受变量,以及如何传递变量并从php执行bash,将不胜感激。 i just cannot figure out how i would make the bash ready to accept a variable and how to make php send it and execute th script 我只是不知道如何使bash准备好接受变量,以及如何使php发送它并执行脚本

In php use the exec() function to invoke the bash script like this: 在php中,使用exec()函数来调用bash脚本,如下所示:

$filename = escapeshellarg($filename);
exec("perl -pi -e 's/ : /:/g' /opt/lampp/htdocs/".$filename);

Try using shell_exec in your php script to execute your shell script and pass your variable, like so: 尝试在PHP脚本中使用shell_exec来执行Shell脚本并传递变量,如下所示:

$cmd="perl -pi -e 's/ : /:/g' /opt/lampp/htdocs/" .  escapeshellarg($variable);
$r=shell_exec($cmd);

escapeshellarg is used to escape any potentially dangerous characters in $variable, to prevent a command line injection attack. escapeshellarg用于对$ variable中的任何潜在危险字符进行转义,以防止命令行注入攻击。

There is another solution, not so elegant, but it works. 还有另一种解决方案,虽然不那么优雅,但是可以起作用。
You can echo the value from the php script, and catch it in a variable in the bash script (using backticks). 您可以从php脚本中回显该值,并将其捕获到bash脚本中的变量中(使用反引号)。
Don't forget to first disable error output in the php script, to eliminate the danger of having an output with notices, warnings... 不要忘记首先禁用php脚本中的错误输出,以消除带有通知,警告输出的危险。

Eg. 例如。

<?php
ini_set("display_errors", "Off");
ini_set("log_errors", "On");
$filename = 'someValue';
echo $filename;
?>


#!/bin/bash
#execute the php file
FILENAME=`php file.php`
perl -pi -e 's/ : /:/g' /opt/lampp/htdocs/$FILENAME

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM