bash python -c vs python -<< heredoc
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30702519/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
python -c vs python -<< heredoc
提问by Kashif
I am trying to run some piece of Python code in a Bash script, so i wanted to understand what is the difference between:
我试图在 Bash 脚本中运行一些 Python 代码,所以我想了解两者之间的区别:
#!/bin/bash
#your bash code
python -c "
#your py code
"
vs
对比
python - <<DOC
#your py code
DOC
I checked the web but couldn't compile the bits around the topic. Do you think one is better over the other? If you wanted to return a value from Python code block to your Bash script then is a heredoc the only way?
我查看了网络,但无法编译有关该主题的内容。你认为一个比另一个更好吗?如果您想将 Python 代码块中的值返回到您的 Bash 脚本,那么heredoc 是唯一的方法吗?
采纳答案by tripleee
The main flaw of using a here document is that the script's standard input will be the here document. So if you have a script which wants to process its standard input, python -c
is pretty much your only option.
使用 here 文档的主要缺陷是脚本的标准输入将是 here 文档。因此,如果您有一个想要处理其标准输入的脚本,这python -c
几乎是您唯一的选择。
On the other hand, using python -c '...'
ties up the single-quote for the shell's needs, so you can only use double-quoted strings in your Python script; using double-quotes instead to protect the script from the shell introduces additional problems (strings in double-quotes undergo various substitutions, whereas single-quoted strings are literal in the shell).
另一方面,使用python -c '...'
单引号来满足 shell 的需要,因此您只能在 Python 脚本中使用双引号字符串;使用双引号来保护脚本免受 shell 的影响会带来额外的问题(双引号中的字符串经过各种替换,而单引号字符串在 shell 中是文字)。
As an aside, notice that you probably want to single-quote the here-doc delimiter, too, otherwise the Python script is subject to similar substitutions.
顺便说一句,请注意,您可能也想单引号 here-doc 分隔符,否则 Python 脚本会受到类似的替换。
python - <<'____HERE'
print("""Look, we can have double quotes!""")
print('And single quotes! And `back ticks`!')
print("$(and what looks to the shell like process substitutions and $variables!)")
____HERE
As an alternative, escaping the delimiter works identically, if you prefer that (python - <<\____HERE
)
作为替代方案,转义分隔符的工作原理相同,如果您喜欢 ( python - <<\____HERE
)
回答by Ernesto Rapetti
If you prefer to use python -c '...'
without having to escape with the double-quotes you can first load the code in a bash variable using here-documents:
如果您更喜欢使用python -c '...'
而不必使用双引号转义,您可以首先使用 here-documents 将代码加载到 bash 变量中:
read -r -d '' CMD << '--END'
print ("'quoted'")
--END
python -c "$CMD"
The python code is loaded verbatim into the CMD variable and there's no need to escape double quotes.
python 代码被逐字加载到 CMD 变量中,不需要转义双引号。
回答by PEdroArthur
If you are using bash, you can avoid heredoc problems if you apply a little bit more of boilerplate:
如果您正在使用 bash,如果您应用更多样板文件,您可以避免 heredoc 问题:
python <(cat <<EoF
name = input()
print(f'hello, {name}!')
EoF
)
This will let you run your embedded Python script without you giving up the standard input. The overhead is mostly the same of using cmda | cmdb
. This technique is known as Process Substitution.
这将使您可以在不放弃标准输入的情况下运行嵌入式 Python 脚本。开销与使用cmda | cmdb
. 这种技术被称为进程替换。
If want to be able to somehow validate the script, I suggest that you dump it to a temporary file:
如果希望能够以某种方式验证脚本,我建议您将其转储到一个临时文件:
#!/bin/bash
temp_file=$(mktemp my_generated_python_script.XXXXXX.py)
cat > $temp_file <<EoF
# embedded python script
EoF
python3 $temp_file && rm $temp_file
This will keep the script if it fails to run.
如果脚本无法运行,这将保留脚本。