first
This commit is contained in:
35
main.py
Normal file
35
main.py
Normal file
@@ -0,0 +1,35 @@
|
||||
import os
|
||||
import re
|
||||
import signal
|
||||
import readline # 用于支持历史记录
|
||||
|
||||
# 标志位,用于处理信号
|
||||
interrupted = False
|
||||
|
||||
def signal_handler(signal, frame):
|
||||
global interrupted
|
||||
interrupted = True # 设置标志位
|
||||
|
||||
signal.signal(signal.SIGINT, signal_handler)
|
||||
|
||||
os.system('clear')
|
||||
|
||||
def shell():
|
||||
dir = os.getcwd()
|
||||
if dir == '/':
|
||||
CmdDir = '/'
|
||||
elif re.match(dir, "/home/*"): # 用户目录下的任意文件夹
|
||||
CmdDir = '~'
|
||||
else:
|
||||
CmdDir = dir.split('/')[-1]
|
||||
print(f'20250910553@何相龙 {CmdDir} % ', end='')
|
||||
cmd = input().strip() # 去除输入中的多余空白字符,包括换行符
|
||||
if cmd: # 如果输入不为空,保存到历史记录
|
||||
readline.add_history(cmd)
|
||||
os.system(cmd)
|
||||
|
||||
while True:
|
||||
if interrupted:
|
||||
print("\nSignal received. Type 'exit' to quit.")
|
||||
interrupted = False # 重置标志位
|
||||
shell()
|
||||
Reference in New Issue
Block a user