在 expect 脚本中,grep 的输出未保存在 expect_out(buffer) 中
In expect script, output of grep is not saved in expect_out(buffer)
我有一个脚本可以根据某个值对文件进行 grep,但是 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(buffer) 时,它会打印 grep 命令,然后是提示,但缺少 grep "option lease 864000 # 10 days of seconds"
的输出
删除 send -- "\n"
它应该可以工作:
set pidBash [ spawn bash ]
puts "\nStarting script....."
#send -- "\n"
expect -re $prompt {}
在发送 grep 命令之前,使用 send -- "\n"
你应该期待两次提示(当你登录时你会看到第一个提示,然后你按 \n
你会看到第二个提示) :
set pidBash [ spawn bash ]
puts "\nStarting script....."
send -- "\n"
expect -re $prompt {}
expect -re $prompt {}
我有一个脚本可以根据某个值对文件进行 grep,但是 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(buffer) 时,它会打印 grep 命令,然后是提示,但缺少 grep "option lease 864000 # 10 days of seconds"
的输出删除 send -- "\n"
它应该可以工作:
set pidBash [ spawn bash ]
puts "\nStarting script....."
#send -- "\n"
expect -re $prompt {}
在发送 grep 命令之前,使用 send -- "\n"
你应该期待两次提示(当你登录时你会看到第一个提示,然后你按 \n
你会看到第二个提示) :
set pidBash [ spawn bash ]
puts "\nStarting script....."
send -- "\n"
expect -re $prompt {}
expect -re $prompt {}