gpt4 book ai didi

linux - Bash 脚本响应控制台输出?

转载 作者:太空宇宙 更新时间:2023-11-04 04:55:02 24 4
gpt4 key购买 nike

当前正在尝试在启动时运行 bash 脚本来自动安装鱿鱼,但是我正在运行的命令需要输入。

目前我拥有的脚本是:

#!/bin/sh
PROXY_USER=user1
PROXY_PASS=password1
wget https://raw.githubusercontent.com/hidden-refuge/spi/master/spi && bash spi -rhel7 && rm spi
#After i run this command it asks "Enter username"
#followed by "Enter password" and "Renter password"
echo $PROXY_USER
echo $PROXY_PASS
echo $PROXY_PASS
echo yes

但是我无法让输入工作,并且脚本无法创建用户名和密码。我运行的是 centos 7。

最佳答案

看看您正在调用一些以交互模式运行的工具,例如 dani-gehtdichnixan在 ( passing arguments to an interactive program non interactively ) 中提到,您可以使用 expect 实用程序。

在 debian 上安装 expect:

apt-get install expect

创建一个脚本调用spi-install.exp,如下所示:

#!/usr/bin/env expect
set user username
set pass your-pass

spawn spi -rhel7
expect "Enter username"
send "$user\r"

expect "Renter password"
send "$pass\r"

然后在主 bash 脚本中调用它:

#!/bin/bash 
wget https://raw.githubusercontent.com/hidden-refuge/spi/master/spi && ./spi-install.exp && rm spi

Expect is used to automate control of interactive applications such as Telnet, FTP, passwd, fsck, rlogin, tip, SSH, and others. Expect uses pseudo terminals (Unix) or emulates a console (Windows), starts the target program, and then communicates with it, just as a human would, via the terminal or console interface. Tk, another Tcl extension, can be used to provide a GUI.

https://en.wikipedia.org/wiki/Expect

引用:

[1] passing arguments to an interactive program non interactively

[2] https://askubuntu.com/questions/307067/how-to-execute-sudo-commands-with-expect-send-commands-in-bash-script

[3] https://superuser.com/questions/488713/what-is-the-meaning-of-spawn-linux-shell-commands-centos6

关于linux - Bash 脚本响应控制台输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49333852/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com