简体   繁体   English

如何将 python 程序插入 bash 脚本?

[英]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.我有一个小型 python 程序,它解析一个文本文件并将输出写入另一个文件。 Currently, I am writing a bash script to call this program several times, it looks something like:目前,我正在编写一个 bash 脚本来多次调用这个程序,它看起来像:

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.对于简单的脚本,您还可以使用 -c 选项运行 python。 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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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