Reputation: 53
Is it possible to pass parameters from php to a shell script. For ex:
<?php
$var = "testfolder";
shell_exec('sh /path/test.sh');
?>
Shell script (test.sh):
sudo mkdir $var /path/
I want the value in php $var
to be exported to the shell script to be able to create a folder with the name as in the variable.
Is this possible?
I know already how to insert a variable into a string, but i can't figure out how to rewrite the line shell_exec
to export this variable to my shell script
Thanks!
Upvotes: 0
Views: 272
Reputation: 4010
Pass the variable as a command line parameter to the shell script. For example:
<?php
$var = "testfolder";
echo 'php: ' . shell_exec("sh ./test.sh $var");
#!/bin/sh
echo "shell: $1"
$ php test.php
php: shell: testfolder
(For clarity, I'm simply echoing here.)
Shells transform command line parameters sequentially into variables $1
, $2
, etc.
Upvotes: 1
Reputation: 21
Yes.You need to pass the variables as arguments to the shell script, and the shell script has to read its arguments.
please check this link: passing a variable from php to bash
Upvotes: 2