2

I'm running a script in bash using python 2.5.2

The script dumps some reports on the shell.

How can I freeze the shell, and prevent it from being interrupted by enter key (or any other keys) while running the script?

Kobayashi
  • 2,402
  • 3
  • 16
  • 25

2 Answers2

4

If I understand you correctly, you want to disable the echo of the users keyboard input?

You can disable the keyboard echo with the following command:

stty -echo

and enable it again with:

stty echo

(however you wan't be able to see what you are typing)

J. P. Petersen
  • 4,871
  • 4
  • 33
  • 33
0

The more sophisticated way of acheiving this is to run it in a thread, and writing the output to a file.

This how you can achieve this in a thread.

import time
from threading import Thread

def noInterrupt():
    f = open('/path/to/filename.txt','w+')
    for i in xrange(4):
        f.write(i)
        time.sleep(1)
    f.close()

a = Thread(target=noInterrupt)
a.start()
a.join()
print "done"

If the particular use case require you to obtain a lock the code is below refrenced from here :

class KeyboardLocker:

    def __init__(self, serio=0):
        self._on = False
        self.serio = serio

    def on(self):
        return self._on

    def write_value(self,path, value):
        with open(path, "a") as f:
            f.write(value)

    def toggle(self):
        if self.on():
            self.turn_off()
        else:
            self.turn_on()

    def description(self):
        path = '/sys/devices/platform/i8042/serio%d/description' % (self.serio,)
        with open(path, "r") as f:
            description = f.read()
        return description

    def turn_on(self):
        try:
            self.write_value('/sys/devices/platform/i8042/serio%d/bind_mode' % (self.serio,),
                            'auto')
        except IOError, e:
            self._on = False
            raise
        else:
            self._on = True
        return self.on()

    def turn_off(self):
        try:
            self.write_value('/sys/devices/platform/i8042/serio%d/bind_mode' % (self.serio,),
                            'manual')
            self.write_value('/sys/devices/platform/i8042/serio%d/drvctl' % (self.serio,),
                            'psmouse')
        except IOError, e:
            self._on = True
            raise
        else:
            self._on = False
        return self.on()

if __name__ == "__main__":
    kl = KeyboardLocker(serio=0)

    device = kl.description()
    print "We got a lock on", device

    proceed = raw_input("Do you want to proceed? (y/n)").lower().startswith("y")
    import sys
    if not proceed: sys.exit(1)

    kl.turn_off()

    import time
    wait = 5
    print "Sleeping few seconds...", wait
    time.sleep(wait)
    print "Voila!"

    kl.turn_on()

    raw_input("Does it work now?")
Community
  • 1
  • 1
harshil9968
  • 3,254
  • 1
  • 16
  • 26