0

I'm trying to run a scrip from php by passing three parameters.The problem is that I can only pass the first parameters well.

code:

$asd=$name;
for($i=0;$i<count($checkbox);$i++){
$raid=$checkbox[$i];

$asd=$asd ." ". $raid;

}
  $railvl1 = exec("sh /var/www/proyecto/borrarraid1.sh ".$asd);

$name has a string and $raid contains the checkbox array.

if I use vardump or die over $ asd it shows me the following.

string(15) "md2 sdc sdd "

my script has this:

mkdir $1
mkdir $2
mkdir $3

Only the md2 folder is created, the sdc folder is created by calling :sdd? and the last folder sdd is not created.

Any suggestions?

andoni
  • 13
  • 3
  • Quite possible your script does more, but PHP can create directories on the file system itself. – Jonnix Jan 28 '19 at 22:45
  • [Pass PHP variables to a Bash script and then launch it](https://stackoverflow.com/q/13903506/608639), [Passing Variables to shell_exec()?](https://stackoverflow.com/q/16932113/608639) and maybe [PHP shell_exec() vs exec()](https://stackoverflow.com/q/7093860/608639) – jww Jan 28 '19 at 23:10
  • 1
    There are too many moving parts here. You should simplify your question to a minimal piece of code, that we can run, that demonstrates your problem. (Right now it's not even clear whether your problem is in your PHP or in your Bash script.) By the way, the statement "the sdc folder is created by calling :sdd?" doesn't make any sense to me. What is `:sdd?`? – ruakh Jan 28 '19 at 23:14

3 Answers3

0

You could do it on a different way, check out my example! Try this

$name = '';
$checkbox = ['FolderA', 'FolderB', 'FolderC'];

$checkboxes = implode(' ', $checkbox);
$name .= " $checkboxes" ;
$output = shell_exec("/var/www/proyecto/borrarraid1.sh $name");

Sh file

set folder1 [lindex $argv 0]
set folder2 [lindex $argv 1]
set folder3 [lindex $argv 2]

mkdir folder1
mkdir folder2
mkdir folder3
Ezequiel Fernandez
  • 954
  • 11
  • 18
0

! Use the function escapeshellarg to prevent from shell injection attacks:

$asd = escapeshellarg($name);
for($i=0; $i<count($checkbox); $i++){
    $raid = $checkbox[$i];
    $asd = $asd ." ". escapeshellarg($raid);
}

$last_line = exec("sh /var/www/proyecto/borrarraid1.sh ". $asd, $output, $retval);

$output_delimiter = '<br/>'
if ($retval !== 0) {
    echo "Error while executing command", $output_delimiter;
    echo implode($output, $output_delimiter);
} else {
    echo "The command was executed successfully", $output_delimiter;
    echo implode($output, $output_delimiter);
}

PS: exec() manual page: http://php.net/manual/en/function.exec.php

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • I'm trying to do what you said, but it does not work.what is it or what does it do $output, $retval ?? – andoni Jan 29 '19 at 11:42
  • _You_ used `exec()` in your question. I just used what you had. Having that, I must assume that you know how `exec()` works and therefore didn't explain it. `$output` and `$retval` are passed by reference to give you a chance to obtain the full output of the command and it's return value. In any case, **read** the manual of `exec()` if you want to know how it works: http://php.net/manual/en/function.exec.php. – hek2mgl Jan 29 '19 at 11:51
-1

Please try

 $railvl1 = exec("sh /var/www/proyecto/borrarraid1.sh '{$asd}'");
smavroud
  • 117
  • 4
  • Hello, thanks for your help.I did as you told me and it worked but not quite. I have created the 3 folders, but they are called md2 (ok), sdd? and sdc ?. Why does the? in the end?http://prntscr.com/mdhz30 – andoni Jan 28 '19 at 23:31
  • What if `$asd` contains a quote? @andoni Use `escapeshellarg` as I showed: https://stackoverflow.com/a/54411650/171318 . This answer is just wrong and by that extremely problematic because it might allow hackers to get access to your backend. – hek2mgl Jan 29 '19 at 11:20