Adi Helio
Adi Helio

Reputation: 25

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

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

i then have my variable in php wich is

$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. 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

Upvotes: 0

Views: 372

Answers (3)

Ion Dulgheru
Ion Dulgheru

Reputation: 129

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).
Don't forget to first disable error output in the php script, to eliminate the danger of having an output with notices, warnings...

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

Upvotes: 0

Bad Wolf
Bad Wolf

Reputation: 8349

In php use the exec() function to invoke the bash script like this:

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

Upvotes: 1

mti2935
mti2935

Reputation: 12037

Try using shell_exec in your php script to execute your shell script and pass your variable, like so:

$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.

Upvotes: 0

Related Questions