Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing GET Variable from one Bash/PHP Script to another

Tags:

linux

bash

php

get

I am not sure the proper name for it, but I am executing PHP code within a Bash script on my Linux server. I have two of these Bash files and want to be able to pass a GET variable from one file to the next.

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 :

PHP Warning: require(bash2.sh?id=1): failed to open stream: No such file or directory in /home/bash/bash1.sh on line 2

If I remove the ?id=1 from the require(), it executes without an error.

like image 961
Chris Avatar asked Nov 27 '12 16:11

Chris


1 Answers

You r thinking web... What u put in the require is the actual file name the PHP engine will look for using the OS. i.e. it looks for a file called bash2.sh?id=1 which u obviously do not have.

Either u call another script from withing, say with system('./bash2.sh 2'); Or, include, and use the method below to pass data.

file1

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

file2

<?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:

<?php
echo $argv[1]; //argv[0] is the script name
like image 167
Itay Moav -Malimovka Avatar answered Sep 20 '22 13:09

Itay Moav -Malimovka