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


Groups > comp.lang.python > #7783 > unrolled thread

Embedding Python in a shell script

Started byJason Friedman <jason@powerpull.net>
First post2011-06-17 00:57 +0000
Last post2011-06-17 15:43 -0500
Articles 4 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  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

#7783 — Embedding Python in a shell script

FromJason Friedman <jason@powerpull.net>
Date2011-06-17 00:57 +0000
SubjectEmbedding Python in a shell script
Message-ID<mailman.50.1308272248.1164.python-list@python.org>
$ 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?

[toc] | [next] | [standalone]


#7840

FromTimo Lindemann <tlindemann@arcor.de>
Date2011-06-17 12:47 -0500
Message-ID<cpydncH6HOHbDmbQnZ2dnUVZ8uadnZ2d@powerusenet.com>
In reply to#7783
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

[toc] | [prev] | [next] | [standalone]


#7853

FromHans Mulder <hansmu@xs4all.nl>
Date2011-06-17 22:15 +0200
Message-ID<4dfbb5fd$0$49178$e4fe514c@news.xs4all.nl>
In reply to#7840
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

[toc] | [prev] | [next] | [standalone]


#7855

FromTimo Lindemann <tlindemann@arcor.de>
Date2011-06-17 15:43 -0500
Message-ID<cpydncD6HOH8IWbQnZ2dnUVZ8uadnZ2d@powerusenet.com>
In reply to#7853
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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web