“python dans le script bash” Réponses codées

Comment exécuter le script bash en python

import subprocess
print "start"
subprocess.call("sleep.sh")
print "end"
Cooperative Crab

python dans le script bash

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"
DreamCoder

python dans le script 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
DreamCoder

Réponses similaires à “python dans le script bash”

Questions similaires à “python dans le script bash”

Plus de réponses similaires à “python dans le script bash” dans Shell/Bash

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code