转换一个bash脚本到Python(小脚本) [英] Converting a bash script to python (small script)
问题描述
我有我一直在使用Linux环境bash脚本,但现在我不得不使用它在Windows平台上,并希望在bash脚本转换为Python脚本,我可以运行。
在bash脚本是相当简单的(我认为),我试着将它由谷歌被倒过来,但转换不能成功转换。
在bash脚本是这样的:
运行= 5查询= 50OUTFILE = outputfile.txt日期和GT;> $ OUTFILE
回声-e\\ n ---------------------------------
回声-e\\ n ----------- --------------正常
回声-e\\ n ---------------------------------
回声-e\\ n ----------- --------------正常>> $ OUTFILE
为((R = 1;为r($运行+ 1); R ++))
做
回声-e运行的$ $ R运行\\ n DB2 FLUSH PACKAGE CACHE DYNAMIC 蟒蛇reads.py -r1 -pquery1.sql -q $查询-shotelspec -k6 -A5>> $ OUTFILE
DONE
主要命令,蟒蛇read.py ...等等是我一直在考虑的另一个Python文件,并有论据,你看。
我知道这是一个很多要求,但它真的会帮助我,如果有人可以将其转换为一个Python脚本,我可以使用,或者至少给我一些提示和说明。
真诚
Mestika
添加每个请求:
这是我写的,但没有成功:
运行= 5
查询= 50
OUTFILE = ReadsAgain.txt
文件=打开(RESULTS.TXT,AB)打印\\ n ---------------------------------
打印\\ n ----------- --------------正常
打印\\ n ---------------------------------
file.write(\\ n ----------- --------------正常\\ n)
打印\\ n -------------查询,而不指数--------------
file.write(\\ n -------------没有指数-------------- \\查询N)
当r = 1;为r (%S + 1); - [R ++运行%
打印跑%,共%s \\ n的%R%运行 DB2 FLUSH PACKAGE CACHE DYNAMIC 输出=蟒蛇reads.py -r1 -pquery1.sql -q $查询-shotelspec -k6 -A5
file.write(输出)file.close()
这应该是相当简单的接口程序。唯一棘手的部分将运行DB2命令和(也许)重构reads.py,以便它可以被称为一个库函数。
其基本思想是一样的:
- 设置局部变量是一样的。
- 替换
回声
与打印
。 - 在范围替换
您的回路R(运行):
- 获得
日期
通过日期时间模块。 - 替换写出来与文件对象模块文件。
- 与替换调用
DB2
子模块。 - 您将需要
进口reads.py
作为一个库(或者你可以使用子)来使用。
但是,正如马塞洛说,如果你想要更多的帮助 - 你最好把你自己的一些努力,提出直接的问题。
I’ve a bash script I’ve been using for a Linux environment but now I have to use it on a Windows platform and want to convert the bash script to a python script which I can run.
The bash script is rather simple (I think) and I’ve tried to convert it by google by way around but can’t convert it successfully.
The bash script looks like this:
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
The main command, the python read.py … etc. is another python file I’ve been given and have the arguments as you see.
I know it is a lot to ask for, but it would really help me out if someone could convert this to a python script I can use or at least give me some hints and directions.
Sincerely
Mestika
Added per request:
This is what I've written but without success:
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()
It should be fairly simple to port your program. The only tricky part will be running the db2 command and (maybe) refactoring reads.py so that it can be called as a library function.
The basic idea is the same:
- Setting local variables is the same.
- Replace
echo
withprint
. - Replace your loop with
for r in range(runs):
. - Get the
date
with the datetime module. - Replace write to file with the file objects module.
- Replace the call to
db2
with the subprocess module. - You'll need to
import reads.py
to use as a library (or you can use subprocess).
But, as Marcelo says, if you want more help- you're best off putting in some effort of your own to ask direct questions.
这篇关于转换一个bash脚本到Python(小脚本)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!