The issue with your solution is that the output of the Shell Script is actually IN the PHP $response variable:
SHELL script:
#!/bin/bash
echo "Before prompt"
read -p 'Enter a value: ' input
echo "You entered $input"
PHP script:
<?php
$shell = shell_exec("./t.sh");
echo "SHELL RESPONSE\n$shell\n";
Result of php t.php:
$ php t.php
Enter a value: foo
SHELL RESPONSE
Before prompt
You entered foo
You captured the entire STDOUT of the Shell Script.
If you are looking to simply pass values to a shell script, the option of $option_string = implode(' ', $array_of_values); will work to place options individually for the script. If you would like something a little more advanced (setting flags, assigning things, etc) try this (https://ideone.com/oetqaY):
function build_shell_args(Array $options = array(), $equals="=") {
static $ok_chars = '/^[-0-9a-z_:\/\.]+$/i';
$args = array();
foreach ($options as $key => $val) if (!is_null($val) && $val !== FALSE) {
$arg = '';
$key_len = 0;
if(is_string($key) && ($key_len = strlen($key)) > 0) {
if(!preg_match($ok_chars, $key))
$key = escapeshellarg($key);
$arg .= '-'.(($key_len > 1) ? '-' : '').$key;
}
if($val !== TRUE) {
if((string) $val !== (string) (int) $val) {
$val = print_r($val, TRUE);
if(!preg_match($ok_chars, $val))
$val = escapeshellarg($val);
}
if($key_len != 0)
$arg .= $equals;
$arg .= $val;
}
if(!empty($arg))
$args[] = $arg;
}
return implode(' ', $args);
}
That will be about your most comprehensive solution for passing to the command line.
If you are instead looking for a way to prompt the user (in general), I would consider staying inside PHP. The most basic way is:
print_r("$question : ");
$fp = fopen('php://stdin', 'r');
$response = fgets($fp, 1024);
Or, to support validating the question, multi-line, and only calling on CLI:
function prompt($message = NULL, $validator = NULL, $terminator = NULL, $include_terminating_line = FALSE) {
if(PHP_SAPI != 'cli') {
throw new \Exception('Can not Prompt. Not Interactive.');
}
$return = '';
// Defaults to 0 or more of any character.
$validator = !is_null($validator) ? $validator : '/^.*$/';
// Defaults to a lonely new-line character.
$terminator = !is_null($terminator) ? $terminator : "/^\\n$/";
if(@preg_match($validator, NULL) === FALSE) {
throw new Exception("Prompt Validator Regex INVALID. - '$validator'");
}
if(@preg_match($terminator, NULL) === FALSE) {
throw new Exception("Prompt Terminator Regex INVALID. - '$terminator'");
}
$fp = fopen('php://stdin', 'r');
$message = print_r($message,true);
while (TRUE) {
print_r("$message : ");
while (TRUE) {
$line = fgets($fp, 1024); // read the special file to get the user input from keyboard
$terminate = preg_match($terminator, $line);
$valid = preg_match($validator, $line);
if (!empty($valid) && (empty($terminate) || $include_terminating_line)) {
$return .= $line;
}
if (!empty($terminate)) {
break 2;
}
if(empty($valid)) {
print_r("\nInput Invalid!\n");
break;
}
}
}
return $return;
}
$stringto be passed as its actual value? It currently is passed as string $string value. Use double quotes inshell_exec("./response.sh $string");so PHP can parse it. As for Bash I have no clue so I won't put it as an answer.sh: 1: Syntax error: word unexpected (expecting ")")