# \[Python\] Challenge:Shell - Stage #PN5

**URL:** <https://forum.codecrafters.io/t/python-challenge-shell-stage-pn5/3548>\
**Category:** Challenges\
**Tags:** challenge:shell\
**Created:** [December 19, 2024, 5:31pm UTC](https://forum.codecrafters.io/t/python-challenge-shell-stage-pn5/3548 "2024-12-19T17:31:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![SOUMYADEEPMAHATA](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/soumyadeepmahata/32/3582_2.png) [@SOUMYADEEPMAHATA](https://forum.codecrafters.io/u/SOUMYADEEPMAHATA)\
**Post date:** [December 19, 2024, 5:31pm UTC](https://forum.codecrafters.io/t/python-challenge-shell-stage-pn5/3548/1 "2024-12-19T17:31:33Z")

</div>

I’m stuck on Stage #PN5

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

Here are my logs:

```auto
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:

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

```

---

<div class="post-metadata">

**Author:** ![noam-no](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/noam-no/32/3558_2.png) [@noam-no](https://forum.codecrafters.io/u/noam-no)\
**Post date:** [December 19, 2024, 7:27pm UTC](https://forum.codecrafters.io/t/python-challenge-shell-stage-pn5/3548/2 "2024-12-19T19:27:27Z")

</div>

> [@SOUMYADEEPMAHATA](#):
>
> `PATH`

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.

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [December 21, 2024, 9:53pm UTC](https://forum.codecrafters.io/t/python-challenge-shell-stage-pn5/3548/3 "2024-12-21T21:53:01Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [December 28, 2024, 12:20am UTC](https://forum.codecrafters.io/t/python-challenge-shell-stage-pn5/3548/4 "2024-12-28T00:20:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/3X/7/0/700657133935c15703e22c7c8870394f6e6dc27d.svg) [@system](https://forum.codecrafters.io/u/system)\
**Post date:** [January 2, 2025, 12:21am UTC](https://forum.codecrafters.io/t/python-challenge-shell-stage-pn5/3548/5 "2025-01-02T00:21:12Z")

</div>

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