Как запустить сценарий Bash в Python
import subprocess
print "start"
subprocess.call("sleep.sh")
print "end"
Cooperative Crab
import subprocess
print "start"
subprocess.call("sleep.sh")
print "end"
Just pass a HereDoc to python -.
From python help python -h:
- : program read from stdin
#!/bin/bash
MYSTRING="Do something in bash"
echo $MYSTRING
python - << EOF
myPyString = "Do something on python"
print myPyString
EOF
echo "Back to bash"
You can use heredoc if you want to keep the source of both bash and python scripts together. For example, say the following are the contents of a file called pyinbash.sh:
#!/bin/bash
echo "Executing a bash statement"
export bashvar=100
cat << EOF > pyscript.py
#!/usr/bin/python
import subprocess
print 'Hello python'
subprocess.call(["echo","$bashvar"])
EOF
chmod 755 pyscript.py
./pyscript.py
Now running the pyinbash.sh will yield:
$ chmod 755 pyinbash.sh
$ ./pyinbash.sh
Exe