I am looking for a way to have two stdins to a bash script, namely one that is interactive and another which could leverage redirection
Say I need to invoke a program which asks for credentials of the type username, password I want to feed that interactively and then I want to feed a while loop from a redirection. Something like:
$> ./myscript.sh < input.lst
or
$> cat input.lst | ./myscript.sh
In my script there would be something like (very simplified):
#!/bin/bash
./authentication_application
while read i
do
echo $i
done
Where authentication_application is a binary I don't own (have no access to source code and haven't compiled it myself), I can't change and can't be replaced with any alternative authentication method.
And the invocation would result in
Username: some_user
Password: password
... contents of input.lst ...
With some_user and password input interactively and contents of input.lst taken from the redirection
I tried redirecting stdin to a new fd and using read -u <fd> inside the script with fd being "7" for instance, and something like this:
#!/bin/bash
exec <fd><&0
exec 0<&-
... but here is where I have no idea of how to re-open (or reset) stdin to the interactive one
Any Ideas? you people even think it's possible?
BTW, I'm not looking for the following:
- prepending
input.lstwith username and password on cleartext (nor any kind of cypher hack) - cat-ing two files into the script (the first one storing username and password on cleartext)
- A workaround where I have to use parameters to
get_credentials.shto pass username and password without prompting the user - passing the file input.lst as a parameter to the script and using positional parameters to access the file in the while loop (it would defeat the purpose of redirecting the input and leveraging the pipelining nature of the script i.e.
$> cat input.lst | ./filter_1.sh | ./filter_2.sh |... | ./filter_n.sh)