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?
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?
+1 on the pipes. A more "shell-ish" approach would be:
import subprocess
the_name = 'the_name'
myproc = subprocess.Popen(['echo %s | bash Hello.sh' % the_name], stdin = subprocess.PIPE, stdout = subprocess.PIPE, shell=True)
out, err = myproc.communicate()
print out
shell=True
, I believe that it is preferred to pass a string, not a list.shell=True
: If args is a string, the string specifies the command to execute through the shell. If args is a sequence, the first item specifies the command string, and any additional items will be treated as additional arguments to the shell itself. So it doesn't really matter.