将bash脚本转换为python(小脚本)

Mes*_*ika 10 python bash

我有一个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

回答

让我们把它分解成碎片.特别是你弄错了.:)


分配

outfile=ReadsAgain.txt
Run Code Online (Sandbox Code Playgroud)

你需要在字符串周围添加引号应该不足为奇.另一方面,=为了便于阅读,您可以放置​​空间.

outfilename = "ReadsAgain.txt"
Run Code Online (Sandbox Code Playgroud)

可变扩展→ str.format(或者,%操作)

python reads.py <snip/> -q$queries <snip/>
Run Code Online (Sandbox Code Playgroud)

所以你知道如何进行重定向,但是你如何进行变量扩展呢?您可以使用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)

C风格的循环→ 面向对象风格的循环

for ((r = 1; r < ($runs + 1); r++)) do done
Run Code Online (Sandbox Code Playgroud)

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)

所以:

python reads.py -r1 -pquery1.sql -q$queries -shotelspec -k6 -a5 >> $outfile
Run Code Online (Sandbox Code Playgroud)

变为:

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绝对是正确的选择.

  • +1 表示慷慨的精神、勤奋的工作和仔细的解释,忽略了 OP 没有采取丝毫努力来学习 Python 或 StackOverflow 的事实(我也犯过同样的错误)。 (3认同)
  • +1:你投入的努力值得;) (2认同)

Ste*_*hen 8

移植程序应该相当简单.唯一棘手的部分是运行db2命令和(可能)重构reads.py,以便可以将其作为库函数调用.

基本思路是一样的:

  • 设置局部变量是相同的.
  • 替换echoprint.
  • 用你的循环代替for r in range(runs):.
  • 获取date日期时间模块.
  • 文件对象模块替换写入文件.
  • 调用替换为db2模块.
  • 您需要将import reads.py其用作库(或者您可以使用子进程).

但是,正如马塞洛所说,如果你想要更多的帮助 - 你最好自己付出一些努力直接提出问题.