简体   繁体   English

stdout流在期望脚本中重定向

[英]stdout stream redirect in expect scripts

I have the following bash script which wraps an expect script (in here-document form): 我有以下bash脚本,其中包装了一个期望脚本(此处文档形式):

#!/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. 经过一番尝试和错误后,我发现stdout流重定向> somebackupfile.sql是罪魁祸首-一旦删除了此重定向,脚本便可以工作。

So I am wondering: How do I use stream redirection in expect scripts? 所以我想知道:如何在期望脚本中使用流重定向?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM