3
votes

I've spent the better part of the day now searching everywhere for an answer, and nothing has worked so far. What I'm trying to do is run an expect script that will attach to a GNU screen session, send it the command line arguments (a command), and output the result of that one command. So far this is one of my scritps:


#!/usr/bin/expect -f


set timeout 3
#exp_internal 1

spawn screen -raAd call_sess
sleep 1

expect -re "\\\$" {
  send $argv
  send "\n"
  sleep 1
}

expect -re ".*" {
    set output $expect_out(0,string)
}

expect "\$" {
  send "\001"
  send "d"
}

expect eof {
  puts "OUTPUT---------------"
  puts $output
  puts "OUTPUT---------------"
}

And $expect_out(0,string) only matches a newline, even though .* should match everything since the last match. If anyone could help me get this working that would be awesome, I'm about ready to give up and call it impossible.

EDIT: Answered in the comments, but I should have specified that the command does get run in the screen, I just can't capture the output correctly.

EDIT2: Changed the script to spawn screen as suggested, thanks!

1
It's a guess: shouldn't you send '\r' after sending $argv?Hai Vu
'\n' works for sending a command, that much I know for sure. Running 3.6.5-1-ARCH if that makes a difference. The command gets run, I just can't capture the output correctly.apottere
I don't know much about Expect, but can you work around by redirecting the output of that command to a temp file, then read off that file?Hai Vu
I thought about that, but that won't work in all cases. I want this to be a dumb wrapper, and work for any command.apottere

1 Answers

2
votes

The easy part of the answer is that .* is matching the empty string, so that's going into expect_out.

The hard part is that screen will give you the whole screen - including any previous commands -, so just finding the output of the command you sent might be a little difficult.