Created
February 24, 2014 15:50
-
-
Save shangbo/9190906 to your computer and use it in GitHub Desktop.
homework about a simple shell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2.7 | |
# -*- coding:utf-8 -*- | |
import subprocess | |
import curses | |
import sys | |
import os | |
def init_once(): | |
""" | |
Init while starting | |
""" | |
curses.setupterm() | |
sys.stdout.write(curses.tigetstr("clear")) | |
sys.stdout.flush() | |
def init_command(): | |
""" | |
Init while inputting | |
""" | |
subprocess.call(['pwd']) | |
commands = raw_input(">>>") | |
return commands | |
def get_simcom(command): | |
command_splited = command.split(" ") | |
return command_splited | |
def run_command(command_list): | |
if command_list[0] == 'cd': | |
command = ' '.join(command_list[1:]) | |
os.chdir(command) | |
return 0 | |
else: | |
status = subprocess.call(command_list) | |
return status | |
def main(): | |
init_once() | |
while True: | |
try: | |
commands = init_command() | |
command_splited = get_simcom(commands) | |
try: | |
status = run_command(command_splited) | |
if status == 0: | |
pass | |
elif status == 2: | |
print "arguments error,please check your arguments!" | |
except OSError: | |
print sys.exc_value[1] | |
print "please check your command" | |
except KeyboardInterrupt: | |
print "\n" | |
pass | |
except EOFError: | |
print "\n" | |
ex = raw_input("Do you really want to exit ([Y]/N)?") | |
if ex == "Y" or ex == "y" or ex == "": | |
print "Bye~ >3<" | |
break | |
elif ex == "N" or ex == "n": | |
pass | |
else: | |
print repr(ex) | |
print "incorrect input!" | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment