Can we call it as magic behavior?

import sys

import os

import shlex

import subprocess

full_path = “”

def isfile(file):


path_dirs = os.environ.get("PATH", "").split(":")

\# path_dirs = os.environ.get("PATH", "").split(";")



for directory in path_dirs:

    if not directory:

        continue



    full_path = os.path.join(directory, file)

    \# print(full_path)

    

    \# file exists

    if os.path.isfile(full_path):

        \# file is executable

        if os.access(full_path, os.X_OK):

            return full_path

return None


def main():


\# TODO: Uncomment the code below to pass the first stage

\# pass



shell = \['type','echo','exit'\]

while(1):

    sys.stdout.write("$ ")

    path = ""

    command = input("")




    if str(command) == "exit":

        break




    if(str(command.split(" ")\[0\]) == "type"):

        

        command = str(command\[5:\])

        path = isfile(command)

        

        if command in shell:

            print(f"{command} is a shell builtin")



        elif path:

            print(f"{command} is {path}")



        else:

            print(f"{command}: not found")






    elif(str(command.split(" ")\[0\]) == "echo"):

        \# print(f"{command}")

        command = str(command\[5:\])

        print(command)




    else:

        path = isfile(str(command.split(" ")\[0\]))

        

        if path:

            new_command = f"{path} {command.split(" ", 1)\[1\]}"

            \# new_command_list = new_command.split(" ")

            coms = shlex.split(new_command)              

            exit_code = subprocess.run(coms).returncode

            \# exit_code = 13214124

            \# print(args)

            \# length = len(command.split(" "))

            length = len(coms)



            if length >= 1:

                print(f"Program was passed {length} args (including program name).")

            for i in range(0,length):

                if i==0:

                    print(command)

                    print(f"Arg #{i} (program name): {command.split(" ")\[0\]}")

                else:

                    print(f"Arg #{i}: {command.split(" ")\[i\]}")

                

            print(f"Program Signature: {exit_code}")




        else:

            print(f"{command}: command not found")


if \_*name*\_ == “\_*main*\_”:


main()

The main problem is that the command which i’m taking the input is changing while printing after subprocess.run() command why this kind of behavior as i know the argv[0] only changes why a string is changing.

remote: [your-program] $ custom_exe_9061 David
remote: [your-program] Program was passed 2 args (including program name).
remote: [your-program] Arg #0 (program name): /tmp/pig/custom_exe_9061
remote: [tester::#IP1] ^ Line does not match expected value.
remote: [tester::#IP1] Expected: “Arg #0 (program name): custom_exe_9061”
remote: [tester::#IP1] Received: “Arg #0 (program name): /tmp/pig/custom_exe_9061”
remote: [your-program] Arg #1: David
remote: [your-program] Program Signature: 8555153816
remote: [your-program] Program was passed 2 args (including program name).
remote: [your-program] Arg #0: custom_exe_9061

The formatting seems a bit off for this site, and I am not entirely sure what you are asking.

But the tester is complaining that when your shell runs the program the program receives:
argv[] = {“/temp/pig/custom_exe_9061”, “David”}
but it is expecting
argv[] = {“custom_exe_9061”, “David”}

On Linux, which Bash uses, which this challenge is based on, typically a subprocess starts via the execve family of (C) functions, which takes a path (to the binary), a list argv (the arguments), and a list envp (the environment variables) the process should run with.

My guess is that when you use your isfile function it returns the full path to the program. In the broken version, the list you construct to pass to subprocess.run contains the full path as the first element, in the working version it contains just custom_exe_9061. You might want to look into the lower level subprocess.Popen since it has a executable kwarg separate from the args.

1 Like

Okk i’ve understand that we don’t need to print anything the tester code will automatically print it. We need to manage carefully the argv[0] in python.