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


Groups > comp.lang.python > #7814

Re: Embedding Python in a shell script

From mg <marco.giusti@gmail.com>
Newsgroups comp.lang.python
Subject Re: Embedding Python in a shell script
Date 2011-06-17 08:58 +0000
Organization Aioe.org NNTP Server
Message-ID <itf4vt$ki1$1@speranza.aioe.org> (permalink)
References <BANLkTimB7ygPz6YhxSzJWuAf_yGaW-Hdww@mail.gmail.com> <mailman.54.1308272749.1164.python-list@python.org> <3206787a-4feb-4098-8ed3-9e0b86ee7b57@o10g2000prn.googlegroups.com>

Show all headers | View raw


rusi <rustompmody@gmail.com> wrote:
> On Jun 17, 6:05 am, Chris Angelico <ros...@gmail.com> wrote:
> 
>> > Python call becomes.  I'd prefer something like:
>>
>> #!/bin/bash
>> for i in 1 2 3 4; do
>>   python -c "if True:
> # comfortably indented python code
> 
> Thanks. Nice!

You can use bash here document feature, <<-, that strips heading tab
characters but not spaces.


	#!/bin/bash

	for i in 1 2 3 4; do
		python /dev/stdin <<-EOF
			for i in range($i):
			    print i # two tabs and four spaces
			EOF
	done

Or alternatively you can use a temporary file:


	#!/bin/bash

	TEMPFILE=$(mktemp)
	trap 'rm -f $TEMPFILE' TERM INT

	cat > $TEMPFILE <<EOF
	import sys

	for i in range(int(sys.argv[1])):
	    print i
	EOF

	for i in 1 2 3 4; do
		python $TEMPFILE $i
	done

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


Thread

Re: Embedding Python in a shell script Chris Angelico <rosuav@gmail.com> - 2011-06-17 11:05 +1000
  Re: Embedding Python in a shell script rusi <rustompmody@gmail.com> - 2011-06-16 19:25 -0700
    Re: Embedding Python in a shell script mg <marco.giusti@gmail.com> - 2011-06-17 08:58 +0000

csiph-web