Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #72383

Re: Multi-line commands with 'python -c'

From Duncan Booth <duncan.booth@invalid.invalid>
Newsgroups comp.lang.python
Subject Re: Multi-line commands with 'python -c'
Date 2014-06-01 20:43 +0000
Message-ID <XnsA33ED0FD114E8duncanbooth@127.0.0.1> (permalink)
References (1 earlier) <XnsA33DE1B7E35C5duncanbooth@127.0.0.1> <CABicbJKjGCoEuZpAXfr_YUzFD=BYq_ezjkSf57k4vRjnb3MhvA@mail.gmail.com> <mailman.10500.1401486463.18130.python-list@python.org> <XnsA33EAA18C31A6duncanbooth@127.0.0.1> <mailman.10511.1401556283.18130.python-list@python.org>

Show all headers | View raw


Peter Otten <__peter__@web.de> wrote:

> Duncan Booth wrote:
> 
>> Chris Angelico <rosuav@gmail.com> wrote:
>> 
>>> On Sat, May 31, 2014 at 7:42 AM, Devin Jeanpierre
>>><jeanpierreda@gmail.com> 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 "<string>", 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']
> 
Thanks, I thought there must be a way to do that (and I should have 
remembered it). It nicely shows up the difference between the *nix 
shells that are all about processing the command line as a string and 
the Powershell way where it is all about objects (so a single value 
stays as a single argument).

-- 
Duncan Booth

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Multi-line commands with 'python -c' Chris Angelico <rosuav@gmail.com> - 2014-05-30 10:52 +1000
  Re: Multi-line commands with 'python -c' Rustom Mody <rustompmody@gmail.com> - 2014-05-29 23:04 -0700
    Re: Multi-line commands with 'python -c' Rustom Mody <rustompmody@gmail.com> - 2014-05-29 23:45 -0700
      Re: Multi-line commands with 'python -c' Rustom Mody <rustompmody@gmail.com> - 2014-05-29 23:54 -0700
        Re: Multi-line commands with 'python -c' Peter Otten <__peter__@web.de> - 2014-05-30 09:33 +0200
      Re: Multi-line commands with 'python -c' Terry Reedy <tjreedy@udel.edu> - 2014-05-30 11:29 -0400
    Re: Multi-line commands with 'python -c' Chris Angelico <rosuav@gmail.com> - 2014-05-30 17:20 +1000
      Re: Multi-line commands with 'python -c' Rustom Mody <rustompmody@gmail.com> - 2014-05-30 05:47 -0700
        Re: Multi-line commands with 'python -c' Chris Angelico <rosuav@gmail.com> - 2014-05-30 22:58 +1000
  Re: Multi-line commands with 'python -c' Duncan Booth <duncan.booth@invalid.invalid> - 2014-05-30 21:11 +0000
    Re: Multi-line commands with 'python -c' Devin Jeanpierre <jeanpierreda@gmail.com> - 2014-05-30 14:42 -0700
    Re: Multi-line commands with 'python -c' Chris Angelico <rosuav@gmail.com> - 2014-05-31 07:47 +1000
      Re: Multi-line commands with 'python -c' Duncan Booth <duncan.booth@invalid.invalid> - 2014-05-31 16:41 +0000
        Re: Multi-line commands with 'python -c' Peter Otten <__peter__@web.de> - 2014-05-31 19:11 +0200
          Re: Multi-line commands with 'python -c' Duncan Booth <duncan.booth@invalid.invalid> - 2014-06-01 20:43 +0000

csiph-web