我有以下任务要做。
步骤2-5应该在循环中运行,直到.txt文件中的所有ID都结束为止,步骤5将选择'no‘或退出。
在Shell/expect中的代码下面尝试过,但有时它跳过列表中的ID或显示空白值,有时在运行时会崩溃并引发错误:
*child process exited abnormally
while executing
"exec cat output.txt | grep -i -B2 "rows selected" > result.txt"
(file "./cmp-test.sh" line 31)*这里是代码:
exec echo "" > output.txt
log_file [pwd]/output.txt
set f [open list.txt r]
# list is the file name contain ID's
set idlist [ split [ read $f ] "\n" ]
close $f
send_user "\n Running script.. \n"
spawn <abc command>
foreach ids $idlist {
expect {
"run? " { send "1\r" }
}
expect {
"ACDIG: " { send "$ids\r" }
}
expect {
"n)?" { send "y\r" }
}
}
exec cat output.txt | grep -i -B2 "rows selected" > result.txt发布于 2016-09-19 21:10:49
你可能不需要期待这样的结果:
while read -r id; do
abc << "answers"
1
yes
$id
y
answers
done < ids.txt或
while read -r id; do
printf "%s\n" 1 yes "$id" y | abc
done < ids.txt您之所以获得child process exited abnormally,是因为当grep在其输入中找不到给定的模式时,它会以非零状态退出:这是一个“正常”的grep退出状态,但由于它不是零,Tcl/expect认为出现了错误。
发布于 2016-09-19 21:26:13
尝试将spawn包含在foreach循环中。例如:
#!usr/bin/expect
exec echo "" > output.txt
log_file -a output.txt
set f [open list.txt r]
set idlist [read $f]
close $f
send_user "\n Running script.. \n"
foreach ids $idlist {
spawn <abc command>
expect "run? "
send "1\r"
expect "ACDIG: "
send "$ids\r"
expect "n)?"
send "y\r"
}
log_file
catch {exec cat output.txt | grep -i -B2 "rows selected" > result.txt}希望第二个expect中额外的空白不会引起任何问题。
此外,您是否在脚本的末尾关闭log_file进程?
https://stackoverflow.com/questions/39581491
复制相似问题