交互式Bash课程:如何捕获用户输入并对其做出反应

时间:2021-03-03 15:45:08

I want to write a little 'game' for my girlfriend (and other people) so she can learn basic bash and linux commands with an interesting storyline.

我想为我的女朋友(以及其他人)写一个小游戏,这样她就可以通过一个有趣的故事情节来学习基本的bash和linux命令。

Since I don't want her to be able to explore without me manually checking if she does it right , I need a way to catch her interactions with the commandline and the output.

因为我不希望她能够在没有我手动检查她是否正确的情况下探索,我需要一种方法来捕捉她与命令行和输出的交互。

Right now I see two approaches none of which I know if it works:

现在我看到两种方法,我知道它是否有效:

  1. Use a python script as the interpreter/login-shell, pass commands on to os.system or subprocess and implement the 'game-logic' in that script. Problems with that: I can't teach her stuff like the ctrl-z,ctrl-d etc.

    使用python脚本作为解释器/ login-shell,将命令传递给os.system或subprocess,并在该脚本中实现“游戏逻辑”。问题:我不能教她的东西,如ctrl-z,ctrl-d等。

  2. Use the actual bash as interpreter/login shell and make my script for the 'game-logic' run prior/after every command (as a kind of hook). Is that possible without digging in the bash-code itself?

    使用实际的bash作为解释器/登录shell,并在每个命令之前/之后运行“游戏逻辑”运行的脚本(作为一种挂钩)。如果没有挖掘bash-code本身,这可能吗?

1 个解决方案

#1


1  

First off:

首先:

import signal, time
def signal_handler(signal, frame):
        print('You pressed Ctrl+<KEY>, ignoring!')
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTSTP, signal_handler)
while 1:
    time.sleep(1)

There's a start on one of your problems.
Secondly, I don't see how you want to "catch" her interactions.. But i'd assume you'd have to be present anyway so why not just let her have two terminals and let her have a go at it while the "game" is running in a separate console where you simply input y/n depending if she did the correct task?

你的一个问题就出现了。其次,我不知道你是怎么想“抓住”她的互动......但我认为你必须要在场,所以为什么不让她有两个终端让她去看看“游戏”是在一个单独的控制台中运行的,你只需输入y / n,这取决于她是否完成了正确的任务?

Or if you really wanna get geeky, why not set up a virtual environment where she can't do any harm and let her do whatever and reset it upon failure.. Give her 5 sets of instructions to do and see if she can google it? She needs to learn how to fetch information rather than learning "if i do what he says, i get cookie".

或者,如果你真的想变得怪异,为什么不建立一个虚拟环境,她不会做任何伤害,让她做任何事情,并在失败时重置它。给她5套指示,看看她是否可以google它?她需要学习如何获取信息,而不是学习“如果我做他说的话,我会得到cookie”。

There's better "game" scenarios to implement a learning curve then having you jump in and correct everything, give her keywords to search for and teach her to just try everything she sees on the google results.. It can't go wrong, after all.. It's just a computer..

有更好的“游戏”场景来实现学习曲线然后让你跳进并纠正一切,给她关键词搜索并教她只是尝试她在谷歌搜索结果上看到的一切......毕竟不会出错..这只是一台电脑......

A practical example:

  1. Create a virtual machine with qemu (guide is longer than it should, you only need a fraction to get started)
  2. 使用qemu创建一个虚拟机(指南比它应该更长,你只需要一小部分就可以开始)
  3. Install a base linux system on it
  4. 在其上安装基础Linux系统
  5. Take a snapshot

    拍一张快照

  6. Create a script that reverts to the last snapshot and boots the OS

    创建一个恢复到上一个​​快照并引导操作系统的脚本

  7. Give her (or him) a set of instructions such as "1: Install Python2.7, (keywords: archlinux python)"
  8. 给她(或他)一组说明,例如“1:安装Python2.7,(关键字:archlinux python)”

I did this for a couple of network labs, people could mess up all they wanted.. Didn't matter because the next time they rebooted (via the script) the machine was as good as new. Only thing you have to do, is invoke a new snapshot when she (or he) is in a good place in your tutorial.

我为几个网络实验室做了这个,人们可能会把他们想要的东西搞得一团糟。没关系,因为下次他们重新启动(通过脚本)机器就像新的一样好。只有你必须做的事情是,当她(或他)在你的教程中处于一个好位置时调用一个新的快照。

#1


1  

First off:

首先:

import signal, time
def signal_handler(signal, frame):
        print('You pressed Ctrl+<KEY>, ignoring!')
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTSTP, signal_handler)
while 1:
    time.sleep(1)

There's a start on one of your problems.
Secondly, I don't see how you want to "catch" her interactions.. But i'd assume you'd have to be present anyway so why not just let her have two terminals and let her have a go at it while the "game" is running in a separate console where you simply input y/n depending if she did the correct task?

你的一个问题就出现了。其次,我不知道你是怎么想“抓住”她的互动......但我认为你必须要在场,所以为什么不让她有两个终端让她去看看“游戏”是在一个单独的控制台中运行的,你只需输入y / n,这取决于她是否完成了正确的任务?

Or if you really wanna get geeky, why not set up a virtual environment where she can't do any harm and let her do whatever and reset it upon failure.. Give her 5 sets of instructions to do and see if she can google it? She needs to learn how to fetch information rather than learning "if i do what he says, i get cookie".

或者,如果你真的想变得怪异,为什么不建立一个虚拟环境,她不会做任何伤害,让她做任何事情,并在失败时重置它。给她5套指示,看看她是否可以google它?她需要学习如何获取信息,而不是学习“如果我做他说的话,我会得到cookie”。

There's better "game" scenarios to implement a learning curve then having you jump in and correct everything, give her keywords to search for and teach her to just try everything she sees on the google results.. It can't go wrong, after all.. It's just a computer..

有更好的“游戏”场景来实现学习曲线然后让你跳进并纠正一切,给她关键词搜索并教她只是尝试她在谷歌搜索结果上看到的一切......毕竟不会出错..这只是一台电脑......

A practical example:

  1. Create a virtual machine with qemu (guide is longer than it should, you only need a fraction to get started)
  2. 使用qemu创建一个虚拟机(指南比它应该更长,你只需要一小部分就可以开始)
  3. Install a base linux system on it
  4. 在其上安装基础Linux系统
  5. Take a snapshot

    拍一张快照

  6. Create a script that reverts to the last snapshot and boots the OS

    创建一个恢复到上一个​​快照并引导操作系统的脚本

  7. Give her (or him) a set of instructions such as "1: Install Python2.7, (keywords: archlinux python)"
  8. 给她(或他)一组说明,例如“1:安装Python2.7,(关键字:archlinux python)”

I did this for a couple of network labs, people could mess up all they wanted.. Didn't matter because the next time they rebooted (via the script) the machine was as good as new. Only thing you have to do, is invoke a new snapshot when she (or he) is in a good place in your tutorial.

我为几个网络实验室做了这个,人们可能会把他们想要的东西搞得一团糟。没关系,因为下次他们重新启动(通过脚本)机器就像新的一样好。只有你必须做的事情是,当她(或他)在你的教程中处于一个好位置时调用一个新的快照。