Python:制作多个bash脚本的脚本 [英] Python: script to make multiple bash scripts

查看:238
本文介绍了Python:制作多个bash脚本的脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


$ b

  input1.txt $ 

我有一个名为list.txt的文件,它是一个文件名列表。 b $ b input2.txt
input3.txt

我想构建一个python脚本将为每个这些文件名创建一个文件。更确切地说,我需要它来打印一些文本,合并文件的名称,并将其保存到一个独特的.sh文件。我的脚本如下:

  import os 
os.chdir(/ Users / user / Desktop / Folder )

打开('list2.txt','r')为f:
lines = f.read()。split('')

对于l行:
print#!/ bin / bash\\\
#BSUB -J,l +。sh,\\\
#BSUB -o / scratch / DBC / user /+ l + .sh.out \\\
#BSUB -e /scratch/DBC/user/\"+l+\".sh.err\\\
#BSUB -n 1 \\\
#BSUB -q normal\\\
#BSUB -P DBCDOBZAK\\\
#BSUB -W 168:00\\\

printcd / scratch / DBC / user\\\

打印grep输入',l +>结果。 (l),'w')作为输出:
output.write(l)$ b+ l +.txt

with open $ b

我有几个问题:


  • 输出文件只包含文件的名称 - 不是我打印的内容。



输出文件(我应该有3个)应该看起来像这样:
$ b $ pre $ $ bin
$# Ĵ input3.sh
#BSUB -o /scratch/DBC/user/input1.sh.out
#BSUB -e /scratch/DBC/user/input3.sh.err
#BSUB - n 1
#BSUB -q正常
#BSUB -P DBCDOBZAK
#BSUB -W 168:00

cd / scratch / DBC / user
grepinputinput3> result.input3.txt

我对Python非常陌生,所以我认为可以有更简单的方法打印和插入我的文字。任何帮助将不胜感激。

更新
我现在已经做了下面的脚本,几乎可以工作。


$ b $

  import os 
os.chdir(/ Users / user / Desktop / Folder)

with打开('list.txt','r')为f:
lines = f.read()。split('\\\
')

$ b header =#!/ bin / bash \\\
#BSUB -J%s.sh \\\
#BSUB -o /scratch/DBC/user/%s.sh.out \\\
#BSUB -e / scratch / DBC / user /%s.sh.err \\\
#BSUB -n 1 \\\
#BSUB -q normal \\\
#BSUB -P DBCDOBZAK \\\
#BSUB -W 168:00\\\
%(l,l,l)
script =cd / scratch / DBC / user\\\

script2 ='grep'input'%s>结果。%s.txt\\\
'%(l,l)
all =\\\
.join([header,script,script2])

with open('脚本_ {}。sh'.format(l),'w')作为输出:
output.write(全部)

我仍然有这个问题,它会创建4个脚本,而不是像我预期的那样创建3个脚本:script_input1.sh,script_input2.sh,script_input3.sh和script_sh。最后一个,script_sh只是打印文本,但没有任何输入文本的地方。我认为这是因为我的list.txt文件有一个\ n在它的结尾?但是,我看了,真的没有。有没有解决的办法?也许我可以使用某种长度的函数?
解决方案

另一个解决方案,而不是创建文件

  script1.sh 
script2.sh
script3.sh

等。将创建一个文件文件 script.sh

 #!/ bin / bash 
#BSUB -J$ 1.sh
#BSUB -o /scratch/DBC/user/\"$1\".sh.out
#BSUB -e / scratch / DBC /user/\"$1\".sh.err
#BSUB -n 1
#BSUB -q正常
#BSUB -P DBCDOBZAK
#BSUB -W 168:00

cd / scratch / DBC / user
grepinput$ 1>结果$ 1.txt

使用

  script.sh input1 
script.sh input2
script.sh input3


I have a file, called list.txt, which is a list of names of files:

input1.txt
input2.txt
input3.txt

I want to build a python script that will make one file for each of these filenames. More precisely, I need it to print some text, incorporate the name of the file and save it to a unique .sh file. My script is as follows:

import os
os.chdir("/Users/user/Desktop/Folder")

with open('list2.txt','r') as f:    
    lines = f.read().split(' ')

for l in lines:
    print "#!/bin/bash\n#BSUB -J",l+".sh","\n#BSUB -o /scratch/DBC/user/"+l+".sh.out\n#BSUB -e /scratch/DBC/user/"+l+".sh.err\n#BSUB -n 1\n#BSUB -q normal\n#BSUB -P DBCDOBZAK\n#BSUB -W 168:00\n"
    print "cd /scratch/DBC/user\n"
    print 'grep "input"',l+" > result."+l+".txt"

    with open('script{}.sh'.format(l), 'w') as output:
        output.write(l)

I have a few issues:

  • The output files just contain the name of the file - not the content I have printed.

To be clear my output files (I should have 3) should look like this:

#!/bin/bash
#BSUB -J input3.sh 
#BSUB -o /scratch/DBC/user/input1.sh.out
#BSUB -e /scratch/DBC/user/input3.sh.err
#BSUB -n 1
#BSUB -q normal
#BSUB -P DBCDOBZAK
#BSUB -W 168:00

cd /scratch/DBC/user
grep "input" input3 > result.input3.txt

I am very new to Python so I am assuming there could be much simpler ways of printing and inserting my text. Any help would be much appreciated.

UPDATE I have now made the following script, which nearly works.

import os
os.chdir("/Users/user/Desktop/Folder")

with open('list.txt','r') as f:
    lines = f.read().split('\n')

for l in lines:
    header = "#!/bin/bash \n#BSUB -J %s.sh \n#BSUB -o /scratch/DBC/user/%s.sh.out \n#BSUB -e /scratch/DBC/user/%s.sh.err \n#BSUB -n 1 \n#BSUB -q normal \n#BSUB -P DBCDOBZAK \n#BSUB -W 168:00\n"%(l,l,l)
    script = "cd /scratch/DBC/user\n"
    script2 = 'grep "input" %s > result.%s.txt\n'%(l,l)
    all= "\n".join([header,script,script2])

    with open('script_{}.sh'.format(l), 'w') as output:
        output.write(all)

The problem I still have is that this creates 4 scripts, not 3 as I expected: script_input1.sh, script_input2.sh, script_input3.sh and script_sh. This last one, script_sh just has the printed text but nothing where the "input" text would be. I think this is because my list.txt file has a "\n" at the end of it? However, I looked and there really isn't. Is there a way around this? Maybe I can use some kind of length function?

解决方案

Another solution that, instead of creating files

script1.sh
script2.sh
script3.sh

etc. would be to create one file file script.sh

#!/bin/bash
#BSUB -J "$1".sh 
#BSUB -o /scratch/DBC/user/"$1".sh.out
#BSUB -e /scratch/DBC/user/"$1".sh.err
#BSUB -n 1
#BSUB -q normal
#BSUB -P DBCDOBZAK
#BSUB -W 168:00

cd /scratch/DBC/user
grep "input" "$1" > result."$1".txt

and run it using

script.sh input1
script.sh input2
script.sh input3

这篇关于Python:制作多个bash脚本的脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆