bash - 预期脚本 : "event not found" while passing in argument

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

最佳答案

这根本不是您的期望脚本的问题,而是您运行它的交互式 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

关于bash - 预期脚本 : "event not found" while passing in argument,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39708594/

相关文章:

linux - 如何使用expect只记录命令的输出

linux - mktemp 的问题

bash - bash 中的一行 if 语句

linux - bash 脚本中带有空格的基本名称?

linux - 最后期望的指令没有执行

linux - 在期望脚本中发送不可打印的字符

bash - 如何暂时只在 bash 脚本中设置 PATH?

bash - 关键字 “if” 如何测试一个值是真还是假?

perl - 在expect脚本中转义双引号

linux - 用于在远程 SFTP 服务器上移动文件的 Shell 脚本