6

I want to get predefined user input.

echo 'Enter value: ';
# here write some 'default value'
$value = fgets(STDIN);

So then user get prompt, he can edit input.

Enter value: default value # Here we can backslash and write new one.

How to achieve?

Vijay Verma
  • 3,660
  • 2
  • 19
  • 27
Astery
  • 1,216
  • 13
  • 22
  • @RoyalBg, php-cli tag does not set for some reason. – Astery Nov 26 '13 at 10:22
  • You can use the $argv array for parameters. Also, check prompting for input here http://stackoverflow.com/questions/6543841/php-cli-getting-input-from-user-and-then-dumping-into-variable-possible . Yeah, I saw it later, it was not tagged, but only in the title :) – Royal Bg Nov 26 '13 at 10:22
  • It should be interactive. – Astery Nov 26 '13 at 10:22
  • as stated in the marked answer, you can use `if ($value != 'predefined_answer') { // doSmth()` or if it's null, assign value, different than fgets() – Royal Bg Nov 26 '13 at 10:25
  • May be it can be achieved with expect://? – Astery Nov 26 '13 at 10:31

1 Answers1

0

That is not the default/commen way of handling this stuff in shell scripts. In almost any programm you'll find something like this:

Enter your value: [Default Value]

$default = "Default Value";
echo "Enter your value: [", $default, "]";
$result = fgets(STDIN);
if (empty($result)) $result = $default;

Where as the default value gets set when the STDIN of that get was empty.

I'm pretty surr that the thing you want to achive isn't possible at all.

DAG
  • 6,710
  • 4
  • 39
  • 63