在expect脚本中,grep的输出不保存在expect_out(缓冲区)中

时间:2017-09-05 22:58:15

标签: bash expect

我有一个脚本可以将文件压缩到某个值,但grep的响应不会保存在expect_out(缓冲区)中。以下是我的代码:

#!/usr/bin/expect 

set prompt "(%|>|#|\\$)"
#set prompt "(%|>|\\$)"

set pidBash [ spawn bash ]
puts "\nStarting script....."

send -- "\n"
expect -re $prompt {}
set output $expect_out(buffer)
send -- "grep '^option' /home/pb791b/udhcpd.conf | grep lease\n"
sleep 4
expect -re $prompt {}

set output $expect_out(buffer)
set outputLines [split $output "\n"]
set len [llength $outputLines]
puts "\nlen: $len\n"
for {set i 0} {$i<[expr $len]} {incr i} {
    set line [lindex $outputLines $i]
    puts "line $i: $line"
}

exec kill $pidBash
puts "\nprocess Bash terminated"

puts "End script."

以下是我的输出:

pb791b@pb791b-VirtualBox:~/devtest/ngs/base/Tests/shellScripts$ ./temp2.sh 
spawn bash

Starting script.....

pb791b@pb791b-VirtualBox:~/devtest/ngs/base/Tests/shellScripts$ 
grep '^option' /home/pb791b/udhcpd.conf | grep lease
pb791b@pb791b-VirtualBox:~/devtest/ngs/base/Tests/shellScripts$ grep '^option' /home/pb791b/udhcpd.conf | grep lease
option  lease   864000      # 10 days of seconds
pb791b@pb791b-VirtualBox:~/devtest/ngs/base/Tests/shellScripts$ 
len: 3

line 0:  
line 1: grep '^option' /home/pb791b/udhcpd.conf | grep lease
line 2: pb791b@pb791b-VirtualBox:~/devtest/ngs/base/Tests/shellScripts$

process Bash terminated
End temp.sh script.
pb791b@pb791b-VirtualBox:~/devtest/ngs/base/Tests/shellScripts$ 

当我打印expect_out(缓冲区)时,它打印grep命令然后输出提示但是缺少grep的输出&#34;选项租约864000#10天秒&#34;

1 个答案:

答案 0 :(得分:0)

删除send -- "\n",它应该有效:

set pidBash [ spawn bash ]
puts "\nStarting script....."

#send -- "\n"
expect -re $prompt {}

使用send -- "\n",您应该期望提示两次(当您登录时看到第一个提示,然后按\n,您将看到第二个提示),然后再发送grep命令:< / p>

set pidBash [ spawn bash ]
puts "\nStarting script....."

send -- "\n"
expect -re $prompt {}
expect -re $prompt {}