我有一个bash脚本,我一直在用于Linux环境,但现在我必须在Windows平台上使用它,并希望将bash脚本转换为我可以运行的python脚本.
bash脚本相当简单(我认为),我试图通过谷歌转换它,但无法成功转换它.
bash脚本如下所示:
runs=5
queries=50
outfile=outputfile.txt
date >> $outfile
echo -e "\n---------------------------------"
echo -e "\n----------- Normal --------------"
echo -e "\n---------------------------------"
echo -e "\n----------- Normal --------------" >> $outfile
for ((r = 1; r < ($runs + 1); r++))
do
echo -e "Run $r of $runs\n"
db2 FLUSH PACKAGE CACHE DYNAMIC
python reads.py -r1 -pquery1.sql -q$queries -shotelspec -k6 -a5 >> $outfile
done
Run Code Online (Sandbox Code Playgroud)
主命令,python read.py ...等是我给出的另一个python文件,并且如你所见有参数.
我知道这要求很多,但是如果有人可以将其转换为我可以使用的python脚本或者至少给我一些提示和指示,它真的会帮助我.
诚挚
Mestika
这是我写的但没有成功:
runs=5
queries=50
outfile=ReadsAgain.txt
file = open("results.txt", "ab")
print "\n---------------------------------"
print "\n----------- Normal --------------"
print "\n---------------------------------"
file.write("\n----------- Normal --------------\n")
print "\n------------- Query without Index --------------"
file.write("\n------------- Query without Index --------------\n")
for r = 1; r < (%s + 1); r++ % runs
print "Run %s of %s \n" % r % runs
db2 FLUSH PACKAGE CACHE DYNAMIC
output = python reads.py -r1 -pquery1.sql -q$queries -shotelspec -k6 -a5
file.write(output)
file.close()
Run Code Online (Sandbox Code Playgroud)
bad*_*adp 33
让我们把它分解成碎片.特别是你弄错了.:)
Run Code Online (Sandbox Code Playgroud)outfile=ReadsAgain.txt
你需要在字符串周围添加引号应该不足为奇.另一方面,=为了便于阅读,您可以放置空间.
outfilename = "ReadsAgain.txt"
Run Code Online (Sandbox Code Playgroud)
str.format(或者,%操作)Run Code Online (Sandbox Code Playgroud)python reads.py <snip/> -q$queries <snip/>
所以你知道如何进行重定向,但是你如何进行变量扩展呢?您可以使用的format方法(V2.6 +):
command = "python reads.py -r1 -pquery1.sql -q{0} -shotelspec -k6 -a5".format(queries)
Run Code Online (Sandbox Code Playgroud)
您也可以使用该%运营商:
#since queries is a number, use %d as a placeholder
command = "python reads.py -r1 -pquery1.sql -q%d -shotelspec -k6 -a5" % queries
Run Code Online (Sandbox Code Playgroud)
Run Code Online (Sandbox Code Playgroud)for ((r = 1; r < ($runs + 1); r++)) do done
Python中的循环不同于C风格的迭代.Python中发生的事情是迭代可迭代对象,例如列表.在这里,你试图做一些事情runs,所以你会这样做:
for r in range(runs):
#loop body here
Run Code Online (Sandbox Code Playgroud)
range(runs)等价于整数元素[0,1,...,runs-1]的列表runs = 5.所以你会重复身体runs时间.在每个cicle,r分配列表的下一个项目.因此,这完全等同于您在Bash中所做的事情.
如果你感觉大胆,请xrange改用.它完全等效,但使用更高级的语言功能(因此用外行的术语来解释起来比较困难),但消耗的资源更少.
subprocess模块"更强硬"的部分,如果您将:执行程序并获得其输出.谷歌救援!显然,最热门的是一个stackoverflow问题:这一个.您可以使用一个简单的函数隐藏它背后的所有复杂性:
import subprocess, shlex
def get_output_of(command):
args = shlex.split(command)
return subprocess.Popen(args,
stdout=subprocess.PIPE).communicate()[0]
# this only returns stdout
Run Code Online (Sandbox Code Playgroud)
所以:
Run Code Online (Sandbox Code Playgroud)python reads.py -r1 -pquery1.sql -q$queries -shotelspec -k6 -a5 >> $outfile
变为:
command = "python reads.py -r1 -pquery1.sql -q%s -shotelspec -k6 -a5" % queries
read_result = get_output_of(command)
Run Code Online (Sandbox Code Playgroud)
subprocess包括电池(可选)考虑您可以date使用以下内容获得几乎相同的输出:
import time
time_now = time.strftime("%c", time.localtime()) # Sat May 15 15:42:47 2010
Run Code Online (Sandbox Code Playgroud)
(注意没有时区信息.如果对你很重要,这应该是另一个问题的主题.)
最终结果应如下所示:
import subprocess, shlex, time
def get_output_of(command):
#... body of get_output_of
#... more functions ...
if __name__ = "__main__":
#only execute the following if you are calling this .py file directly,
#and not, say, importing it
#... initialization ...
with file("outputfile.txt", "a") as output_file: #alternative way to open files, v2.5+
#... write date and other stuff ...
for r in range(runs):
#... loop body here ...
Run Code Online (Sandbox Code Playgroud)
与相对简单和简短的Bash脚本相比,这看起来非常可怕,对吧?Python不是一种专门的语言:它的目的是做好所有事情,但不是直接用于运行程序并获得它们的输出.
不过,你通常不会在Bash中编写数据库引擎,对吗?它是适用于不同工作的不同工具.在这里,除非你打算做一些用这种语言写一些非常重要的改动,[Ba] sh绝对是正确的选择.
| 归档时间: |
|
| 查看次数: |
21439 次 |
| 最近记录: |