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