2010-03-01 122 views
2

我想從Bash while循環中調用Python腳本。但是,我不太清楚如何正確使用Bash的while循環(也許是變量)語法。我正在尋找的行爲是,雖然文件仍然包含行(DNA序列),但我調用Python腳本來提取序列組,以便另一個程序(dialign2)可以對齊它們。最後,我將對齊添加到結果文件中。注意:我不想遍歷文件。爲了讓Bash while循環正常工作,我應該更改哪些內容?我還想確保while循環將重新檢查每個循環中正在更改的file.txt。這是我的嘗試:Bash while循環調用Python腳本

#!/bin/bash 
# Call a python script as many times as needed to treat a text file 

c=1 
while [ `wc -l file.txt` > 0 ] ; # Stop when file.txt has no more lines 
do 
    echo "Python script called $c times" 
    python script.py # Uses file.txt and removes lines from it 
    # The Python script also returns a temp.txt file containing DNA sequences 
    c=$c + 1 
    dialign -f temp.txt # aligns DNA sequences 
    cat temp.fa >>results.txt # append DNA alignements to result file 
done 

謝謝!

+0

你可能需要一個更清楚一點什麼沒有做什麼工作的? – Singletoned 2010-03-01 23:35:12

+0

'WC -l file.txt'將返回像'100 file.txt',如果你真的想使用wc,你需要提取數字。例如。 'wc -l file.txt | cut -f1 -d''' – MattH 2010-03-01 23:40:41

+0

爲什麼不從Python腳本調用Python? – Alex 2010-03-02 00:06:05

回答

3

不知道爲什麼要這麼做。

c=1 
while [[ -s file.txt ]] ; # Stop when file.txt has no more lines 
do 
    echo "Python script called $c times" 
    python script.py # Uses file.txt and removes lines from it 
    c=$(($c + 1)) 
done 
+0

如果文件存在並且大小大於零,則「-s file」爲True。 – MattH 2010-03-01 23:38:41

+0

你只需要小心,python腳本不會留下任何結尾的換行符或什麼。 – 2010-03-01 23:41:21

+0

有很多關於這個問題的提示警告謹慎。我很難想象爲什麼沒有參數的python腳本需要被bash多次調用。 – MattH 2010-03-01 23:46:48

1

嘗試-gt消除外殼元字符>

while [ `wc -l file.txt` -gt 0 ] 
do 
    ... 
    c=$[c + 1] 
done 
+0

-gt操作符到底是什麼? – Morlock 2010-03-01 23:50:51

+0

gt =「大於」 – vladr 2010-03-02 00:06:32

+0

@Vlad Romascanu謝謝! – Morlock 2010-03-02 00:08:51

0

下應該做的,你說你想要的:

#!/bin/bash 

c=1 
while read line; 
do 
    echo "Python script called $c times" 
    # $line contains a line of text from file.txt 
    python script.py 
    c=$((c + 1)) 
done < file.txt 

然而,沒有必要使用bash,遍歷文件中的行。你可以很容易地做到這一點,而不必離開python:

myfile = open('file.txt', 'r') 

for count, line in enumerate(myfile): 
    print '%i lines in file' % (count + 1,) 
    # the variable "line" contains the line of text from the file.txt 

    # Do your thing here. 
+0

UUOC。不需要貓while while循環 – ghostdog74 2010-03-01 23:48:56

+0

嗨,我不是試圖只遍歷一個文件的行,而是要創建另一個臨時文件在每個傳遞由另一個程序使用,直到原始文件爲空。我在這個問題中增加了細節。乾杯 – Morlock 2010-03-01 23:52:34

+0

@ ghostdog74:的確如此。但是如果你在讀x vezult 2010-03-02 13:41:53

0

@OP如果你想循環一個文件,只是在讀循環時使用。此外,您不使用變量$ c以及該行。你是否將每一行傳遞給你的Python腳本?或者,只要遇到某一行,就調用Python腳本? (如果你這樣做,你的腳本會變得很慢)

while true 
do 
    while read -r line 
    do 
     # if you are taking STDIN in myscript.py, then something must be passed to 
     # myscript.py, if not i really don't understand what you are doing. 

     echo "$line" | python myscript.py > temp.txt 
     dialign -f temp.txt # aligns DNA sequences 
     cat temp.txt >>results.txt 
    done <"file.txt" 
    if [ ! -s "file.txt" ]; break ;fi 
done 

最後,你可以用Python做所有事情。在Python遍歷文件「file.txt」的方法就是

f=open("file.txt"): 
for line in f: 
    print "do something with line" 
    print "or bring what you have in myscript.py here" 
f.close() 
+0

謝謝,但我不想通過文件循環。我在原始問題中增加了細節。 – Morlock 2010-03-01 23:56:51

+0

是的,你是。您在while循環中使用file.txt上的wc -l,這意味着您正在迭代文件。唯一的區別是您是否想要使用迭代的行變量。 – ghostdog74 2010-03-01 23:59:12

+0

我只是試圖使用文件的特徵(它仍然有信息)作爲繼續使用Python腳本的標準。我想'wc'函數是迭代文件。你是這個意思嗎? – Morlock 2010-03-02 00:02:14