简体   繁体   中英

How to insert a python program into a bash script?

I have a small python program that parses a text file and writes the output to another file. Currently, I am writing a bash script to call this program several times, it looks something like:

for i in $(seq 1 100); do

          python /home/Documents/myProgram.py myTextFile$i

done
#!/bin/bash

python - 1 2 3 << 'EOF'
import sys

print 'Argument List:', str(sys.argv)
EOF

I think you should be able to put:

python  << END
[Python code here]
END

for simple scripts you can also run python with the -c option. For example

python -c "x=1; print x; print 'great program eh'"

Pardon the thread necromancy, but here's a technique that is missing that may be useful to someone.

#!/bin/bash
""":" # Hide bash from python
for i in $(seq 1 100); do
  python3 $(readlink -f "$0") myTextFile$i
done
exit
"""
# Python script starts here
import sys
print('Argument List: ', str(sys.argv))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM