Embedding Python in a shell script
Hans Mulder
hansmu at xs4all.nl
Fri Jun 17 16:15:57 EDT 2011
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:
>
>
> #! /bin/bash
>
> /usr/bin/python2<< EOPYTHON
> def hello():
> print("Hello, World");
>
> if __name__ == "__main__":
> hello();
>
> EOPYTHON
That does not solve the problem as stated. The OP wants to call python
inside a loop and he wants to indent things properly:
#!/bin/bash
for i in 1 2 3 4 5
do
python << EOPYTHON
def hello():
print("Hello, World");
if __name__ == "__main__":
hello();
EOPYTHON
done
That doesn't work, because the EOPYTHON token is indented.
If you put the EOPYTHON token flush left, it still doesn't work, because
Python does not accept indentation on line 1:
File "<stdin>", line 1
def hello():
^
IndentationError: unexpected indent
For some ideas that may work, read the earlier posts in this thread.
-- HansM
More information about the Python-list
mailing list