Jun 17, 2011 1:05 am
Chris AngelicoOn 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:
>
> 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
RusiOn 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:
Thanks. Nice!
Jun 17, 2011 8:58 am
Mgrusi <rustompmody@gmail.com> wrote:
> On Jun 17, 6:05?am, Chris Angelico <ros...@gmail.com> wrote:
>
>> #!/bin/bash
>> for i in 1 2 3 4; do
>> ? python -c "if True:
> # comfortably indented python code
>
> Thanks. Nice!
>
>> > Python call becomes. ?I'd prefer something like:
>>>> #!/bin/bash
>> for i in 1 2 3 4; do
>> ? python -c "if True:
>
> 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
- Changing current dir and executing a shell script
- Link errors embedding Python 3.2
- How to use shell return value like $? In python?
- Question on Python 3 shell restarting
- Python as a default shell, replacement of bash, sh, cmd ?
- About UNIX shell trap, any relative function in Python ?
- Python shell that saves history of typed in commands that willpersist between reboots
- Embedding and extending on windows
- Embedding a for inside an html template for substitution