From 66c0de4e53acfba5290cd426dbc4b9c42d5d4908 Mon Sep 17 00:00:00 2001 From: Tommmy Date: Wed, 2 Apr 2025 09:59:25 +0800 Subject: [PATCH] 11111 --- zsh.py | 3 +++ zsh2.py | 35 +++++++++++++++++++++++++++++++++++ zsh3.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 zsh.py create mode 100644 zsh2.py create mode 100644 zsh3.py diff --git a/zsh.py b/zsh.py new file mode 100644 index 0000000..f5b0f4d --- /dev/null +++ b/zsh.py @@ -0,0 +1,3 @@ +import os +while True: + os.system(input("20240915786@陈坤阳 ~ % ")) \ No newline at end of file diff --git a/zsh2.py b/zsh2.py new file mode 100644 index 0000000..5d5893e --- /dev/null +++ b/zsh2.py @@ -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() \ No newline at end of file diff --git a/zsh3.py b/zsh3.py new file mode 100644 index 0000000..1e1238e --- /dev/null +++ b/zsh3.py @@ -0,0 +1,42 @@ +import os +import shlex +import subprocess +import readline + +def completer(text, state): + commands = os.listdir('/bin') + os.listdir('/usr/bin') + os.listdir('/usr/local/bin') + matches = [cmd for cmd in commands if cmd.startswith(text)] + return matches[state] if state < len(matches) else None + +def pseudo_zsh(): + readline.parse_and_bind("tab: complete") + readline.set_completer(completer) + + while True: + try: + cmd = input("20240915786@\u9648\u5764\u9633 ~ % ") + + args = shlex.split(cmd) + if not args: + continue + + if args[0] == 'cd': + try: + os.chdir(args[1]) + except IndexError: + print("cd: missing argument") + except FileNotFoundError: + print(f"cd: no such file or directory: {args[1]}") + continue + + try: + subprocess.run(args) + except FileNotFoundError: + print(f"zsh: command not found: {args[0]}") + except KeyboardInterrupt: + print("\nZsh cannot be exited!") + except EOFError: + print("\nZsh cannot be exited!") + +if __name__ == "__main__": + pseudo_zsh() \ No newline at end of file