Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '"this': 0.03; '(at': 0.04; 'syntax': 0.04; 'model,': 0.05; 'python3': 0.07; 'bash': 0.09; 'booth': 0.09; 'difference,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'root,': 0.09; 'statements': 0.09; 'python': 0.11; 'wrote': 0.14; 'windows': 0.15; "'for',": 0.16; "'if',": 0.16; "'in',": 0.16; 'argument.': 0.16; 'bash,': 0.16; 'cleaner': 0.16; 'files)': 0.16; 'it;': 0.16; 'line)': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'syntaxerror:': 0.16; 'subject:python': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'bit': 0.19; 'file,': 0.19; 'command': 0.22; '>>>': 0.22; 'example': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; '31,': 0.24; 'simpler': 0.24; "i've": 0.25; 'script': 0.25; 'this:': 0.26; 'least': 0.26; 'primary': 0.26; 'header:X-Complaints-To:1': 0.27; 'point': 0.28; 'chris': 0.29; 'am,': 0.29; 'unix': 0.29; 'related': 0.29; 'originally': 0.30; "skip:' 10": 0.31; '"",': 0.31; 'minor': 0.31; 'file': 0.32; 'running': 0.33; 'style': 0.33; 'actual': 0.34; 'subject:with': 0.35; 'point.': 0.35; 'but': 0.35; 'useful': 0.36; 'skip:. 20': 0.38; 'to:addr:python-list': 0.38; 'files': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'commands': 0.60; 'new': 0.61; 'more': 0.64; 'series': 0.66; 'direct': 0.67; 'importantly,': 0.68; 'invalid': 0.68; 'lack': 0.78; 'repeat.': 0.84; 'subject:commands': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Multi-line commands with 'python -c' Date: Sat, 31 May 2014 19:11:03 +0200 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bdb5da.dip0.t-ipconnect.de User-Agent: KNode/4.11.5 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 69 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1401556283 news.xs4all.nl 2837 [2001:888:2000:d::a6]:56421 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:72343 Duncan Booth wrote: > Chris Angelico wrote: > >> On Sat, May 31, 2014 at 7:42 AM, Devin Jeanpierre >> wrote: >>> In unix shells you can literally use a new line. Or is that only > bash? >> >> You can in bash, I know, but it's fiddly to type it; and more >> importantly, it's not a good point in the "this is cleaner than a >> series of pipes" argument. My primary recommendation, of course, was a >> three-line script saved as an actual file, but for a more direct >> parallel to the pipe-it-three-ways model, I wanted to use -c. > > and you also wrote originally that it's fiddly to edit. I think that > Windows Powershell has (at least in the current ISE command line) got > the editing a bit better. It's a minor difference though and it has > taken Microsoft about 30 years to get to that point. > > What may be a larger difference, or may just be my lack of Linux-foo, is > this: > > PS C:\python33> $script = @" > import os > for root, dirs, files in os.walk("."): > if len(dirs + files) == 1: print(root) > "@ > > PS C:\python33> python -c $script > .\Doc > .\Lib\concurrent\__pycache__ > .\Lib\curses\__pycache__ > ... > > which is a style I've found useful for example when running a group of > related timeit.py commands as I can put things like multi-line setup > statements in a variable and then have a simpler command to repeat. > > But bash as far as I can won't let me do that: > > $ script='import os > for root, dirs, files in os.walk("."): > if len(dirs + files) == 1: print(root) > ' > $ python -c $script > File "", line 1 > import > ^ > SyntaxError: invalid syntax $ script='import os > for root, dirs, files in os.walk("."): > if len(dirs + files) == 1: > print(root) > ' $ python3 -c "$script" . ./heureka $ python3 -c 'import sys; print(sys.argv)' $script ['-c', 'import', 'os', 'for', 'root,', 'dirs,', 'files', 'in', 'os.walk("."):', 'if', 'len(dirs', '+', 'files)', '==', '1:', 'print(root)'] $ python3 -c 'import sys; print(sys.argv)' "$script" ['-c', 'import os\nfor root, dirs, files in os.walk("."):\n if len(dirs + files) == 1:\n print(root)\n']