长程序使用python -c开关 [英] Long programs using python -c switch
问题描述
我想使用python的东西,我一直在使用bash。可以对长程序使用-c开关,例如。一个for循环有两个语句?这将允许我直接从命令行使用python,就像bash或php。
I would like to use python for things I've been doing using bash. Is it possible to use the -c switch for long programs, e.g. a for loop with two statements? This would let me use python directly from command line, just like bash or php.
谢谢。
:不知道我错过了,只是做一个python -c',然后按enter键,我想做的。我试过很多变化,一个使用\但是没有工作,所以我问的问题。
eg
Don't know how I missed it, simply doing a python -c ' and then pressing enter does what I've wanted to do. I'd tried a lot of variations, and one using a \ but that didn't work, so I asked the question. e.g.
$python -c '
>print "x"
>for i in range(3):
> print "y" '
做我想做的事,尽管Rod的回答看起来也不错。
does what I wanted to do, though Rod's answer looks good too.
推荐答案
如果你的底层shell是bash没有问题,因为如果打开'
(报价)尚未关闭 - 例如:
No problem if your underlying shell is bash, since you can continue an argument across multiple lines if an opened '
(quote) is not yet closed -- e.g.:
$ python -c'for x in range(3):
> if x!=1:
> print x'
0
2
$
c $ c>> 是bash的默认 PS2
,多行延续提示,区别于 $
,AKA PS1
是正常的开始输入命令提示。
The >
is bash's default PS2
, the "multi-line continuation prompt", as distinguished from $
, AKA PS1
, the normal "start entering a command" prompt.
你不能使用这样的多行连续,多个嵌套的块语句(如 if
在一个循环内)否则会有问题。
If you can't use such multi-line continuation, multiple nested block statements (such as an if
within a loop) could otherwise be problematic.
这篇关于长程序使用python -c开关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!