0

I have the following bash script which wraps an expect script (in here-document form):

#!/bin/bash

PASSWORD_MYSQL_ROOT=root

expect <<- DONE
  set timeout -1
  spawn mysqldump --all-databases --user=root --password --protocol=TCP --host=localhost --verbose > somebackupfile.sql
  expect "*?asswor?:*"
  send -- "$PASSWORD_MYSQL_ROOT\r"
  expect eof
DONE

When I execute this script, I get the following output:

spawn mysqldump --all-databases --user=root --password --protocol=TCP --host=localhost --verbose > somebackupfile.sql
Usage: mysqldump [OPTIONS] database [tables]
OR     mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]
OR     mysqldump [OPTIONS] --all-databases [OPTIONS]
For more options, use mysqldump --help
send: spawn id exp4 not open
    while executing
"send -- "root\r""

So something didn't work right.

After some try and error, I found out that the stdout stream redirect > somebackupfile.sql is the culprit -- the script works once this redirect is removed.

So I am wondering: How do I use stream redirection in expect scripts?

Abdull
  • 26,371
  • 26
  • 130
  • 172
  • See if my answer to [another similar question](http://stackoverflow.com/questions/31236483/how-to-create-a-mysql-database-from-dump-file-in-expect-tcl-script/31236758) can help. – pynexj Nov 16 '15 at 16:20

1 Answers1

0

untested, but this should work:

expect <<- DONE
  set timeout -1
  spawn mysqldump --all-databases --user=root --password --protocol=TCP --host=localhost --verbose
  expect "*?asswor?:*"
  send -- "$PASSWORD_MYSQL_ROOT\r"
  log_file somebackupfile.sql
  expect eof
DONE
glenn jackman
  • 238,783
  • 38
  • 220
  • 352