Feed script variable to command that prompts for user input

Unix & Linux Asked by Vishnu Sajeevan on December 15, 2020

I have a shellscript and it will have a command to upload a file onto server which will prompt for username and password.

Instead of providing username and password there, I would like to pass the username and password as an external argument only, how it is possible?

username =$1

The pushcommand will ask to enter username and password; the shellscript should automatically take it from $1 and $2.

One Answer

If pushcommand simply reads first username and then password from standard input, separated by newline, you can do the following:

echo "$usernamen$password" | pushcommand

The "echo" call here will output the username and password variables on separate lines.
The "|" means "take the output of the command on the left, and feed it as input to the command on the right". This construct is called a "pipe" in shell scripts.

For scripting more complex interactions, I'd recommend looking at expect or possibly kermit. To get help with expect or kermit, you'd have to post an example on how an interactive use of pushcommand actually looks like, e.g. what prompts it displays and so on.

Answered by rosarote elfe on December 15, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP