Question

#!/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

and when I run it

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>

and it shold be:

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

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.

Was it helpful?

Solution

After you

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

Expect sees all this output

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

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>

You should expect this:

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top