1

I encountered a problem in work. Here it is.

I have several scripts(mostly are shell scripts) to execute, and I want to write a python script to run them automatically. One of these shell scripts needs interactive input during it's execution. What troubled me is that I can't find a way to read its input prompt, so I can't decide what to enter to continue.

I simplified the problem to something like this:

There is a script named mediator.py, which run greeter.sh inside. The mediator takes greeter's input prompt and print it to the user, then gets user's input and pass it to greeter. The mediator needs to act exactly the same as the greeter from user's point of view.

Here is greeter.sh:

#! /bin/bash
echo "Please enter your name: " # <- I want 'mediator.py' to read this prompt and show it to me, and then get what I input, then pass my input to 'greeter.sh'
read name 
echo "Hello, " $name

I want to do this in the following order:

  1. The user (that's me) run mediator.py
  2. The mediator run greeter.sh inside
  3. The mediator get the input prompt of greeter, and output it on the screen.(At this time, the greeter is waiting for user's input. This is the main problem I stuck with)
  4. The user input a string (for example, 'Mike'), mediator get the string 'Mike' and transmit it to greeter
  5. The greeter get the name 'Mike', and print a greeting
  6. The mediator get the greeting, and output it on the screen.

I searched for some solution and determined to use Popen function in subprocess module with stdout of sub-process directed to PIPE, it's something like this:

sb = subprocess.Popen(['sh', 'greeter.sh'], stdout = subprocess.PIPE, stdin = stdout, stderr = stdout)

but I can't solve the main problem in step 3 above. Can anyone give me some advice for help? Thanks very much!

leo_z
  • 11
  • 3

1 Answers1

0

You make it much more complicated (and brittle) than it has to be. Instead of coding everything at the top-level and try to use subprocess or whatever to use your scripts as if they where functions, just write modules and functions and use them from your main script.

Here's an example with all contained in the script itself, but you can split it into distinct modules if you need to share some functions between different scripts

# main.py
def ask_name():
    return raw_input("Please enter your name: ")

def greet(name):
    return "Hello, {} name !\n".format(name)

def main():
    name = ask_name()
    print greet(name)

if __name__ == "__main__":
    main()
bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
  • Thanks for your answer first. I came up with this usage scenario beacuse I met it in work. I have several shell scripts to run, and I want to write a python script to run them automatically. One of these shell scripts needs user to enter some options during it's exexuting, so I need to get the prompt of the shell script and then determine which option to input for continuing execution. That's it. – leo_z Oct 25 '16 at 09:23
  • You should edit your question with this context then - that's a somehow different problem. Which I'll leave to a `subprocess` expert FWIW... – bruno desthuilliers Oct 25 '16 at 10:36
  • Thanks~ The question has been re-described now. – leo_z Oct 26 '16 at 02:41
  • I removed the previous `greeter.py` code and rewrote it to `greeter.sh` using shell script, so it became more correspondent to my usage scenario – leo_z Oct 26 '16 at 03:03