linux下用python实现文本menu
linux用shell实现menu比较简单,今天用python实现了一个,因为python没有switch语法,所以使用了dict来替代,另外调用了linux的clear来清屏,本例只是说明一下简单的实现方法,菜单的命令或者语句可以自由发挥 呵呵。
具体代码如下:
#!/usr/bin/evn python
# -*- coding: utf-8 -*-
#Author: 2hei#2hei.net
#Date: 2009-11-17 18:24
import os,sys
running = True
menu = “””
menu
——————————
1: Disk info
2: Mem info
3: Network info
4: Sys load info
5: Process info
h: Help
q: Quit
——————————
“””
menu_dict={
“h”: “echo help ^_^”,
“1”: “df -h”,
“2”: “free -m”,
“3”: “netstat -lnt”,
“4”: “uptime”,
“5”: “ps x”
}
def commands(args):
cmd = menu_dict.get(args)
return cmd
if __name__ == “__main__”:
os.system(‘clear’)
print menu
while running:
cmd = raw_input(“Input your commond :\n”)
if cmd != ‘q’:
os.system(‘clear’)
try:
print menu
if commands(cmd) != None:
fo = os.popen(commands(cmd))
print fo.read()
else:
print “Input is Wrong!\n”
except Exception,e:
print menu
print e
else:
print ‘we will exit the menu\n’
sys.exit()
本文固定链接: https://www.2hei.net/2009/11/17/linux-python-txt-menu/ | 2hei.net
最活跃的读者