2014-10-30 39 views
0

我想使用python生成24個shell腳本,這些腳本的名稱和命令中列出的i不同,並將它們寫入24個單獨的文件。寫入「內容」的所有內容的正確語法是什麼?在包含可迭代參數的python中生成多個bash腳本

for i in range(1,25): 

    with open('run_chr_{0}.sh'.format(i),'w') as f: 
     content = [ 

     #!/bin/bash 
     #$ -cwd 
     #$ -V 
     #$ -N run_chr $i 
     #$ -e chr_$i.err 
     #$ -o chr_$i.out 

     DATA_DIR=___ 
     bfile=___ 
     plink --bfile ${DATA_DIR}$bfile.name --chr $i --make-bed --out ${DATA_DIR}$bfile.chr$i\n" 

     ] 
     f.writelines(content) 
    f.close() 
+2

這看起來並不像有效的Python代碼,這是相當不清楚你正試圖在這裏實現什麼(由於混合Python的變量在bash變量:你希望「$ i」設置爲自動替換的Python沒有按?不這樣做)。您可能需要考慮先閱讀字符串格式。 – 2014-10-30 22:03:25

+1

@kat初看起來你的(不清楚)意圖可以通過參數化的shell腳本實現,用'$ 1'替換'content'中的所有'$ i',將'content'的內容保存到一個文件中,比如'run_chr '並給出命令'sh run_chr 13'來操作數據集#13 – gboffi 2014-10-30 22:38:20

+0

類似於http://stackoverflow.com/questions/26661804/shell-variables-in-qsub即使主題不同,至少在面對它。 – tripleee 2014-10-31 07:51:10

回答

0

真的是沒有必要寫多個腳本來執行你要求什麼。如果將以下腳本保存爲run_chr,然後將其作爲./run_chr 17運行,則它將以i=17運行。

#!/bin/bash 
    #$ -cwd 
    #$ -V 
    #$ -N run_chr $i 
    #$ -e chr_$i.err 
    #$ -o chr_$i.out 

    DATA_DIR=___ 
    bfile=___ 
    i=$1 
    plink --bfile "${DATA_DIR}$bfile.name" --chr "$i" --make-bed --out "${DATA_DIR}$bfile.chr$i"