user3319356
user3319356

Reputation: 173

expect script, puting name of file in command

I have problems with expect script. Well when I grep this file I need to put it to line under and it should look like :

/opt/ericsson/arne/bin/import.sh -f bla_bla_bla.xml -val:rall

but I don't know how to put this file in beetween this line. Because when I have put grep command in beetween in didn't work, maybe problem is -val:rall that I have after? If someone know's how could I put name of file in File1

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

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

set PASSWORD ter
set DUL [lindex $argv 0]
set VAR _cus_ipsec
match_max 1000
spawn ssh mashost

expect {
    "assword"  {send "$PASSWORD\r"}
}
        expect "ran@rn23"
        send -- "cd /tih/opt/bla/tih/ \r"
        expect "ran@rn23"
        send -- "grep -il $DUL * \r*"
        expect "ran@rn23"
        send -- "/opt/bl/arne/bin/imp.sh -f File1 -val:rall\r"
        expect "ran@rn03"
        send -- "/opt/bl/arne/b/imp.sh -f File1 -import\r"
        expect "ran@rn23"
interact

Upvotes: 1

Views: 583

Answers (1)

James
James

Reputation: 1236

Ok, thanks for the clarification, I believe I do understand what you're trying to do now.

What you need to do is change the expect statement you have after you send the grep command to one that will capture your filename. And you will probably benefit from using the regexp mode of the expect command (-re), and possibly using parenthesis to capture the filename (not used in my sample below). I do not know what are the possible filenames that you can get from your grep, so you will probably need to tweak the below quite a bit, but assuming your grep will give you a single .xml file beginning with "NAME", you could do something like the following:

send -- "grep -il $DUL * \r*"
expect -re "NAME.*\.xml"
send -- "/opt/bl/arne/bin/imp.sh -f $expect_out(0,string) -val:rall\r"

As a suggestion, you should really include some timeout options for your expect statements, and some error checking, otherwise this script will not stop if anything does not go as expected. E.g. only send if you have found what you expected, etc.

Your regexp probably will be more complicated than the one I showed you, but you can get the idea. Also, include exp_internal 1 somewhere near the top of your script to get good, solid debugging info on what your script is matching (or not matching). It will be very useful as you test it.

Let me know how that goes.

Upvotes: 1

Related Questions