[Python] Challenge:Shell - Stage #PN5

I’m stuck on Stage #PN5

I’ve tried … (mention what you’ve tried so far).

Here are my logs:

remote: Debug = true
remote: 
remote: [tester::#MG5] Running tests for Stage #MG5 (The type builtin: executable files)
remote: [tester::#MG5] Running ./your_program.sh
remote: [your-program] $ type cat
remote: [your-program] cat is /bin/cat
remote: [tester::#MG5] ✓ Received expected response
remote: [your-program] $ type cp
remote: [your-program] cp is /bin/cp
remote: [tester::#MG5] ✓ Received expected response
remote: [your-program] $ type mkdir
remote: [your-program] mkdir is /bin/mkdir
remote: [tester::#MG5] ✓ Received expected response
remote: [your-program] $ type my_exe
remote: [your-program] my_exe is /tmp/blueberry/pear/pear/my_exe
remote: [tester::#MG5] ✓ Received expected response
remote: [your-program] $ type invalid_orange_command
remote: [your-program] invalid_orange_command: not found
remote: [tester::#MG5] ✓ Received expected response
remote: [your-program] $ type invalid_banana_command
remote: [your-program] invalid_banana_command: not found
remote: [tester::#MG5] ✓ Received expected response
remote: [your-program] $ 
remote: [tester::#MG5] Test passed.
remote: 
remote: [tester::#EZ5] Running tests for Stage #EZ5 (The type builtin: builtins)
remote: [tester::#EZ5] Running ./your_program.sh
remote: [your-program] $ type echo
remote: [your-program] echo is a shell builtin
remote: [tester::#EZ5] ✓ Received expected response
remote: [your-program] $ type exit
remote: [your-program] exit is a shell builtin
remote: [tester::#EZ5] ✓ Received expected response
remote: [your-program] $ type type
remote: [your-program] type is a shell builtin
remote: [tester::#EZ5] ✓ Received expected response
remote: [your-program] $ type invalid_banana_command
remote: [your-program] invalid_banana_command: not found
remote: [tester::#EZ5] ✓ Received expected response
remote: [your-program] $ type invalid_orange_command
remote: [your-program] invalid_orange_command: not found
remote: [tester::#EZ5] ✓ Received expected response
remote: [your-program] $ 
remote: [tester::#EZ5] Test passed.
remote: 
remote: [tester::#IZ3] Running tests for Stage #IZ3 (The echo builtin)
remote: [tester::#IZ3] Running ./your_program.sh
remote: [your-program] $ echo apple strawberry
remote: [your-program] apple strawberry
remote: [tester::#IZ3] ✓ Received expected response
remote: [your-program] $ echo grape pear mango
remote: [your-program] grape pear mango
remote: [tester::#IZ3] ✓ Received expected response
remote: [your-program] $ 
remote: [tester::#IZ3] Test passed.
remote: 
remote: [tester::#PN5] Running tests for Stage #PN5 (The exit builtin)
remote: [tester::#PN5] Running ./your_program.sh
remote: [your-program] $ invalid_banana_command
remote: [your-program] $ 
remote: [tester::#PN5] Output does not match expected value.
remote: [tester::#PN5] Expected: "invalid_banana_command: command not found"
remote: [tester::#PN5] Received: "$ "
remote: [tester::#PN5] Assertion failed.
remote: [tester::#PN5] Test failed

And here’s a snippet of my code:

import sys
import os 

def main():
    # Uncomment this block to pass the first stage
    valid_cmd = [ "ls", "abcd"]
    built_in_cmd = ["echo","exit", "type",]
    PATH = os.getenv("PATH")
    #sys.stdout.write("invalid_command: command not found")
    
    
    # Wait for user input
    while True:
        sys.stdout.write("$ ")
        command = input()
        cmd = command.split(" ")
        if command == "exit 0":
            break
        

        match cmd[0]:
            case "echo":
                sys.stdout.write(" ".join(cmd[1:]))
                sys.stdout.write("\n")

            case "type":
                dir1 = os.popen(f"which {cmd[1]}").read()
                #print(dir1)
                #print(f"{cmd[1]} not in built_in_cmd:", cmd[1] not in built_in_cmd )
                if (dir1 != "") and (cmd[1] not in built_in_cmd):
                    sys.stdout.write(f"{cmd[1]} is {dir1}")
                    #sys.stdout.write("\n")
                    built_in_cmd.append(cmd[1])
                    #continue



                elif cmd[1] not in (built_in_cmd + valid_cmd) :
                    sys.stdout.write(f"{cmd[1]}: not found")
                    #sys.stdout.write("\b"* len(f"{command}: command not found"))
                    sys.stdout.write("\n")
                elif cmd[1] in built_in_cmd:
                    sys.stdout.write(f"{cmd[1]} is a shell builtin")
                    sys.stdout.write("\n")
                    continue #sys.stdout.write("\n")

        
            case "exit":
                break


if __name__ == "__main__":
    main()

I don’t see any code to search for files in PATH, Could you paste your code entirely or share it on a public GitHub repo there is a built-in thing for that on codecrafters.

@SOUMYADEEPMAHATA Looks like you’ve got past this stage — do you happen to remember what was wrong? Would love to see if we can improve the tester / instructions.

Closing this thread due to inactivity. If you still need assistance, feel free to reopen or start a new discussion!

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.