【问题标题】:Is there a way to input automatically when running a script?有没有办法在运行脚本时自动输入?
【发布时间】:2016-07-18 06:33:27
【问题描述】:

我正在尝试执行一个要求输入密码的脚本文件。有什么方法可以自动为命令提供它需要的输入? 它在 psql 中,其中有参数-U 作为用户名,但没有参数作为密码。

例子:

exa00009@exa00009:~$ sh redshift.sh
Password for user xyz:

【问题讨论】:

标签: shell ubuntu psql


【解决方案1】:

您可以为此使用expect。 这是一个简单的例子:

#!/usr/bin/expect -f
set PASSWORD "1"
set USER "dvp"
spawn psql -U ${USER} -W
expect "*?assword*"
send -- "${PASSWORD}\r"
expect eof

可能你需要先安装expect

sudo apt-get install expect

【讨论】:

    【解决方案2】:

    您可以为您的脚本提供命令行参数,如下所示: sh redshift.sh yourInput 并在您的脚本中将其命名为 var input=$1 并相应地使用。

    这个问题已经回答here

    【讨论】:

    • 你能详细说明一下吗,我的 .sh 文件有 psql 命令,除了用户名参数但没有密码。我需要自动化它,以便我只需要运行脚本
    猜你喜欢
    • 1970-01-01
    • 2021-12-06
    • 2016-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-30
    • 1970-01-01
    相关资源
    最近更新 更多