1

When using bash shell commands it would sometimes be usefull to pipe in python and write a short program and then maybe pipe that into something else. Im not finding a lot of documentation about writing python programs like this although it looks like the "-c" option is the option to use..but when writing even the simplest python program the compiler or should i say interpreter complains. See example below:

$ python -c "
import os

if os.path.isfile("test"):
    print "test is a file"
else:
    print "test is not a file"
"

When entering the last " the interpretor complains. This runs fine if i put it in a file but if i type it like that on the command line i get errors.

$ python -c "
import os

if os.path.isfile("test"):
    print "test is a file"
else:
    print "test is not a file"
"
Traceback (most recent call last):
  File "<string>", line 4, in <module>
NameError: name 'test' is not defined

I have no idea why the interpretor is complaining here. Does someone know why this isnt working ?

What im really after is something like this:

$ cat somefile | python -c "
import re

check = re.search(pattern, <file input>)
"

I dont know how to access the output of cat in this situation so i just wrote it literally.

4 Answers4

4

You are using double quotes inside double quotes which is ending the quoted string you are passing to python, in a place where you don't expect. Try replacing the outer quotes with single quotes, like I did here:

python -c '
import os

if os.path.isfile("test"):
    print "test is a file"
else:
    print "test is not a file"
'

If you are using single quotes to terminate the string you are passing to python, make sure to only use double quotes in your code. Additionally, if you can guarantee the availability of Bash as your shell, you can gain added awesome points by using heredoc format instead:

$ python <<EOF
> print "I can put python code here"
> EOF
I can put python code here
Community
  • 1
  • 1
ErlVolton
  • 6,714
  • 2
  • 15
  • 26
  • 2
    And when doing so, remember to only use double-quotes (`"`) around strings in your script, not single-quotes (`'`). Single-quotes will end the script abruptly and leave the shell to try to interpret the rest. – cxw Oct 09 '14 at 14:39
  • Or escape the single quotes with `\` – Barranka Oct 09 '14 at 14:44
  • Yes, escaping would work by makes me sad because things are not as readable then. – ErlVolton Oct 09 '14 at 14:45
1

Another solution is to escape your inner double quotes so bash doesn't parse them. Like this:

$ python -c "
import os

if os.path.isfile(\"test\"):
    print \"test is a file\"
else:
    print \"test is not a file\"
"
0

Either use single quotes to enclose your short program or, if you want to use double quotes to enclose it, escape the quotes with \.

Examples:

1. Escaping quotes

$ python -c "
print \"hello\"
for i in (1,2,3):
    print i
"

Output:

hello
1
2
3

2. With single quotes

$ python -c '
print "hello"
for i in (1,2,3):
    print i
'

Of course, if you use single quotes to enclose your program and you want to use single quotes inside your python code, you'll have to escape them with \ ;-).

The output is the same.

Barranka
  • 20,547
  • 13
  • 65
  • 83
0

You can use what is commonly called a "here document" (as in "use the document that is right here"). This avoids all quoting problems when using python -c "..." or python -c '...'

For example:

#!/bin/sh
python <<EOF
print "hello"
for i in (1,2,3):
    print i
EOF

The "here document" takes an arbitrary marker ("EOF" is a common choice, but it can be any string you know doesn't occur anywhere else in the data), and accepts all data up unto it finds a line that contains that marker.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685