【发布时间】:2019-01-14 18:20:36
【问题描述】:
我已经在 CentOS 7 上使用 yum install expect -y 安装了 expect 命令。我想在我的脚本中自动化一些输入,但它似乎不再在 bash 中解释。
这是我的脚本:
#!/usr/bin/expect -f
homeDir="/home"
if [ $# -eq 1 ]; then
CLIENT="$1"
./easyrsa build-client-full "$CLIENT" nopass
elif [ $# -eq 2 ]; then
CLIENT="$1"
password="$2"
set timeout -1
spawn ./easyrsa build-client-full "$CLIENT"
expect "Enter PEM pass phrase:"
send -- "$password"
expect eof
else
echo "script <username> <password (optional)>"
fi
我使用chmod +x script 使脚本可执行并像./script 一样运行它。
我得到的错误:
script: line11: spawn : command not found 无法读取文件 "Enter PEM 密码短语:": 没有这样的文件或目录 脚本:ligne13:发送: 找不到命令无法读取文件“eof”:没有这样的文件或 目录
如果我让whereis expect
我明白了:
expect: /usr/bin/expect /usr/share/man/man1/expect.1.gz
我想问一下是否有不使用其他库的替代方法?
我也尝试过使用这行代码,但这没有任何回报:
echo "$password" | ./easyrsa build-client-full "$CLIENT"
【问题讨论】:
-
你的shebang是
#!/usr/bin/expect -f。所以当你用./运行它时,脚本中的命令不会被bash解释,而是被expect解释。