从bash运行的python multiline命令 [英] python multiline command running from bash

查看:65
本文介绍了从bash运行的python multiline命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试运行此

python -c "for i in range(10):\n  print i"

但是我得到一个错误:

File "<string>", line 1
for i in range(10):\n  print i
                             ^
SyntaxError: unexpected character after line continuation character

根据,我认为 bash 应该已经处理过(即换行符号)命令行参数,但是返回的错误却显示了相反的情况.我在哪里错了,为什么会这样?

According to this I assume that bash should have processed (namely, newline symbol) command line arguments but the returned error shows the opposite case. Where am I wrong, and why does this happen?

P.S.python-2.7

P.S. python-2.7

编辑

让我解释一下我的动机.这个代码示例绝对是愚蠢的.由于文档说" command 可以是一个或更多由换行符分隔的语句,并且具有与常规模块代码中一样大的前导空格",我对如何将这些提到的换行符正确地放入 command 感兴趣.这里提出的解决方案是:

Let me explain my motivation a bit. This code example is definitely pretty silly. Since the doc says that "command can be one or more statements separated by newlines, with significant leading whitespace as in normal module code", I was interested in how should I bring those mentioned newlines to the command properly. The proposed solutions here are:

  1. 使用; 区分循环内的多个命令.是的,可以,但是它仍然是一个单线,如果在循环后要运行 ,则不能使用它.; 不能替代换行符.

  1. Use ; to distinguish several commands inside the loop. Yes, that works but it still is a one-liner, I can not use it If I want to run some commands after the loop. ; is not a replacement for a newline.

键入 ^ M ,其中需要换行符.以我的观点,这更精确地实现了目标,但是不幸的是,这基本上破坏了从命令行运行python代码的整个想法,因为它需要交互模式.据我了解,这与按 Enter 键输入命令ant相同.因此,键入 python 并在其shell中工作没有区别.也就是说,我无法在bash脚本中编写此代码.还是可以吗?

Type ^M where newline is needed. This hits the goal more precisely but unfortunately, to my point of view, this basically ruins the whole idea of running a python code from the command line because it requires interactive mode. As I understand it's the same as entering a command ant hitting Enter key. So no difference to typing python and working in its shell. That said, I cannot write this in a bash script. Or may I?

这个问题可能真的应该分为两个问题:

Probably the question really should have been splitted into two ones:

  1. 重击转义:

用双引号('')引起来的字符将保留引号中所有字符的文字值,但'$','','\'除外,并且在启用历史记录扩展功能时,'!'.字符'$'和''在双引号中保留其特殊含义(请参见Shell Expansions).反斜杠仅在其后跟随以下字符之一时保留其特殊含义:'$',''','','\'或换行符.

Enclosing characters in double quotes (‘"’) preserves the literal value of all characters within the quotes, with the exception of ‘$’, ‘’, ‘\’, and, when history expansion is enabled, ‘!’. The characters ‘$’ and ‘’ retain their special meaning within double quotes (see Shell Expansions). The backslash retains its special meaning only when followed by one of the following characters: ‘$’, ‘`’, ‘"’, ‘\’, or newline.

这与所描述的情况如何对应?bash如何处理换行符?我发现将命令放入一元引号不会造成任何改变.

How does this correspond to the case described? How does bash handles newlines? I found that putting the command into unary quotes makes no change.

  1. 如何以非交互方式将换行符传递给python.(您可能会说-为什么不编写一个包含所有所需换行符的普通python文件-您是对的,但是我对文档中的确切含义感兴趣,因为它引用了换行符)

推荐答案

您实际上需要将 \ n 部分转换为实际的换行符.可以使用 $''语法完成:

You actually would need to transform the \n part into an actual newline. That can be done with the $'' syntax:

python -c $'for i in range(10):\n  print i'
0
1
2
3
4
5
6
7
8
9

您还可以通过 echo -e printf

$ python -c "$(echo -e "for i in range(10):\n  print i")"

您还可以在此处使用字符串:

You could also use a here string:

$ python <<< $(echo -e "for i in range(10):\n  print i")

请参见 3.1.2.4 ANSI-C引用Bash手册页以获取更多信息.

See section 3.1.2.4 ANSI-C Quoting of the Bash Manpage for more information.

这篇关于从bash运行的python multiline命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆