TL;DR -- echo -n abc | bash -c 'read -s x && echo -n \$x' doesn't output abc.
I have a PHP CLI script which needs to read a password from stdin.
<?php
function quiet_read_stdin ()
{
return shell_exec ("bash -c 'read -s x && echo -n \$x'");
}
print "got: ###".quiet_read_stdin ()."###\n";
?>
If I run this in a terminal and input, say, 'xyz':
$> php /tmp/foo.php
got: ###xyz###
As expected, also this works
$> echo xyz | php /tmp/foo.php
got: ###xyz###
But this doesn't.
$> echo -n xyz | php /tmp/foo.php
got: ######
This is causing problems in my script in the case that the secret key is stored in a file without a newline, equivalent to php /tmp/foo.php < keyfile.
This doesn't work (Inappropriate ioctl for device):
system ('stty -echo');
$password = trim (fgets (STDIN));
system ('stty echo');
I don't want to fudge it by inserting a newline at any stage. How can I modify quiet_read_stdin so that it works whether or not the input has a trailing newline? A Linux-only solution is fine.