期望脚本:"event not found" 同时传入参数

Expect script: "event not found" while passing in argument

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

[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 的脚本之外手动执行此操作并将密码括在引号中时,这似乎确实有效。它只会因脚本而失败。

这根本不是您的 expect 脚本的问题,而是交互式 shell 的问题,您 运行 它来自:在交互式 shell启用历史扩展后,!foo(带有非数字参数 foo)指的是历史中以 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