一、问题描述
在linux使用某些命令的时候,常常需要我们确认,例如输入回车,Y、yes等等。如果是键盘输入的话,没什么问题;如果需要脚本中检测某些关键词并且回应的时候,需要使用特定的模块except。
二、问题解决
1、安装有关模块
sudo apt-get install tcl tk expect
2、检测某些关键词并且给出回应
这里以nohup要求确认句中的“output”为例,
给出后台运行 python3 test.py 的代码。
#!/usr/bin/expect
set timeout 30
spawn nohup python3 test.py &
#检测关键词
expect "output"
#发送回车
send "\n"
expect eof