在bash脚本中模拟用户input

我正在创build自己的bash脚本,但是现在我陷入了困境。 基本上,脚本将被用来在CentOS中自动进行服务器设置。 有些软件通常会要求用户input密码。 我希望脚本把我已经生成并存储为variables的密码,而不是询问用户。

在安装过程中出现消息“New password:”时,如何让脚本将值存储在variables$key ,就像用户使用bash脚本键入的那样?

你会发现'expect'命令会做你需要的。 它广泛可用。 看这里的例子: http : //www.thegeekstuff.com/2010/10/expect-examples/

(非常粗略的例子)

 #!/usr/bin/expect set pass "mysecret" spawn /usr/bin/passwd expect "password: " send "$pass" expect "password: " send "$pass" 

这是我为y脚本编写的一段代码,要求input用户的密码并将其设置在/ etc / passwd中。 你可以操纵它有点可能得到你所需要的:

 echo -n " Please enter the password for the given user: " read userPass useradd $userAcct && echo -e "$userPass\n$userPass\n" | passwd $userAcct > /dev/null 2>&1 && echo " User account has been created." || echo " ERR -- User acount creation failed!"