File tree 1 file changed +121
-0
lines changed
1 file changed +121
-0
lines changed Original file line number Diff line number Diff line change
1
+ #coding:utf-8
2
+ #Bash on Python
3
+ #内置环境pip(准确说是仿pip,是upip的命令行套壳)
4
+ #内置软件包ls cd pwd touch fedit(自制简易文本覆盖修改器)
5
+
6
+ import upip ,os
7
+
8
+ welcome_string = '''
9
+ Bash on mPython v1.4
10
+ By FredTools
11
+ '''
12
+ user = 'root'
13
+ hostname = "mPython"
14
+
15
+ class pip ():
16
+ def __init__ (self ):
17
+ self .name = "pip"
18
+ def use (self ,args ):
19
+
20
+ if args == []:
21
+ return "This is a Command format of upip. "
22
+ if args [0 ] == "install" :
23
+ if len (args ) == 1 :
24
+ return "Usage: pip install [Package Name]"
25
+ pkgs = args [1 :]
26
+ for pkg in pkgs :
27
+ upip .install (pkg )
28
+ return ""
29
+
30
+ class ls ():
31
+ def __init__ (self ):
32
+ self .name = "ls"
33
+ def use (self ,args ):
34
+ output = ''
35
+ if args == []:
36
+ for thing in os .listdir ("." ):
37
+ output += thing + "\n "
38
+ return output
39
+
40
+ class pwd ():
41
+ def __init__ (self ):
42
+ self .name = "pwd"
43
+ def use (self ,args ):
44
+ return os .getcwd ()
45
+
46
+
47
+ class cd ():
48
+ def __init__ (self ):
49
+ self .name = "cd"
50
+ def use (self ,args ):
51
+ if args == []:
52
+ return "This is a Command format of chdir. "
53
+ else :
54
+ os .chdir (' ' .join (args ))
55
+ return ""
56
+
57
+ class touch ():
58
+ def __init__ (self ):
59
+ self .name = "touch"
60
+ def use (self ,args ):
61
+ if args == []:
62
+ return "This is a Command format of touch. "
63
+ elif os .path .isfile (' ' .join (args )):
64
+ return ""
65
+ elif os .path .isdir (' ' .join (args )):
66
+ return "touch: %s: Is dir" % (' ' .join (args ))
67
+ else :
68
+ f = open (' ' .join (args ),"w" )
69
+ f .close ()
70
+
71
+ class fedit ():
72
+ def __init__ (self ):
73
+ self .name = "fedit"
74
+ def use (self ,args = []):
75
+ if args == []:
76
+ return "This is a Eazy-to-Use text editor by FredTools. \n Usage: fedit filename [End-Target]\n For Example:\n fedit a.txt EOF"
77
+ else :
78
+ if len (args ) == 1 :
79
+ args .append ('EOF' )
80
+ print ("-----Fedit-----" )
81
+ print ("--Enter %s to exit.-" % (args [1 ]))
82
+ f = open (args [0 ],'w' )
83
+ a = input ()
84
+ while a != args [1 ]:
85
+ f .write (a )
86
+ f .close ()
87
+ return "Wrote. "
88
+
89
+
90
+
91
+
92
+
93
+ def command (cmd ):
94
+ try :
95
+ l = cmd .split (' ' )
96
+ print (l )
97
+ cmdname = l [0 ]
98
+ del l [0 ]
99
+ print (eval (cmdname + "()" ).use (l ))
100
+ except :
101
+ print ("Bash: %s: Bad command. " % (cmd .split (' ' )[0 ]))
102
+
103
+ def main ():
104
+ print (welcome_string )
105
+ while True :
106
+ try :
107
+ cmd = input ("%s@%s: %s%s " % (user ,hostname ,os .getcwd (),"#" if (user == 'root' ) else "$" ))
108
+ if cmd == "" :
109
+ continue
110
+ command (cmd )
111
+ print ("" )
112
+ except KeyboardInterrupt :
113
+ print ("Exited. " )
114
+ break
115
+ except :
116
+ print ("Error! Please cheak. " )
117
+ print ("Exited. " )
118
+
119
+
120
+ if __name__ == "__main__" :
121
+ main ()
You can’t perform that action at this time.
0 commit comments