【发布时间】:2020-11-24 06:38:36
【问题描述】:
在expect脚本中我无法转义方括号,示例如下,
我有一个 question.sh 脚本如下,
#!/bin/bash
echo "Would you like to specify inputs and execute the script? [Y/n]"
read $REPLY
echo "Deleted Item with ID: 50 and do cleanup? [Y/n]"
read $REPLY
echo "Deleted Item with ID: 51 and do cleanup? [Y/n]"
read $REPLY
echo "Deleted Item with ID: 52 and do cleanup? [Y/n]"
read $REPLY
对于上面的question.sh我有下面的answer.exp,用'expect answer.exp 51'命令执行同样的,
#!/usr/bin/expect -f
set timeout -1
spawn ./question.sh
set item "Deleted Item with ID: "
append item [lindex $argv 0]
append item " and do cleanup? \[Y/n\]"
expect "Would you like to specify inputs and execute the script? \[Y/n\]"
send -- "Y\r"
expect {
$item {
send "Y\r"
exp_continue
}
" and do cleanup? \[Y/n\]" {
send "n\r"
exp_continue
}
}
当我使用方括号时,期望不匹配并且不会将答案 (Y/N) 发回。
当我从问题中删除方括号并且答案脚本期望工作正常时,相同,更改后的问题和答案脚本如下。
question.sh:
#!/bin/bash
echo "Would you like to specify inputs and execute the script? Y/n"
read $REPLY
echo "Deleted Item with ID: 50 and do cleanup? Y/n"
read $REPLY
echo "Deleted Item with ID: 51 and do cleanup? Y/n"
read $REPLY
echo "Deleted Item with ID: 52 and do cleanup? Y/n"
read $REPLY
answer.exp:
#!/usr/bin/expect -f
set timeout -1
spawn ./question.sh
set item "Deleted Item with ID: "
append item [lindex $argv 0]
append item " and do cleanup? Y/n"
expect "Would you like to specify inputs and execute the script? Y/n"
send -- "Y\r"
expect {
$item {
send "Y\r"
exp_continue
}
" and do cleanup? Y/n" {
send "n\r"
exp_continue
}
}
【问题讨论】:
-
看看我的sexpect (Expect for Shells),你可以用它来编写仅使用shell代码的Expect脚本。