Get shell script “read” value from python script

前端 未结 2 528
生来不讨喜
生来不讨喜 2021-01-23 10:13

Shell script: Hello.sh

#!/bin/bash
echo \"Enter your name: \"
read name
echo \"Hello $name\"

I want to invoke Hello.sh from within python and f

相关标签:
2条回答
  • 2021-01-23 10:55
    name = raw_input('Enter your name: ')
    print 'Hello ', name
    
    0 讨论(0)
  • 2021-01-23 10:56

    Not sure how to read your question.

    EDIT: Comment was added by the OP: invoke Hello.sh from within python and "fill" name noninteractively

    Which changes things, so here is a different answer for the new question:

    import subprocess
    cmd = '/home/user1/Hello.sh'
    proc = subprocess.Popen(cmd, stdin=subprocess.PIPE)
    proc.communicate("Fred Bloggs")
    

    I have used the full path to the script, it is safer.

    0 讨论(0)
提交回复
热议问题