2

I am getting the following message while using an expect script to reset a password in a server database. I only get this error when I try to reset a password that contains a "!" as the first character.

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

My script is as follows:

#!/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

This does seem to work when I do this manually outside of the script from CLI while enclosing the password in quotes. It only fails with the script.

user53029
  • 675
  • 1
  • 8
  • 23
  • Nothing to do with your script. The `event not found` error happens before your expect script is even started -- before, for that matter, the shell even checks whether a `changepw` command exists at all. – Charles Duffy Sep 26 '16 at 17:19

1 Answers1

4

This isn't a problem with your expect script at all, but is rather an issue with the interactive shell you're running it from: In an interactive shell with history expansion enabled, !foo (with a nonnumeric argument foo) refers to the most recent command in history starting with foo.


Use set +H to turn off history expansion (in your ~/.bashrc if you want it off-by-default), or put single quotes around your exclamation marks:

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

...or...

./changepw username '!password!' # or just add quotes
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441