python -c option

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike

    python -c option

    I am very new to Python and read the docs section about having python run
    commands from the command line. I have been successful with:

    python -c "print 'Hello'"

    It prints Hello

    In the interpreter I can type 2+2 and it will return 4 but how do I do
    that from the command line? I have tried a few things without success.

    What I am really after is to do some command line processing where the
    output of grep is returning "prog=.0123 4" and I want to beautify it so it
    prints "12%"

    Maybe I should mess with bash but I am new to *nix in general and I wanted
    to learn Python.

    Thanks,
    Mike
  • Klaus Alexander Seistrup

    #2
    Re: python -c option

    Mike wrote:
    [color=blue]
    > I am very new to Python and read the docs section about having
    > python run commands from the command line. I have been successful
    > with:
    >
    > python -c "print 'Hello'"
    >
    > It prints Hello
    >
    > In the interpreter I can type 2+2 and it will return 4 but how do
    > I do that from the command line?[/color]

    #v+

    python -c 'print 2+2'

    #v-
    [color=blue]
    > What I am really after is to do some command line processing where
    > the output of grep is returning "prog=.0123 4" and I want to beautify
    > it so it prints "12%"[/color]

    Assuming the output is "prog=.1234 ", you _could_ do ugly things like

    #v+

    python -c 'import sys; exec(sys.stdin. readline()); print "%d%%" % (prog*100,)'

    #v-

    but why not write a small python script instead?


    // Klaus

    --[color=blue]
    ><> unselfish actions pay back better[/color]

    Comment

    Working...