我有以下任務執行。使用expect發送多個響應交互式Linux命令
- 運行Linux命令「ABC」,它會要求多響應
- 首先,有多個線,它是要求從1-10中選擇選項。以'run?'結尾,必須選擇1
- 第二,以'是/否'結束。總是'是'的迴應
- 三,輸入ID。從一個.txt文件取一個ID作爲輸入。每行有一個ID。
- 四,y/n。始終選擇「y」作爲迴應。
步驟2-5應循環運行,直到.txt文件中的所有ID都結束,步驟5將選擇「否」或僅退出。
Tried below code in Shell/expect but sometimes it skip the ID's from list or show blank value and sometimes get crash while running and throw error:
*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
其中是腳本讀取list.txt文件? –
@anujtyagi這裏是腳本的其餘部分按要求。我很確定'read'自動地通過換行符分割輸入文件,所以'split'幾乎是多餘的。順便說一下,我添加了一個catch命令來擺脫那個惱人的grep錯誤... – 2016-09-20 16:49:16