如何编写一个 bash 脚本来给出另一个程序响应

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4731566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 23:18:02  来源:igfitidea点击:

How to write a bash script to give another program response

linuxbash

提问by Nick Heiner

I have a bash script that does several tasks, including python manage.py syncdbon a fresh database. This command asks for input, like the login info for the admin. Currently, I just type this into the command line every time. Is there a way I can automatically provide these replies as part of the bash script?

我有一个 bash 脚本,可以执行多项任务,包括python manage.py syncdb在新数据库上。此命令要求输入,例如管理员的登录信息。目前,我每次都只在命令行中输入它。有没有办法可以自动提供这些回复作为 bash 脚本的一部分?

Thanks, I don't really know anything about bash.

谢谢,我对 bash 一无所知。

I'm using Ubuntu 10.10.

我正在使用 Ubuntu 10.10。

采纳答案by Zac Thompson

I answered a similar questionon SF, but this one is more general, and it's good to have on SO.

我在 SF 上回答了一个类似的问题,但这个问题更笼统,在 SO 上有一个很好的答案。

"You want to use expectfor this. It's probably already on your machine [try which expect]. It's the standard tool for any kind of interactive command-line automation. It's a Tcl library, so you'll get some Tcl skills along the way for free. Beware; it's addictive."

“您想为此使用expect。它可能已经在您的机器上 [尝试which expect]。它是任何类型的交互式命令行自动化的标准工具。它是一个 Tcl 库,因此您将在此过程中获得一些 Tcl 技能免费的。小心;它会让人上瘾。”

I should mention in this case that there is also pexpect, which is a Python expect-alike.

在这种情况下,我应该提到还有pexpect,它与 Python 类似。

#!/path/to/expect
spawn python manage.py syncdb
expect "login:*"
send -- "myuser\r"
expect "*ssword:*"
send -- "mypass\r"
interact

回答by SiegeX

If the program in question cannot read the input from stdinsuch as:

如果有问题的程序无法从stdin以下位置读取输入:

echo "some input" | your_progam

then you'll need to look to something like expectand/or autoexepect

那么你需要寻找类似expect和/或autoexepect

回答by nunopolonia

You can give defaults values to the variables. In line 4 and 5, if the variables RSRC and LOCAL aren't set, they are set to those default values. This way you can give the options to your script or use the default ones

您可以为变量提供默认值。在第 4 行和第 5 行中,如果未设置变量 RSRC 和 LOCAL,则将它们设置为这些默认值。通过这种方式,您可以为脚本提供选项或使用默认选项

#!/bin/bash
RSRC=
LOCAL=
: ${RSRC:="/var/www"}
: ${LOCAL:="/disk2/backup/remote/hot"}
rsync -avz -e 'ssh ' user@myserver:$RSRC $LOCAL

回答by mpenkov

You can do it like this, given an example login.pyscript:

给出示例login.py脚本,您可以这样做:

if __name__ == '__main__':
    import sys
    user = sys.stdin.readline().strip()
    passwd = sys.stdin.readline().strip()

    if user == 'root' and passwd == 'password':
        print 'Login successful'
        sys.exit(0)

    sys.stderr.write('error: invalid username or password\n')
    sys.exit(1)

good-credentials.txt

良好的凭据.txt

root
password

bad-credentials.txt

坏凭据.txt

user
foo

Then you can do the login automatically using:

然后您可以使用以下方法自动登录:

$cat good-credentials.txt | python login.py
Login successful

$cat bad-credentials.txt | python login.py 
error: invalid username or password

The down-side of this approach is you're storing your password in plain text, which isn't great practice.

这种方法的缺点是您以纯文本形式存储密码,这不是很好的做法。