Mar-21-2018, 09:45 AM
You can do this with here-doc.
#!/bin/bash echo "Hello World from bash" python <<EOF from __future__ import print_function import sys print(sys.version) print("Using a variable from shell: $PWD") EOF python <<'EOF' from __future__ import print_function print('Without variables: $PWD') EOF echo "Preparing a list in shell" li=(1 2 3 4 5 6) echo ${li[@]} python <<EOF from __future__ import print_function print('Using list from shell in Pyhon') numbers = list(map(int, "${li[@]}".split())) print(numbers) EOF if [ -x '/usr/bin/python3' ]; then /usr/bin/python3 <<EOF import sys print('Python 3 exists on this system....') print(sys.version) EOF fi if [ -x '/usr/bin/python3' ]; then PYTHON='/usr/bin/python3' elif [ -x '/usr/bin/python2' ]; then PYTHON='/usr/bin/python2' elif [ -x '/usr/bin/python' ]; then PYTHON='/usr/bin/python' fi $PYTHON <<'EOF' from __future__ import print_function import sys print('Using detected Python interpreter...') print(sys.version) EOF
Output:Hello World from bash
2.7.14 (default, Sep 23 2017, 22:06:14)
[GCC 7.2.0]
Using a variable from shell: /home/andre
Without variables: $PWD
Preparing a list in shell
1 2 3 4 5 6
Using list from shell in Pyhon
[1, 2, 3, 4, 5, 6]
Python 3 exists on this system....
3.6.3 (default, Oct 3 2017, 21:45:48)
[GCC 7.2.0]
Using detected Python interpreter...
3.6.3 (default, Oct 3 2017, 21:45:48)
[GCC 7.2.0]
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!