【问题标题】:Python: script to make multiple bash scriptsPython:制作多个 bash 脚本的脚本
【发布时间】:2018-03-22 23:35:58
【问题描述】:

我有一个文件,叫做 list.txt,它是文件名的列表:

input1.txt
input2.txt
input3.txt

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

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)

我有几个问题:

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

要清楚我的输出文件(我应该有 3 个)应该是这样的:

#!/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

我对 Python 很陌生,所以我假设可能有更简单的方法来打印和插入我的文本。任何帮助将不胜感激。

更新 我现在制作了以下脚本,几乎可以正常工作。

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)

我仍然遇到的问题是,这会创建 4 个脚本,而不是我预期的 3 个:script_input1.sh、script_input2.sh、script_input3.sh 和 script_sh。最后一个,script_sh 只有打印的文本,但没有“输入”文本所在的位置。我认为这是因为我的 list.txt 文件末尾有一个“\n”?但是,我看了,确实没有。有没有解决的办法?也许我可以使用某种长度函数?

【问题讨论】:

  • 真的不用Python,Bash脚本也行吗?
  • 不,我真的很想在这种情况下使用 Python。我知道 Bash 可以做到这一点。
  • 正确的解决方案可能是将变量作为参数传递给静态脚本,而不是编写一些内联变量的静态脚本。

标签: python bash for-loop output writing


【解决方案1】:

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

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 normal
#BSUB -P DBCDOBZAK
#BSUB -W 168:00

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

并使用

运行它
script.sh input1
script.sh input2
script.sh input3

【讨论】:

    【解决方案2】:

    所以,按顺序回答:

    1) 你能详细说明这个问题吗?你算了 4 个 txt 文件,但你的代码只生成了 3 个不同的脚本?

    2) 当然,您需要创建一个 var,而不仅仅是使用 print 语句 3)只需更改权限

    所以,总结一下,我会使用这种方法:

    import os
    for i, file in enumerate(os.listdir("/Users/user/Desktop/Folder")):
      if "input" in file:
        with open(file) as f:
            lines = f.readlines()
    
            for l in lines:
                data = ""
                data += "#!/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"
                data += "cd /scratch/DBC/user\n"
                data += 'grep "input"'+l+" > result."+l+".txt"
    
                with open('script%s.sh'%i, 'w') as output:
                    output.write(data)
                os.chmod("script%s.sh'%i", 700)
    

    顺便说一句,我的代码只是一个猜测。我认为您在说明您的问题时应该更加明确。我不明白你想要实现什么。

    【讨论】:

    • 谢谢@FrankBr。回答您的问题: 1)确实,我的 list.txt 文件只有 3 个文件(input1.txt、input2.txt 和 input3.txt)。但是,我当前的代码创建了 4 个文件:“scriptinput1.sh”、“scriptinput2.sh”、“scriptinput3.sh”和“script?.sh”。起初,我认为我的代码正在制作第 4 个文件,因为我在 list.txt 的末尾有一个换行符或空格。但是我删除了所有空格和换行符,所以我很困惑。 2)这对var有意义。你能解释一下“如果文件中的“输入”:”是做什么的吗?
    • 由于您循环遍历一个文件夹,请务必选择名称中包含“输入”的文件。但是,如果您知道文件名,只需删除该行。没看懂。
    • 谢谢。我会继续寻找,因为它仍然无法正常工作。问题 1 已解决,但我使用变量存储文本不起作用。 Python 告诉我你不能追加到元组变量。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-03
    • 2017-12-12
    • 2017-01-11
    • 2012-11-21
    • 2011-08-15
    • 2020-04-21
    • 1970-01-01
    相关资源
    最近更新 更多