在 bash 脚本中模拟用户输入 [英] Simulate user input in bash script

查看:25
本文介绍了在 bash 脚本中模拟用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建自己的 bash 脚本,但目前我卡住了.基本上,该脚本将用于在 CentOS 中自动设置服务器.某些软件通常会要求用户输入密码.我希望脚本将我生成并存储为变量的密码放入而不是询问用户.

I am creating my own bash script, but I am stuck at the moment. Basically, the script would be used to automate server setup in CentOS. Some software normally asks the user to type a password. I want the script to put the password that I have generated and stored as a variable instead of asking the user.

当安装过程中出现消息新密码:"时,如何让脚本将存储在变量 $key 中的值当作用户输入的值,使用 bash 脚本?

When the message "New password:" appears during install, how can I make the script put the value stored in a variable $key as if the user had typed it, with a bash script?

推荐答案

你应该发现'expect'命令会做你需要它做的事情.它广泛可用.

You should find the 'expect' command will do what you need it to do. It's widely available.

一个非常粗略的例子:

#!/usr/bin/expect
set pass "mysecret"

spawn /usr/bin/passwd

expect "password: "
send "$pass"
expect "password: "
send "$pass"

这篇关于在 bash 脚本中模拟用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆