Get shell script “read” value from python script

2019-03-02 11:23发布

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 fill variable "name" non-interactively. How can it be done?

2条回答
啃猪蹄的小仙女
2楼-- · 2019-03-02 11:48

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.

查看更多
甜甜的少女心
3楼-- · 2019-03-02 12:01
name = raw_input('Enter your name: ')
print 'Hello ', name
查看更多
登录 后发表回答