期望,将输出分配给缓冲区

时间:2014-03-11 09:47:36

标签: buffer expect

#!/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(文件名)的输出我需要放在这里所示的行中,但我不知道如何。我在这里尝试了一些建议,但我还是无法管理它。谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

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

Expect看到所有这些输出

  

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

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

  

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”(捕获所有这些),然后是一些字符,提示和结束串。 反斜杠加倍,因为我们使用的是双引号字符串,反斜杠必须按字面顺序传递给正则表达式引擎。