How can I get Parameters with Shell Request.
I mean so but not work:
PHP:
error_reporting(E_ALL);
$hallo ="Hallo Welt";
print_r(shell_exec("sh client.sh $hallo"));
Shell:
echo $hallo
echo ceck
How can I get Parameters with Shell Request.
I mean so but not work:
PHP:
error_reporting(E_ALL);
$hallo ="Hallo Welt";
print_r(shell_exec("sh client.sh $hallo"));
Shell:
echo $hallo
echo ceck
Why doesn't it work? The PHP is OK. It runs client.sh with a couple parameters. If the problem is with client.sh then you'll need to paste that rather than the PHP code.
I have post the Shell script:
Its only:
echo $hallo
echo ceck
It display only ceck not "Hallo Welt"
Did i frorgot something?
I mean echo and the Variable $hallo display that.
Is the client.sh really a 'sh' or is it 'bash'?
Check the shebang of the file:
head -n1 client.sh
hth
You should have received an empty line and then "ceck". Your $hallo
variable will be undefined in the subshell as you did not export it.
---------- Post updated at 14:28 ---------- Previous update was at 14:26 ----------
If you called the script with $hallo
as the first positional parameter, you'd need to echo $1
instead of $hallo.
You parent process call subprocess
sh client.sh $hallo
On the shell subprocess you can handle arguments using variable 1, 2, 3 ...
So your subprocess script should be something like:
echo "arg1: $1"
echo "arg2: $2"
echo "argall: $*"
echo "argcnt: $#"
echo "myname: $0"
But command line is parsed using IFS = white space, so if your variable hallo include whitespaces, shell will split your value. If you like to keep whitespaces in your value, then you need to tell it: round the string using ' characters, you'll tell that "this is the string".
So your php include it:
<?php
$hallo ="Hallo Welt";
print_r(shell_exec("sh client.sh '$hallo' "));
?>
It's also little risk to use sh. I'll tell exactly which shell I like to use: bash, ksh, dash, ... Usually sh is linked to the one of those, but on the older *nix it'll be Bourne Shell.
Example "I like to use bash to run my script":
<?php
$hallo ="Hallo Welt";
print_r(shell_exec("bash client.sh '$hallo'"));
?>
Or you can setup in your script (=correct method) which shell it need/has tested to use
#!/bin/ksh
echo hello world
And you setup execution privilege for script:
chmod a+rx client.sh
Then your php call "program", not script:
<?php
$hallo ="Hallo Welt";
print_r(shell_exec("client.sh '$hallo'"));
?>
And shell script will use that interpreter which you have set in the script.