[英]How can I pass arguments to a python script being fed to the interpreter on stdin?
I have a bash script which exports two auxiliary python scripts, using heredoc with " ", and executes them from /tmp.我有一个 bash 脚本,它导出两个辅助 python 脚本,使用带有“”的heredoc,并从/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.它工作正常,但是当我在具有自动 /tmp 清理功能的服务器上部署此脚本时,它总是会在一段时间后删除导出的 python 脚本。
Moreover, I can not manually copy the needed python scripts to the server, only this script executes.此外,我无法手动将所需的 python 脚本复制到服务器,只能执行此脚本。
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.
我尝试引用和使用 xargs。
Is there any way to pass arguments to scripts executed like this?有没有办法将 arguments 传递给像这样执行的脚本?
Pass the Python interpreter -
as the filename you want it to read code from, when you want it to get code from stdin.当您希望它从标准输入获取代码时,传递 Python 解释器
-
作为您希望它从中读取代码的文件名。 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:将代码传递到 Python 解释器的另一种方法是在命令行上:
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: ...output 会略有不同:
['-c', 'argument one', 'argument two', 'argument three']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.