【问题标题】:Expect command not found in script在脚本中找不到期望命令
【发布时间】: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解释。

标签: bash shell expect


【解决方案1】:

您基本上希望在 bash 脚本中包含一个 expect 脚本,如下所示:

#!/bin/bash

homeDir="/home"

if [ $# -eq 1 ]; then
    CLIENT="$1"
    ./easyrsa build-client-full "$CLIENT" nopass
elif [ $# -eq 2 ]; then
    CLIENT="$1"
    password="$2"
    /usr/bin/expect <<EOF
    ...
    DO EXPECT STUFF
    ...
EOF
else
    echo "script <username> <password (optional)>"
fi

【讨论】:

  • 这样做我有权限错误。 :awk: fatal : impossible to open file « ... »
  • 不确定你做了什么,但我建议的脚本中没有 awk
  • 我认为这是因为我调用的脚本 (./easyrsa) 没有足够的权限,如果我运行通过期望
  • 也许在您原来的问题下点击edit 并粘贴您现在正在运行的代码。
  • 我不是脚本./easyrsa的作者,这个脚本它自己调用另一个脚本
猜你喜欢
  • 1970-01-01
  • 2014-08-11
  • 2015-06-27
  • 2020-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-01
  • 1970-01-01
相关资源
最近更新 更多