bash Python -c 开关

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15835316/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 05:07:24  来源:igfitidea点击:

Python -c switch

pythonbashshellquoting

提问by felix001

I currently have

我目前有

".".join(str(z) for z in [int(x, 16) for x in (re.sub(r'(.{2})(?!$)', r'.', "00112233")).split('.')])
'xx.xx.xx.xx'

which works but when i try to use it via the python -c switch it fails ?

哪个有效,但是当我尝试通过 python -c 开关使用它时失败了?

[root@monty ~]# python -c "import re ; ".".join(str(z) for z in [int(x, 16) for x in (re.sub(r'(.{2})(?!$)', r'.', "00112233")).split('.')])"
python -c "import re ; ".".join(str(z) for z in [int(x, 16) for x in (re.sub(r'(.{2})(?"import re ; ".".join(str(z) for z in [int(x, 16) for x in (re.sub(r'(.{2})(?python)', r'.', "00112233")).split('.')])")', r'.', "00112233")).split('.')])"
-bash: syntax error near unexpected token `str'

Any ideas ?

有任何想法吗 ?

回答by unwind

Looks like a quoting issue on the command line.

看起来像命令行上的引用问题。

Try wrapping the Python string in single quotes instead, and not using single quotes inside it.

尝试将 Python 字符串用单引号括起来,而不是在其中使用单引号。

You can also escape the quotes that collide with the shell's interpretation, using \".

您还可以使用\".

$ python -c 'import re;print ".".join(str(z) for z in [int(x, 16) for x in (re.sub(r"(.{2})(?!$)", r".", "00112233")).split(".")])'
0.17.34.51

Note: as your not running the python interpretor any more you need to explicitly print the results.

注意:由于您不再运行 python 解释器,因此您需要显式打印结果。

回答by Charles Duffy

Feed your script in on a quoted heredoc instead of using python -c, and you make the problem moot in its entirety; also, this lets you use newlines in your code, so it can be much more readable.

将您的脚本输入引用的 heredoc 而不是使用python -c,并且您使问题完全没有实际意义;此外,这允许您在代码中使用换行符,因此它可以更具可读性。

python - <<'EOF'
import re
print ".".join(str(z) for z in [int(x, 16)
                                for x in (re.sub(r'(.{2})(?!$)',
                                          r'.',
                                          "00112233")).split('.')])
EOF

Note that it's essential that you use <<'EOF'rather than <<EOFhere; the former prevents the shell from trying to expand the heredoc's contents.

请注意,您必须使用<<'EOF'而不是<<EOF在这里;前者防止外壳尝试扩展heredoc 的内容。



If you reallywant to use python -c, that's approach can still benefit from a heredoc used to capture the script into a variable:

如果您真的想使用python -c,这种方法仍然可以从用于将脚本捕获到变量中的 heredoc 中受益:

python_script=$(cat <<'EOF'
import re
print ".".join(str(z) for z in [int(x, 16)
                                for x in (re.sub(r'(.{2})(?!$)',
                                          r'.',
                                          "00112233")).split('.')])
EOF
)

python -c "$python_script"