When I try to execute autoexpect file I get an error send: spawn id exp7 not open
Here is my file sh.exp
:
#!/usr/bin/expect
# mysql credentials and connection data
db_host='localhost'
db_name='webui_dev'
db_user='root'
db_pass=''
new_db_name='db_2011'
expect <<EOF
log_user 0
spawn mysql -h $db_host -u $db_user -p $db_pass 'create database $new_db_name'
expect "password:"
send "$db_pass\r"
log_user 1
expect eof
EOF
Can't find where is an error.
Try quoting your variables properly:
Variables not quoted in double quotes are subject to word splitting and pathname expansion. And variables under single quotes don't expand. Read more about shell quoting here.
Update: It seems that you're actually expanding it through a here document, but it would still apply since your arguments still need to be quoted in expect. This is what it would appear as input to expect:
This is how it would appear if you haven't quoted it yet:
UPDATE:
The cause of the problem actually is because mysql ends quickly without showing a prompt for password due to the extra argument. The solution is to send the command manually. It's also preferable to just run the whole script as an expect script, not an embedded one for lesser confusion
Save the script as an expect file and run it with
expect -f expect_file
.