0

I am trying to run scripts for python in MAC OS.

I was unable to run scripts that run Bash.sh scripts, any one here have an idea about how can I run bash.sh From python in Mac OS.

Thanks you for any help :)!

(This is the scripts:

Called: RunScript.sh, and I need to run it from Python Script.)

2 Answers 2

6

Does os.system not work?

import os
os.system("/path/to/script.sh")
1

Presumably you would use the subprocess module.

The questions become:

  • Do you intend to capture output from this script?
  • Do you intend to feed input into it?
  • Does it need to interact with the user via the terminal (that Python is using)?
  • Do need to capture and or deal with error messages or error codes (return values)?
3
  • Hey Jim, sorry for digging into this (very) old thread. But here I have the third use case in your list. Does os.system() open a terminal where it runs the shell/bash script? Does subprocess.Popen() opens a terminal window to show what the script is doing? because my script (shell/bash) has some sudo commands, and the users should be able to enter in their password when prompted. Any help is appreciated :) May 8, 2021 at 12:17
  • 1
    @PSSolanki, opening a terminal from within a Python script for password processing is almost certainly a terrible idea. sudo offers a -S (--stdin) option. So you could use the Python getpass module to prompt for and receive the password, and can you feed it into sudo's standard input stream (on a line by itself) the Popen() object's stdin attribute (which is a file like object, using subprocess.PIPE option.)
    – Jim Dennis
    Jun 19, 2021 at 12:16
  • Appreciate the input. I saw the -S flag through pycharm prompts. I'll try that :) Jun 19, 2021 at 19:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.