How to provide password to a command that prompts

2019-01-04 00:37发布

I'm writing a UNIX shell function that is going to execute a command that will prompt the user for a password. I want to hard-code the password into the script and provide it to the command. I've tried piping the password into the command like this:

function() {
    echo "password" | command
}

This may not work for some commands as the command may flush the input buffer before prompting for the password.

I've also tried redirecting standard input to a file containing the password like this, but that doesn't work either:

function() {
    echo "password" > pass.tmp
    command < pass.tmp
    rm pass.tmp
}

I know that some commands allow for the password to be provided as an argument, but I'd rather go through standard input.

I'm looking for a quick and dirty way of piping a password into a command in bash.

7条回答
beautiful°
2楼-- · 2019-01-04 01:31
登录 后发表回答