Expect脚本执行报错:close-quote后存在多余字符问题排查
Expect脚本多选择场景执行报错解决
我编写Expect脚本处理自动化执行中的多选择场景,执行时偶尔会出现内部错误,发送^即可继续下一步,但代码执行失败。
原错误代码
expect { "Enter RETURN to continue or '^' to exit:" {send "^\n",exp_continue } "Select Systems Manager Menu Option:" } send "fm\n"
报错信息
extra characters after close-quote while executing "send "^\n",exp_continue " invoked from within "expect { -ex "Enter RETURN to continue or '^' to exit:" {send "^\n",exp_continue } -ex "Select Systems Manager Menu Option:" }" (file "./ClinicStop_extract.sh" line 29) [root@localhost cron]#
问题原因与修复
错误根源是send命令的语法误用:你用逗号将字符串和exp_continue拼接,但send仅接受字符串参数,exp_continue是Expect的流程控制命令,必须单独作为一行语句执行。
修正后的代码:
expect { "Enter RETURN to continue or '^' to exit:" { send "^\n" exp_continue } "Select Systems Manager Menu Option:" } send "fm\n"
修正逻辑说明:
- 匹配到第一个提示时,先执行
send "^\n"发送指定字符,再通过exp_continue让Expect持续监听后续匹配项 - 匹配到第二个菜单提示后,退出expect块,执行后续的
send "fm\n"
内容的提问来源于stack exchange,提问作者alhadrusi
相关产品推荐
相关产品推荐

