在我的noob python cod中找不到命令“cat”的bug

2024-06-17 10:00:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用Python实现一个Shell,我有一个小bug,我已经花了好几个小时试图修复这个bug,但却无法解决这个问题。你知道吗

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

例如:

1)cat <test >toto运行得很好,现在toto有了内部测试。你知道吗

2)cat < test >toto运行完美,现在toto拥有了测试内部的内容。你知道吗

3)cat < test > toto运行得很好,现在toto有了内部测试,但是我在终端中得到了以下行(cat:toto:input file是output file)

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()

Tags: testsrcoutputifossyslineargs
1条回答
网友
1楼 · 发布于 2024-06-17 10:00:10

cat < test > toto中,问题是您没有删除testtoto,所以您得到['cat', 'test', 'toto'],然后将testtoto分配给stdinstdout,所以最后系统将其视为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()

相关问题 更多 >