Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm trying to run some expect language, and grep/parse the output all in one script. I want to grep the output of and look for "error" (I should note that standard linux commands like awk, sed, grep, etc are not available on the remote VPlexcli machine)

#!/bin/bash

expect - << EOF
# connect to vplexcli
spawn vplexcli
# Look for login prompt
expect -re "Name:"
# Send login
send "service\r"
# Look for password prompt
expect -re "Password:"
# Send password
send "letmein123\r"
expect -re "VPlexcli:/> "
send "ll /clusters/cluster-1/storage-elements/\r"
expect -re "VPlexcli:/> "
send "exit\r"
EOF

Output looks like this:

VPD83T3:6006016036c02c00e217465c0516e211  ok            APM00121002844.SPA  APM00121002844.SPB  both        0x002e000000000000  implicit-explicit
VPD83T3:6006016036c02c00e4dc0671f907e211  ok            APM00121002844.SPA  APM00121002844.SPB  both        0x0010000000000000  implicit-explicit
VPD83T3:6006016036c02c00ec79619bdd08e211  error         APM00121002844.SPA  APM00121002844.SPB  none                            implicit-explicit
VPD83T3:6006016036c02c00f0bfd3dedd08e211  error         APM00121002844.SPA  APM00121002844.SPB  none                            implicit-explicit
share|improve this question
    
Please show some typical output from the expect program –  glenn jackman Dec 10 '12 at 16:40
    
grep "error"... and then what? Note that you can expect the string "error" in your expect script, and exit with an error code if matched. –  angus Dec 10 '12 at 17:29
    
I just want a list returned to me with items on the "error" lines. VPD83T3:6006016036c02c00f0bfd3dedd08e211 –  LVLAaron Dec 10 '12 at 21:37

2 Answers 2

Is this what you want?

#!/bin/bash

(expect - << EOF

...

EOF
) | while read line; do if [[ "$line" =~ "error" ]]; then  echo $line; fi; done

Surprised you don't have grep.

share|improve this answer

You can capture the output and parse it within expect

#!/usr/bin/expect -f

# connect to vplexcli
spawn vplexcli
# Look for login prompt
expect -re "Name:"
# Send login
send "service\r"
# Look for password prompt
expect -re "Password:"
# Send password
send "letmein123\r"
expect -re "VPlexcli:/> "
send "ll /clusters/cluster-1/storage-elements/\r"
expect -re "(.*)VPlexcli:/> "

foreach line [split $expect_out(1,string) \n] {
    if {[string match *error* $line]} {
        puts $line
    }
}

send "exit\r"
expect eof
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.