Python forum for Python programmers

Re: Embedding Python in a shell script

Jun 17, 2011 1:05 am
Chris Angelico

On Fri, Jun 17, 2011 at 10:57 AM, Jason Friedman <jason@powerpull.net> wrote:
> 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:
>

#!/bin/bash
for i in 1 2 3 4; do
python -c "if True:
for j in range($i):
print j
"
done

Untested, but it's a hack I've used in a few places. The if tells
Python to expect an indent, and nobody cares if your first indent is
two miles and the one after that is only another two spaces.

ChrisA

Jun 17, 2011 2:25 am
Rusi
Re: Embedding Python in a shell script

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!


Jun 17, 2011 8:58 am
Mg
Re: Embedding Python in a shell script

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






Previous Thread: Embedding Python in a shell script
Next Thread: How do you copy files from one location to another?

Related Forum Topics
Embedding Python in a shell script
$ 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...
Changing current dir and executing a shell script
Hi,
I want to execute the following command line stuff from inside python.
$cd directory
$./executable

I tried the following but I get errors
import subprocess
subprocess.check_call('cd dir_name;./executable')

Due to filename path issues, I cannot try this...
Link errors embedding Python 3.2
I'm starting to feel incredibly stupid here. Hopefully someone can
point out a really obvious thing that I've missed, thus enabling me to
move forward!

Up until now, I've been embedding Python 2.6.6 in my C++ program, by
compiling with "-I/usr/include/python2.6 -lpython2.6", and all...
How to use shell return value like $? In python?
exp:
os.system('ls -al')
#I like to catch return value after this command. 0 or 1,2,3....
does python support to get "$?"?
then I can use something like:
If $?==0:
........
................
TIA
david




Question on Python 3 shell restarting
How may I get a fresh Python shell with Idle 3.2 ?
I have to run the same modules several times with all
variables cleared.

Thanks,

franck


Python as a default shell, replacement of bash, sh, cmd ?
Has it been considered to add shell features to python, such that it can be used as a default shell, as a replacement for bash, etc.

I'm sure everyone would agree that doing this would make the terminal very powerful.

What are your views on this?


About UNIX shell trap, any relative function in Python ?

Hi All

Machine : AIX 5.3
Python : 2.6.2

In UNIX have, trap to run defined CLEAN_UP function. When HUP INT
KILL STOP TERM will run CLEAN_UP function.

trap 'echo "\n\nProcessing Clean up"; CLEAN_UP; exit' HUP INT KILL
STOP TERM

Any relative function in Python ?

moonhkt


Python shell that saves history of typed in commands that willpersist between reboots
Hi,

Using Windows. Is there a python shell that has a history of typed in
commands?

I don't need output of commands just what I typed it. I need it to
save between sessions - something that no shell seems to do. If I
reboot there will still be a command history somewhere.

Like bash...
Embedding and extending on windows
A client wants to 'be lectured' on extending and embedding python on
windows.
I am familiar with this (or was until python2.3 or thereabouts) on
linux -- never done it on windows.

Can some kind soul point me to some link on the issues/pitfalls re
this?

I see three choices:
1. Us MS C...
Embedding a for inside an html template for substitution
Hello pythonistas!

I'am tryign to populate a table with dictionary keys and values:

Foe that iam using an html template and the questions is what i should write inside 'files.html' so then then the python script populate the table.

<table>
<tr><th>SuperHost -...