简体   繁体   English

使用expect向交互式Linux命令发送多个响应

[英]Send multiple response to interactive Linux command using expect

I have below task to perform. 我有以下任务执行。

  1. Run a linux command 'abc' and it will ask for multiple response 运行linux命令'abc',它会要求多个响应
  2. First, there are multiple lines and it is asking to select option from 1-10. 首先,有多行,要求从1-10中选择选项。 Ending with 'run?', always have to select 1 以'run?'结尾,总是必须选择1
  3. Second, Ending with 'yes/no'. 第二,以'是/否'结束。 Always 'yes' response 总是'是'回应
  4. Third, Enter ID. 第三,输入ID。 Take one ID as input from a .txt file. 从.txt文件中输入一个ID作为输入。 There is one ID in each row. 每行中有一个ID。
  5. Fourth, y/n. 第四,y / n。 Always select "y" as response. 始终选择“y”作为响应。

Step 2-5 should run in loop till all ID's in .txt file get over and step 5 will select 'no' or just exit. 步骤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: 尝试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)*

Here is the code: 这是代码:

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

You probably don't need expect for this: 你可能不需要期望:

while read -r id; do
abc << "answers"
1
yes
$id
y
answers
done < ids.txt

or 要么

while read -r id; do
    printf "%s\n" 1 yes "$id" y | abc
done < ids.txt

You're getting the child process exited abnormally because grep exits with a non-zero status when it can't find the given pattern in its input: that's a "normal" grep exit status, but because it is non-zero, Tcl/expect thinks an error occurred. 您正在使child process exited abnormally退出,因为当grep在其输入中找不到给定的模式时, grep退出非零状态:这是一个“正常”的grep退出状态,但因为它非零,所以Tcl /期待发生错误。

Try including spawn within the foreach loop. 尝试在foreach循环中包含spawn For example: 例如:

#!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}

Hope that extra white space in your second expect isn't causing any issues. 希望你的第二个expect额外的空白不会导致任何问题。

Furthermore, are you closing the log_file process at the end of your script? 此外,您是否在脚本结束时关闭了log_file进程?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM