bash 如果语句和命令行中的一行 python 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23300567/
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
If statements and one line python scripts from command line
提问by AffluentOwl
Why do I receive a syntax error for the following one liner python code?
为什么我收到以下一个 liner python 代码的语法错误?
python -c 'import re; if True: print "HELLO";'
File "<string>", line 1
import re; if True: print "HELLO";
^
SyntaxError: invalid syntax
The following code works just fine
下面的代码工作得很好
python -c 'if True: print "HELLO";'
How can I change my one line to execute my intended script on a single line from the command line?
如何更改我的一行以从命令行在一行上执行我想要的脚本?
回答by Rynant
One option to work around this limitation is to specify the command with the $'string'
format using the newline escape sequence \n
.
解决此限制的一种方法是$'string'
使用换行符转义序列的格式指定命令\n
。
python -c $'import re\nif True: print "HELLO";'
Note: this is supported by shells such as bash and zsh, but is not valid POSIX sh.
注意:这被 bash 和 zsh 等 shell 支持,但不是有效的 POSIX sh。
As mentioned by @slaadvak, there are some other workarounds here: Executing Python multi-line statements in the one-line command-line
正如@slaadvak 所提到的,这里还有一些其他的解决方法:在单行命令行中执行 Python 多行语句
回答by dano
The problem isn't with the import statement specifically, its that you have anything before a control flow statement. This won't work, either:
问题不在于导入语句,而是在控制流语句之前有任何内容。这也行不通:
dan@dan:~> python -c 'a = "1234" ; if True: print "hi"'
File "<string>", line 1
a = "1234" ; if True: print "hi"
^
SyntaxError: invalid syntax
According to the Python reference (https://docs.python.org/2/reference/compound_stmts.html), ';' can only be used to combine "simple statements" together. In this case you're combining the simple statement import re
, with if True:
. if True
isn't a simple statement, because it's introducing flow control, and is therefore a compound statement. At least that's how I interpret the documentation.
根据 Python 参考(https://docs.python.org/2/reference/compound_stmts.html),';' 只能用于将“简单语句”组合在一起。在这种情况下,您将简单的语句import re
, 与if True:
. if True
不是一个简单的语句,因为它引入了流程控制,因此是一个复合语句。至少我是这样解释文档的。
Here's the full text from the Python reference:
以下是 Python 参考的全文:
Compound statements consist of one or more ‘clauses.' A clause consists of a header and a ‘suite.' The clause headers of a particular compound statement are all at the same indentation level. Each clause header begins with a uniquely identifying keyword and ends with a colon. A suite is a group of statements controlled by a clause. A suite can be one or more semicolon-separated simple statements on the same line as the header, following the header's colon, or it can be one or more indented statements on subsequent lines
复合语句由一个或多个“子句”组成。子句由标题和“套件”组成。特定复合语句的子句标题都处于相同的缩进级别。每个子句标题都以唯一标识关键字开始,并以冒号结束。套件是由子句控制的一组语句。一个套件可以是一个或多个分号分隔的简单语句,与标题位于同一行,跟随标题的冒号,或者它可以是后续行的一个或多个缩进语句
compound_stmt ::= if_stmt
| while_stmt
| for_stmt
| try_stmt
| with_stmt
| funcdef
| classdef
| decorated
suite ::= stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement ::= stmt_list NEWLINE | compound_stmt
stmt_list ::= simple_stmt (";" simple_stmt)* [";"]
回答by chepner
You can embed newlines directly in the argument.
您可以直接在参数中嵌入换行符。
$ python -c 'import re
> if True:
> print "HELLO"
> '
回答by jfs
Why do I receive a syntax error for the following one liner python code?
为什么我收到以下一个 liner python 代码的语法错误?
Python grammarmight forbid small_stmt ';' compound_stmt
. -c
argument is probably is interpreted as file_input
:
Python 语法可能禁止small_stmt ';' compound_stmt
. -c
参数可能被解释为file_input
:
fileinput: (NEWLINE | stmt)* ENDMARKER
stmt: simple_stmt | compound_stmt
simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
small_stmt: import_stmt <in this case>
compound_stmt: if_stmt <in this case>
Note: there is a newline at the end of simple_stmt
. if_stmt
is not small_stmt
it can't follow another small_stmt
after ';'
. A newline is necessary to introduce compound_stmt
after small_stmt
.
注意:末尾有一个换行符simple_stmt
。if_stmt
是不是small_stmt
不能跟在另一个small_stmt
之后';'
。compound_stmt
在 之后需要引入一个换行符small_stmt
。
It is not an issue because bash
allows multiline arguments, just don't close the opening single quote until you done e.g.:
这不是问题,因为bash
允许多行参数,只是在完成之前不要关闭开头的单引号,例如:
$ python -c '
> import re
> if 1:
> print(1)
> '
1
Note: >
are printed by the shell itself here. It is not entered by hand.
注意:这里>
是由外壳本身打印的。它不是手动输入的。