How to insert a python program into a bash script?

a 夏天 提交于 2019-12-02 05:00:48

问题


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

This works fine but I want to know if it is possible to have the python program inside the bash script file so when another user runs the script they don't need to have the python program in their memory; i.e., is it possible to copy and paste the python program into the bash script and run it from within the script itself?


回答1:


#!/bin/bash

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

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

Output:

Argument List: ['-', '1', '2', '3']



回答2:


I think you should be able to put:

python  << END
[Python code here]
END

But I have not tested this.




回答3:


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

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

I wouldn't recommend writing anything too complicated, but it could work for something simple.



来源:https://stackoverflow.com/questions/24896943/how-to-insert-a-python-program-into-a-bash-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!