简体   繁体   English

将GET变量从一个Bash / PHP脚本传递到另一个

[英]Passing GET Variable from one Bash/PHP Script to another

I am not sure the proper name for it, but I am executing PHP code within a Bash script on my Linux server. 我不确定它的正确名称,但是我正在Linux服务器上的Bash脚本中执行PHP代码。 I have two of these Bash files and want to be able to pass a GET variable from one file to the next. 我有两个这样的Bash文件,希望能够将GET变量从一个文件传递到下一个文件。

Here is a simplified version of the 1st file: 这是第一个文件的简化版本:

#!/usr/bin/php -q
<?php

require("bash2.sh?id=1");

Here is a simplified version of the 2nd file: 这是第二个文件的简化版本:

#!/usr/bin/php -q
<?php

echo $_GET['id'];

Currently, when I execute the 1st file on a Crontab, I get an error that says : 当前,当我在Crontab上执行第一个文件时,出现错误消息:

PHP Warning: require(bash2.sh?id=1): failed to open stream: No such file or directory in /home/bash/bash1.sh on line 2 PHP警告:require(bash2.sh?id=1):无法打开流:第2行的/home/bash/bash1.sh中没有此类文件或目录

If I remove the ?id=1 from the require() , it executes without an error. 如果我从require()删除?id=1 ,它会执行而不会出错。

You r thinking web... What u put in the require is the actual file name the PHP engine will look for using the OS. 您正在考虑网络...您require是PHP引擎将使用OS查找的实际文件名。 ie it looks for a file called bash2.sh?id=1 which u obviously do not have. 即它会查找一个名为bash2.sh?id=1的文件,而您显然没有。

Either u call another script from withing, say with system('./bash2.sh 2'); 您要么从system('./bash2.sh 2');调用另一个脚本,例如使用system('./bash2.sh 2'); Or, include, and use the method below to pass data. 或者,包括并使用以下方法传递数据。

file1 文件1

<?php
$id = 1;
require("bash2.sh");

file2 文件2

<?php
echo $id;

If u use the first example ( system('./bash2.sh 2'); ) Then in bash2.sh you will access the variable in the following way: 如果u使用第一个示例( system('./bash2.sh 2'); ),则在bash2.sh中,您将通过以下方式访问变量:

<?php
echo $argv[1]; //argv[0] is the script name

You cannot add a parameter to a static file on your harddrive. 您不能将参数添加到硬盘上的静态文件中。 But you can define a global variable which is accessable by the reqired script. 但是您可以定义一个全局变量,所需的脚本可以访问该变量。

<?php
$id=1
require("bash2.php");

and for your bash2.php: 而对于您的bash2.php:

<?php
echo $id;

No dude you should use arguments. 老兄,您不应该使用参数。 When you execute php script (I am guessing in cron job), you add arguments like some.php variable1 variable2 ..... etc`, 当您执行php脚本时(我猜是在cron工作中),您添加了诸如some.php variable1 variable2 ..... etc等参数,

and then in php you get that varibale with $argv[0], $argv[1] .... etc . 然后在php中,用$argv[0], $argv[1] .... etc得到该变量。

That is the way from bash scripts. 这就是bash脚本的方式。

Try something like this: 尝试这样的事情:

<?php

    $id = $_GET['id'];
    exec("./bash2.sh $id");

?>

And then in the bash script you'll be able to access the first parameter passed as $1. 然后,在bash脚本中,您将能够访问作为$ 1传递的第一个参数。

More info here and here 在这里这里 更多信息

Hope this helps! 希望这可以帮助!

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

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