期望,将输出分配给缓冲区 [英] expect, assign output to buffer

查看:104
本文介绍了期望,将输出分配给缓冲区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#!/usr/local/bin/expect -- 



set env(TERM) vt100
set env(SHELL) /bin/sh
set env(HOME) /usr/local/bin

set PASSWORD eri
set DUL [lindex $argv 0]
match_max 100000
spawn ssh mashost

expect {
    "assword"  {send "$PASSWORD\r"}
}
        expect "ranosusr@rn2osscs603"
        send -- "cd /var/opt/bla/edd/ARNE_SIU \r"
        expect "ranosusr@rn2osscs603"
        send -- "grep -il $DUL *\r"
        expect -re "$DUL.*\.xml"
        set outcome $expect_out(0,string)
        expect "ranosusr@rn2osscs603"
        send -- "/opt/bla/arne/bin/import.sh -f $outcome -val:rall\r"
        expect "ranosusr@rn2osscs603"
interact

,当我运行它

ssh mahost
Password:
ranosusr@rn2osscs603> cd /var/opt/bla/edd/ARNE_SIU
ranosusr@rn2osscs603> grep -il FXLP89 *
FXLP89_FRTALZ_SIU_ARNE.xml
ranosusr@rn2osscs603> /opt/bla/arne/bin/import.sh -f FXLP89 *
Logging to file /var/opt/bla/arne/ARNE_Import_Log.2014-03-11_10:37:47
Failed to write to file. Writing to stdout instead.
The file 'FXLP89' can not be found.
ranosusr@rn2osscs603>
ranosusr@rn2osscs603>
ranosusr@rn2osscs603> FXLP89_FRTALZ_SIU_ARNE.xml -val:rall
FXLP89_FRTALZ_SIU_ARNE.xml: Command not found.
ranosusr@rn2osscs603>

,其值应为:

ranosusr@rn2osscs603> cd /var/opt/bla/edd/ARNE_SIU
ranosusr@rn2osscs603> grep -il FXLP89 *
FXLP89_FRTALZ_SIU_ARNE.xml
ranosusr@rn2osscs603>/opt/bla/arne/bin/import.sh -f FXLP89_FRTALZ_SIU_ARNE.xml -val:rall

所以基本上,grep的输出(文件名)我需要放在下面显示的行中,但是我不知道如何。我尝试了这里的一些建议,但仍然无法解决。谢谢您的帮助。

so basicly, output of grep (name of file) I need to put in the line under as showed here, but I don't know how. I tried some suggestion from guys here, but I can't manage it still. Thanks for any help.

推荐答案

之后

send -- "grep -il $DUL *\r"

期望看到所有这些输出


ranosusr @ rn2osscs603> grep -il FXLP89 *

FXLP89_FRTALZ_SIU_ARNE.xml

ranosusr @ rn2osscs603>

ranosusr@rn2osscs603> grep -il FXLP89 *
FXLP89_FRTALZ_SIU_ARNE.xml
ranosusr@rn2osscs603>

与正则表达式 $ DUL。*。xml匹配的部分以粗体显示:

The part that is matched by the regular expression "$DUL.*.xml" is in bold:


ranosusr @ rn2osscs603> grep -il FXLP89 *

FXLP89_FRTALZ_SIU_ARNE.xml


ranosusr @ rn2osscs603>

ranosusr@rn2osscs603> grep -il FXLP89 *
FXLP89_FRTALZ_SIU_ARNE.xml

ranosusr@rn2osscs603>

您应该期望:

set prompt {ranosusr@rn2osscs603> }
expect -re "($DUL\\S+\\.xml).*$prompt$"
set filename $expect_out(1,string)

在这里,我们期望的模式是:$ DUL的值,后跟一些非空白字符,然后是文字点和 xml(捕获所有内容),后跟一些字符,提示和字符串结尾。
反斜杠加倍,因为我们使用的是双引号引起来的字符串,并且反斜杠必须按字面值传递给regex引擎。

Here, we're expecting the pattern: the value of $DUL followed by some non-whitespace chars followed by a literal dot and "xml" (capture all of that) followed by some chars and the prompt and the end of string. The backslashes are doubled because we're using a double quoted string, and the backslashes have to be passed literally to the regex engine.

这篇关于期望,将输出分配给缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆