5

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.

1
  • I'm afraid this won't work... require searches for a exact name of the script, you can't pass variables to it since it's not a URL, it's a path. The only ways I know to pass variables between scripts are session_start() and header() but I'm afraid these won't work without a web server. Commented Nov 27, 2012 at 16:51

4 Answers 4

2

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
Sign up to request clarification or add additional context in comments.

1 Comment

Should be $argv[1] not argv[1]
2

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:

<?php
echo $id;

Comments

1

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`,

and then in php you get that varibale with $argv[0], $argv[1] .... etc.

That is the way from bash scripts.

Comments

0

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.

More info here and here

Hope this helps!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.