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


Groups > comp.lang.python > #7783

Embedding Python in a shell script

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <jsf80238@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.047
X-Spam-Evidence '*H*': 0.91; '*S*': 0.00; 'subject:Python': 0.06; 'unexpected': 0.07; 'python': 0.08; 'indent': 0.16; 'script.': 0.19; 'code': 0.24; 'expect': 0.25; 'sender:addr:gmail.com': 0.28; 'message-id:@mail.gmail.com': 0.28; 'behaves': 0.30; 'received:209.85.210.46': 0.30; 'received:mail- pz0-f46.google.com': 0.30; 'print': 0.31; 'done': 0.32; 'to:addr :python-list': 0.33; 'file': 0.34; '"",': 0.35; 'like:': 0.35; 'received:google.com': 0.37; 'something': 0.37; 'received:209.85': 0.37; 'reasons': 0.37; 'but': 0.38; 'realize': 0.38; "i'd": 0.39; 'received:209': 0.39; 'to:addr:python.org': 0.39; 'want,': 0.74; '#!/bin/bash': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:sender:date:x-google-sender-auth :message-id:subject:from:to:content-type; bh=uV1SIoP7+zh84gkx0G9baMn3WM4Z78r88rXd11HRIWg=; b=iJV42XmZJ7zPt0MaXChPYVj+42PJx39qf4hLtC3JboCXTXeL63oRIfUdg4nzrX3IMZ DmQtvAjP83vXf4c8cumP2FtEGtraoudm9NXLJSLpElRf+OXyeb8qKDhsVPeozUbv4ZtT wrihXscGIG7u08i0Dm5vCWUZ3ugUDBuegzv6U=
DomainKey-Signature a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:sender:date:x-google-sender-auth:message-id:subject :from:to:content-type; b=FtMXIp8jAbalp+AXc9+H2UKTvF354EpSXLovtD4FcU1MIfIy/UPsmCj7lUmBgGbgfK f3wilwvjusS5JDomg1lfYeV9Dk+NZJQzIqhZzyZlh99DFQBZgGZVRXoFD+INZX5TLRVF XYcOGmI3qdqIw8hsoa324tYILjZ9k5fSIK560=
MIME-Version 1.0
Sender jsf80238@gmail.com
Date Fri, 17 Jun 2011 00:57:25 +0000
X-Google-Sender-Auth eIzNGrUdTgkYWIKY8All1BKchUU
Subject Embedding Python in a shell script
From Jason Friedman <jason@powerpull.net>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.50.1308272248.1164.python-list@python.org> (permalink)
Lines 63
NNTP-Posting-Host 82.94.164.166
X-Trace 1308272248 news.xs4all.nl 49179 [::ffff:82.94.164.166]:36390
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:7783

Show key headers only | 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