0

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

1
  • Are you using bash, or perl? Commented Jul 25, 2013 at 20:21

3 Answers 3

1

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

Comments

0

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.

1 Comment

i already have the perl in a bash script a basically need to pass the variable to /opt/lampp/htdocs/bash.sh and then execute it how would i do that
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.