bash 期望输出写入文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19952070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Expect output write to a file
提问by sauletasmiestas
I am trying to login to a remote device and write it`s output to a file. I came up with this code. But in rez.txt file i get line "(buffer)"
我正在尝试登录远程设备并将其输出写入文件。我想出了这个代码。但是在 rez.txt 文件中我得到了“(缓冲区)”行
My code is very basic - from manual, i do not know what is wrong:
我的代码非常基本 - 从手册中,我不知道出了什么问题:
/usr/bin/expect << SSHLOGIN
spawn ssh -l $user $host
set timeout 100
expect {
"assword: " {
send "$password\r"
}
}
expect {
">" {
send "?\r"
}
}
expect {
"?" {
puts [open rez.txt w] $expect_out(buffer)
}
}
expect {
">" {
send "exit\r"
}
}
SSHLOGIN
回答by Alaa Ali
It seems that you are calling Expect in a bash script.
您似乎是在 bash 脚本中调用 Expect。
The $expect_out
part of $expect_out(buffer)
is being substituted by the shell. Since $expect_out
is not set to anything, the Expect program is actually doing this:
的$expect_out
部分$expect_out(buffer)
正在被外壳取代。由于$expect_out
未设置任何内容,Expect 程序实际上是这样做的:
puts [open rez.txt w] (buffer)
So it's basically putting the word (buffer)
.
所以它基本上是把这个词(buffer)
。
To fix this, you need to escape $expect_out
so that it is not expanded by the shell and passed correctly to Expect, like this:
要解决此问题,您需要转义,$expect_out
以便它不会被 shell 扩展并正确传递给 Expect,如下所示:
puts [open rez.txt w] $expect_out(buffer)
回答by glenn Hymanman
expect's default matching mode is "glob"and ?is a special glob character. After you send "?", when expect sees a single character, your output file is written.
expect 的默认匹配模式是"glob"并且?是一个特殊的 glob 字符。发送“?”后,当期望看到单个字符时,将写入您的输出文件。
If you're expecting a literal ?, then you want to use exactmatching:
如果您期待一个字面量?,那么您想使用精确匹配:
expect -ex "?" {
puts [open rez.txt w] $expect_out(buffer)
}