Embedding Python in a shell script

Timo Lindemann tlindemann at arcor.de
Fri Jun 17 16:43:13 EDT 2011


On Fri, 17 Jun 2011 22:15:57 +0200, Hans Mulder said:

> On 17/06/11 19:47:50, Timo Lindemann wrote:
>> On Fri, 17 Jun 2011 00:57:25 +0000, Jason Friedman said:
>>
>>
>>
>>> but for various reasons I want a single script.  Any alternatives?
>>
>> you can use a here document like this:
> That does not solve the problem as stated.  The OP wants to call python
> inside a loop and he wants to indent things properly:

so, wrap it inside a bash function like this:

#! /bin/bash

call_python() {
/usr/bin/python2 << EOPYTHON
def hello():
	print("Hello, World $1");

if __name__ == "__main__":
	hello();

EOPYTHON
}

for i in 1 2 3 4 5 6
do
	call_python $i
done	

That way, the indentation on is nicer; passing parameters to the script 
inside the heredoc might be mean if the parameters are formed 
difficultly, like, causing syntax errors if it's something like '"' or 
somesuch. Probably beside the point though. 

> For some ideas that may work, read the earlier posts in this thread.

maybe my news server doesn't fetch the whole thread. I didnt see any 
replies, and still don't except yours. 

Nice evenin'
-T.



More information about the Python-list mailing list