Reputation: 25
I have a bash script which exports two auxiliary python scripts, using heredoc with " ", and executes them from /tmp.
It works fine, but when i deployed this script on a server which has automatic /tmp cleanup, it will always delete the exported python scripts after a while.
Moreover, I can not manually copy the needed python scripts to the server, only this script executes.
an idea came to me
Right now I export the scripts like this
cat << "SCRIPT" > /tmp/script.py && chmod +x /tmp/script.py
And then execute them with an argument
I tried doing it like this
python << "SCRIPT"
The script runs, but it doesn't take arguemnts. I tried quoting and using xargs.
Is there any way to pass arguments to scripts executed like this?
Upvotes: 0
Views: 801
Reputation: 295363
Pass the Python interpreter -
as the filename you want it to read code from, when you want it to get code from stdin. Thus:
python - "argument one" "argument two" "argument three" <<'EOF'
import sys, pprint
pprint.pprint(sys.argv)
EOF
...properly emits:
['-', 'argument one', 'argument two', 'argument three']
Another way to pass code into a Python interpreter is on the command line:
pyscript=$(cat <<'EOF'
import sys, pprint
pprint.pprint(sys.argv)
EOF
)
python -c "$pyscript" "argument one" "argument two" "argument three"
...which will have slightly different output:
['-c', 'argument one', 'argument two', 'argument three']
Upvotes: 2