user4293437
user4293437

Reputation:

Cant pass a variable from PHP to bash

Im trying to pass a variable from php to bash, but im having problems.

Here is a snippet of the PHP code.

<?php
$shopidX = escapeshellarg('$shop_id');
exec("~/bin/notaproblem $shopidX");
<?

Here is where the variable is defined in bash.

shopidX=$1

Now the variable does not work. I have tried hardcoding the variable in the bash script (e.x)

shopidX=89234796743682446811473645238461264123465243614537285417254237645712345768235472364536217481238431654187

And it does work. The $shop_id is also 100% defined in PHP. What's the problem?

Upvotes: 1

Views: 90

Answers (2)

Shahroze Ali
Shahroze Ali

Reputation: 57

HTML code:

<html>
<form>
<input name="name">
</form>
</html>

PHP code:

<?php 
$name = $_POST['name'];
$output = shell_exec("C:/xampp/folder/scriptname.sh $name");
?>

Shell script:

#!/bin/bash -e
name=$1
echo name=${name:-$1}

Upvotes: 0

Marc B
Marc B

Reputation: 360762

Wrong quotes:

$shopidX = escapeshellarg('$shop_id');
                          ^--------^

'-quotes do NOT interpolate variables:

$foo = 'bar;
echo '$foo'; // outputs $, f, o, o
echo "$foo"; // outputs b, a, r

You're sending $, s, h, etc.. to the shell, not the contents of the variable.

In fact, you don't need quotes AT ALL

$shopidX = escapeshellarg($shop_id);

Upvotes: 1

Related Questions