如何在期望中转义方括号?
How to escape square brackets in expect?
我无法在 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
}
}
每当我使用方括号时,expect 都不匹配并且不会发送回答案 (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
}
}
expect
命令默认匹配 tcl string match
style wildcard patterns,因此 [Y/n]
试图匹配字符 Y、正斜杠或 n 之一。
解决方案:
- 添加更多反斜杠:
append item " and do cleanup? \\[Y/n\\]"
所以变成了\[Y/N\]
.
- 改用大括号:
append item { and do cleanup? \[Y/n\]}
防止替换反斜杠。
- 通过在每个模式前使用
-ex
选项告诉 expect
进行精确匹配:
#!/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 -ex "Would you like to specify inputs and execute the script? \[Y/n\]"
send -- "Y\r"
expect {
-ex $item {
send "Y\r"
exp_continue
}
-ex " and do cleanup? \[Y/n\]" {
send "n\r"
exp_continue
}
}
我无法在 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
}
}
每当我使用方括号时,expect 都不匹配并且不会发送回答案 (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
}
}
expect
命令默认匹配 tcl string match
style wildcard patterns,因此 [Y/n]
试图匹配字符 Y、正斜杠或 n 之一。
解决方案:
- 添加更多反斜杠:
append item " and do cleanup? \\[Y/n\\]"
所以变成了\[Y/N\]
.
- 改用大括号:
append item { and do cleanup? \[Y/n\]}
防止替换反斜杠。
- 通过在每个模式前使用
-ex
选项告诉expect
进行精确匹配:
#!/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 -ex "Would you like to specify inputs and execute the script? \[Y/n\]"
send -- "Y\r"
expect {
-ex $item {
send "Y\r"
exp_continue
}
-ex " and do cleanup? \[Y/n\]" {
send "n\r"
exp_continue
}
}