栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python实现的bash shell套壳

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python实现的bash shell套壳

在某些受管制的环境下,若用户在Shell终端一段时间后无操作则Shell会自动退出。针对此问题实现python套壳shell,作为区别于bash的进程运行。代码如下:

#!/usr/local/bin/python3
#应将#!后替换为实际python3路径
import readline
import os


def execCmd(cmd):  
    r = os.popen(cmd)  
    text = r.read()  
    r.close()  
    return text  
   
while True: 
    pwd=os.getcwd()
    command=input(f"[pyshell:{pwd}]")
    if(command.strip()=="quit"):
        break
    if(command.startswith("cd")):
        command_parts=command.split()
        if(command_parts[0]=="cd" and len(command_parts)==2):
            try:
                os.chdir(command_parts[1])
            except Exception as error:
                print(error)
        else:
            os.system(command)
    else:
        os.system(command)


缺点:命令自动补全功能,ls罗列的文件名无法按照分类显示不同样式。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/953614.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号