Skip to content

Commit edd3d73

Browse files
authoredJul 9, 2019
Upload Bash.py
1 parent 7a8200a commit edd3d73

File tree

1 file changed

+164
-0
lines changed

1 file changed

+164
-0
lines changed
 

‎Bash.py

+164
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
#coding:utf-8
2+
#Bash on Python
3+
#内置环境pip(准确说是仿pip,是upip的命令行套壳)
4+
#内置软件包ls cd pwd touch fedit(自制简易文本覆盖修改器)
5+
6+
7+
from mpython import *
8+
9+
10+
#oled.fill(0)
11+
12+
#oled.DispChar("Hello, world!", 0, 0, 1)
13+
14+
#oled.show()
15+
16+
oled.fill(0)
17+
18+
import upip,os
19+
20+
welcome_string = '''
21+
Bash on mPython v1.5
22+
By FredTools
23+
'''
24+
user = 'root'
25+
hostname = "mPython"
26+
27+
class pip():
28+
def __init__(self):
29+
self.name = "pip"
30+
def use(self,args):
31+
32+
if args == []:
33+
return "This is a Command format of upip. "
34+
if args[0] == "install":
35+
if len(args) == 1:
36+
return "Usage: pip install [Package Name]"
37+
pkgs = args[1:]
38+
for pkg in pkgs:
39+
upip.install(pkg)
40+
return ""
41+
42+
class ls():
43+
def __init__(self):
44+
self.name = "ls"
45+
def use(self,args):
46+
output = ''
47+
if args == []:
48+
for thing in os.listdir("."):
49+
output += thing + "\n"
50+
return output
51+
52+
class pwd():
53+
def __init__(self):
54+
self.name = "pwd"
55+
def use(self,args):
56+
return os.getcwd()
57+
58+
59+
class cd():
60+
def __init__(self):
61+
self.name = "cd"
62+
def use(self,args):
63+
if args == []:
64+
return "This is a Command format of chdir. "
65+
else:
66+
os.chdir(' '.join(args))
67+
return ""
68+
69+
class touch():
70+
def __init__(self):
71+
self.name = "touch"
72+
def use(self,args):
73+
if args == []:
74+
return "This is a Command format of touch. "
75+
elif os.path.isfile(' '.join(args)):
76+
return ""
77+
elif os.path.isdir(' '.join(args)):
78+
return "touch: %s: Is dir" % (' '.join(args))
79+
else:
80+
f = open(' '.join(args),"w")
81+
f.close()
82+
83+
class fedit():
84+
def __init__(self):
85+
self.name = "fedit"
86+
def use(self,args=[]):
87+
if args == []:
88+
return "This is a Eazy-to-Use text editor by FredTools. \nUsage: fedit filename [End-Target]\nFor Example:\nfedit a.txt EOF"
89+
else:
90+
if len(args) == 1:
91+
args.append('EOF')
92+
doprint("-----Fedit-----")
93+
doprint("--Enter %s to exit.-" % (args[1]))
94+
f = open(args[0],'w')
95+
a=input()
96+
while a!=args[1]:
97+
f.write(a)
98+
f.close()
99+
return "Wrote. "
100+
101+
102+
103+
104+
105+
def command(cmd):
106+
try:
107+
l = cmd.split(' ')
108+
print(l)
109+
cmdname = l[0]
110+
del l[0]
111+
doprint(eval(cmdname + "()").use(l))
112+
except:
113+
doprint("Bash: %s: Bad command. " % (cmd.split(' ')[0]))
114+
115+
def main():
116+
doprint(welcome_string)
117+
while True:
118+
try:
119+
doprint("%s@%s: %s%s " % (user,hostname,os.getcwd(),"#" if (user=='root') else "$"))
120+
cmd = input()
121+
if cmd == "":
122+
continue
123+
doprint(cmd,False)
124+
command(cmd)
125+
doprint("")
126+
except KeyboardInterrupt:
127+
doprint("Exited. ")
128+
break
129+
except:
130+
doprint("Error! Please cheak. ")
131+
doprint("Exited. ")
132+
133+
OLEDl = ['','','','','']
134+
135+
def doprint(string,abool=True):
136+
oled.fill(0)
137+
if string == "":
138+
return
139+
if len(string)>1:
140+
a = 0
141+
global firststr
142+
firststr = ""
143+
global secondstr
144+
secondstr = ""
145+
for i in string:
146+
a+=1
147+
if a<=19:
148+
firststr += i
149+
else:
150+
secondstr += i
151+
if abool:
152+
print(string)
153+
OLEDl[0] = OLEDl[1]
154+
OLEDl[1] = OLEDl[2]
155+
OLEDl[2] = OLEDl[3]
156+
OLEDl[3] = firststr
157+
for i in range(4):
158+
oled.DispChar(OLEDl[i], 0, i*16, 1)
159+
oled.show()
160+
if secondstr!='':
161+
doprint(secondstr,False)
162+
163+
if __name__ == "__main__":
164+
main()

0 commit comments

Comments
 (0)
Please sign in to comment.