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


Groups > comp.lang.python > #7783

Embedding Python in a shell script

Date 2011-06-17 00:57 +0000
Subject Embedding Python in a shell script
From Jason Friedman <jason@powerpull.net>
Newsgroups comp.lang.python
Message-ID <mailman.50.1308272248.1164.python-list@python.org> (permalink)

Show all headers | View raw


$ cat test.sh
#!/bin/bash
for i in 1 2 3 4; do
   python -c "
for j in range($i):
   print j
"
done

$ sh test.sh
0
0
1
0
1
2
0
1
2
3

The code behaves as I expect and want, but the de-denting of the
Python call is unattractive, especially unattractive the longer the
Python call becomes.  I'd prefer something like:

$ cat test.sh
#!/bin/bash
for i in 1 2 3 4; do
   python -c "
       for j in range($i):
           print j
   "
done

But that yields:

$ sh test.sh
 File "<string>", line 2
   for j in range(1):
   ^
IndentationError: unexpected indent
 File "<string>", line 2
   for j in range(2):
   ^
IndentationError: unexpected indent
 File "<string>", line 2
   for j in range(3):
   ^
IndentationError: unexpected indent
 File "<string>", line 2
   for j in range(4):
   ^
IndentationError: unexpected indent

I realize I can create a "call_me.py" and do:

$ cat test.sh
#!/bin/bash
for i in 1 2 3 4; do
   python call_me.py $i
done

but for various reasons I want a single script.  Any alternatives?

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


Thread

Embedding Python in a shell script Jason Friedman <jason@powerpull.net> - 2011-06-17 00:57 +0000
  Re: Embedding Python in a shell script Timo Lindemann <tlindemann@arcor.de> - 2011-06-17 12:47 -0500
    Re: Embedding Python in a shell script Hans Mulder <hansmu@xs4all.nl> - 2011-06-17 22:15 +0200
      Re: Embedding Python in a shell script Timo Lindemann <tlindemann@arcor.de> - 2011-06-17 15:43 -0500

csiph-web