35 lines
920 B
Python
35 lines
920 B
Python
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() |