python -c option

M

Mike

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=.01234" 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
 
K

Klaus Alexander Seistrup

Mike said:
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?

#v+

python -c 'print 2+2'

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

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

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top