繁体   English   中英

在我的 noob python 代码中找不到命令“cat”的错误

[英]Can't find the bug with the command "cat" in my noob python code

我正在使用 Python 实现一个 Shell,我遇到了一个小错误,我试图修复它已经几个小时了,但无法弄清楚问题所在。

当我运行我的程序并开始用“cat”命令输入一行时,一切正常,直到我在标准输出和文件名之间放了一个空格。

例如 :

1) cat <test >toto运行完美,现在 toto 有了内部测试。

2) cat < test >toto完美运行,现在toto有什么里面的测试了。

3) cat < test > toto运行完美,现在 toto 有了内部测试,但我在终端中得到以下行( cat: toto: 输入文件是输出文件)

4) cat <test > toto不起作用,我得到了相同的行( cat: toto ....)

当我在“>”和文件名之间加一个空格是怎么回事,为什么一直说输入文件与输出文件相同?! 这是我的代码

#!/usr/bin/python3

 import os
 import sys
 from shutil import copyfile
 def main():
    while True:
        sys.stdout.write("%% ")
        line = input()
        line2 = line[0:len(line)]
        line = ""
        dirs = os.listdir()
        d = ""
        for j in range(0, len(dirs)):
            d += dirs[j] + " "
        for i in range(0, len(line2)):
            if line2[i] == "*":
                line += d
            else:
                line += line2[i]
        args=list(filter(None,line.split(" ")))
        list_arg=[]  
        src=""
        dst=""
        sr_c=0
        ds_t=0  
        cmd=args[0]
        for temp in args:
            if temp=="<":
                sr_c=1
                src=args[args.index("<")+1]             
            elif temp[0]=="<":
                sr_c=1
                src+=temp[1:]
            elif temp==">":
                ds_t=1
                dst=args[args.index(">")+1]
            elif temp[0]==">":
                ds_t=1
                dst+=temp[1:]
            else:
                list_arg+=[temp]
        if ds_t:
            output=os.open(dst,os.O_CREAT |os.O_WRONLY |os.O_TRUNC)
        else: output=1

        if sr_c:
            inputt=os.open(src,os.O_RDONLY)
        else: inputt=0

        pid = os.fork() 
        if pid == 0:
            os.dup2(inputt,0)
            os.dup2(output,1) 
            os.execvp(cmd,list_arg)
        else:
            os.waitpid(pid,0)       
    sys.stdout.write("Bye!\n")
    sys.exit(0)

      main()

cat < test > toto问题是你没有删除testtoto所以你得到['cat', 'test', 'toto']并且你将testtoto分配给stdin , stdout所以最后系统将其视为cat test toto <test >toto这样你就可以从toto读取并写入toto

您可以使用data = iter(args)创建iterator ,您可以像以前一样将其for temp in data ,但您也可以使用next(data)从数据中获取下一个元素,并且for将跳过此元素 - 所以它不会将此元素添加到list_arg

import os
import sys
from shutil import copyfile

def main():
    while True:
        sys.stdout.write("%% ")
        line = input()

        dirs = os.listdir()
        d = " ".join(dirs)

        print('[DEBUG] d:', d)

        new_line = ""

        for char in line:
            if char == "*":
                new_line += d
            else:
                new_line += char

        args = list(filter(None, new_line.split(" ")))

        list_arg = []

        src = None
        dst = None

        cmd = args[0]

        data = iter(args)

        for temp in data:
            if temp == "<":
                src = next(data)
            elif temp[0] == "<":
                src = temp[1:]
            elif temp == ">":
                dst = next(data)
            elif temp[0] == ">":
                dst = temp[1:]
            else:
                list_arg.append(temp)

        if dst:
            output = os.open(dst, os.O_CREAT |os.O_WRONLY |os.O_TRUNC)
        else:
            output = 0

        if src:
            input_ = os.open(src, os.O_RDONLY)
        else:
            input_ = 1

        print('[DEBUG] src:', src)
        print('[DEBUG] dst:', dst)
        print('[DEBUG] arg:', list_arg)

        pid = os.fork()

        if pid == 0:
            os.dup2(input_, 0)
            os.dup2(output, 1)
            os.execvp(cmd, list_arg)
        else:
            os.waitpid(pid, 0)

    sys.stdout.write("Bye!\n")
    sys.exit(0)

main()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM