我在使用期望脚本重置服务器数据库中的密码时收到以下消息。仅当我尝试重置包含“!”的密码时,才会出现此错误。作为第一个字符。
[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/