这是一个有点笨拙的问题,因为我想不出一个好的方法来描述它,但在expect中,您可以这样做:
interact {
\001 {do_something}
\003 {do_something_else}
"?" {
set timeout 1
expect_user {
"?" {send "?"}
timeout {send_user "show a menu of the things you can do"}
}
stty raw -echo
set timeout 60
}
\035 {send "^]"
send "quit\r"
send_user "\n"
exit
}
}
这将创建一个交互式会话,用户可以像往常一样进行业务,但只需按键盘组合键(
ctrl键
+
一
,
ctrl键
+
c
,
ctrl键
+
e
, ?, etc)执行操作或显示描述可能的快捷方式的文本。
我正在尝试将许多脚本更新到python&中;pexpect,但无法确定这在pexpect中是否可行。我尝试使用输入过滤器,但似乎这不是一个真正“正确”的地方,或者我似乎找不到任何好的实例。
@pynexj:尝试了你的脚本,尽管我没有从ctrl命令中得到任何关于stdout的信息。
16:51:16 ~/scripts $ p3 testInputFilter.py | tee testInput.txt
16:51:19 ~/scripts $ ps u
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
usr 45721 0.0 0.0 109620 1876 pts/1 Ss 16:49 0:00 -ksh
usr 46622 0.0 0.0 108436 1776 pts/1 S 16:51 0:00 bash
usr 46734 5.5 0.0 135728 7688 pts/1 S+ 16:51 0:00 python3.6 testI
usr 46735 0.0 0.0 100912 632 pts/1 S+ 16:51 0:00 tee testInput.t
usr 46736 0.0 0.0 108336 1692 pts/5 Ss 16:51 0:00 /bin/bash --nor
usr 46759 0.0 0.0 110236 1132 pts/5 R+ 16:51 0:00 ps u
16:51:21 ~/scripts $ ^C
16:51:42 ~/scripts $ exit
16:51:43 ~/scripts $ cat testInput.txt
16:51:19 ~/scripts $ ps u
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
usr 45721 0.0 0.0 109620 1876 pts/1 Ss 16:49 0:00 -ksh
usr 46622 0.0 0.0 108436 1776 pts/1 S 16:51 0:00 bash
usr 46734 5.5 0.0 135728 7688 pts/1 S+ 16:51 0:00 python3.6 testI
usr 46735 0.0 0.0 100912 632 pts/1 S+ 16:51 0:00 tee testInput.t
usr 46736 0.0 0.0 108336 1692 pts/5 Ss 16:51 0:00 /bin/bash --nor
usr 46759 0.0 0.0 110236 1132 pts/5 R+ 16:51 0:00 ps u
16:51:21 ~/scripts $ ^C
16:51:42 ~/scripts $ exit
16:51:57 ~/scripts $