0

My requirement is, I am calling a shell script from my python script. When the python script is executed. It shows all the shell script content(not only the echo, even the secret keys which are read from param files in shell script) in the output. Here is my code

bashCommand = "sh -x {0} {1} {2} {3} {4} {5} {6} {7} ".format(Shell_script, local_directory,dest_file_path,Overwrite_Flag,file_name,client_id, tenant_id, encrypt_location)
process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE)
output = process.communicate()
logger.info(output.decode())

I just have to write the output of the shell into a log file and if possible I have to print the output of the shell script but not the entire content of the shell script.

Kindly help on this, Thanks

Reddy
  • 7
  • 2
  • Use subprocess.run() – C. Pappy Mar 24 '22 at 21:47
  • If you don't want to see the commands being run, why are you invoking `sh` with the `-x` option? That's exactly what that option does. – jasonharper Mar 24 '22 at 21:54
  • 1
    Why are you building a string only to split it again? `Popen(["sh", "-x", Shell_script, local_directory, dest_file, ...])`. – chepner Mar 24 '22 at 21:58
  • The tracing messages due to `-x` are written to `stderr`, not `stdout`. Since you're only redirecting `stdout` to the pipe, the trace messages get written to the terminal. – Barmar Mar 24 '22 at 22:11
  • @chepner your solution also worked for me. Thank you – Reddy Mar 24 '22 at 23:01

0 Answers0