简体   繁体   English

如何将 arguments 传递给 python 脚本,该脚本将被提供给标准输入上的解释器?

[英]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.

相关问题 如何将 arguments 传递给通过管道传输到标准输入上的 Python 解释器的脚本? - How do I pass arguments to a script piped to the Python interpreter on stdin? 将STDIN传递给一个脚本,该脚本本身是通过管道传输到Python解释器的? - Pipe STDIN to a script that is itself being piped to the Python interpreter? 如何传递 3 个参数,其中之一是 Python 中的数组? - How can I pass 3 arguments being one of them an array in Python? 如何将 arguments 传递给我 Tkinter 中的 python 脚本 - How can I pass arguments to my python script in Tkinter 将命令行参数传递给没有脚本文件的python解释器 - pass command line arguments to python interpreter without a script file 如何使用Python将stdin / stdout传递给Perl脚本 - How can I use Python to pipe stdin/stdout to Perl script 如何使用命令将参数传递给具有python入口点脚本的docker容器? - How can I pass arguments to a docker container with a python entry-point script using command? 如何从标准输入将字节码提供给 python 解释器? - How to feed bytecode to python interpreter from stdin? 如何在Python脚本中将用户输入字符串作为参数传递给函数? - How can I pass a user input string to a function as arguments in a python script? 如何将“ -i”传递给读取其他参数的python脚本 - how to pass “-i” to a python script that reads in other arguments
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM