【问题标题】:Expect script: "event not found" while passing in argument [duplicate]期望脚本:传入参数时“未找到事件”[重复]
【发布时间】:2016-09-26 17:11:18
【问题描述】:

我在使用期望脚本重置服务器数据库中的密码时收到以下消息。当我尝试重置包含“!”的密码时,我只会收到此错误。作为第一个字符。

[root@server]# ./changepw username !password!
-bash: !password!: event not found

我的脚本如下:

#!/usr/bin/expect -f

## Set up variables to be passed in as command line arguments
lassign $argv username password

spawn telnet 127.0.0.1 23
expect "200 PWD Server ready"
send "USER admin\r"
expect "300 please send the PASS"
send "PASS password\r"
expect "200 login OK, proceed"

# Use the line below for a password that must be quoted ie one  that contains a $ or a ! by escaping the double quotes
send "SETACCOUNTPASSWORD $username PASSWORD \"$password\"\r"

expect "200 OK"
send "quit\r"
interact

当我在 CLI 脚本之外手动执行此操作并用引号将密码括起来时,这似乎确实有效。它只会因脚本而失败。

【问题讨论】:

  • 与您的脚本无关。 event not found 错误甚至在您的期望脚本启动之前发生 - 在此之前,shell 甚至会检查是否存在 changepw 命令。

标签: bash expect


【解决方案1】:

这根本不是您的期望脚本的问题,而是您运行它的交互式 shell 的问题:在启用历史扩展的交互式 shell 中,!foo(带有非数字参数 @ 987654322@) 指的是历史上最近的命令,以foo开头。


使用set +H 关闭历史扩展(在您的~/.bashrc 中,如果您希望它默认关闭),或者在感叹号周围加上单引号:

set +H                           # turn off history expansion...
./changepw username !password!   # ...and this will now work

...或...

./changepw username '!password!' # or just add quotes

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-21
    • 1970-01-01
    • 2011-06-01
    • 2015-11-19
    • 2011-12-01
    • 1970-01-01
    • 2017-09-02
    • 1970-01-01
    相关资源
    最近更新 更多