2013-03-21 66 views
0

我在運行blastx的bash中創建了一個腳本。for循環在我的腳本中不工作

#!/bin/sh -l 
# $Id: Barrine.sh federicogaiti $ 

echo "Right now it is:" 
date 
echo "" 
function usage() { 
echo "Barrine.sh script by Federico Gaiti, March 2013." 
echo "" 
echo "Run Blastx" 
echo "" 
echo "Usage: " 
echo "Barrine.sh <All contigs (fasta extension)> <Number of fasta split sequences> <Head file with PBS command where you set up account, walltime, etc..> " 
echo "" 
echo "Example: " 
echo "Barrine.sh InputFasta n Head " 
echo "" 
exit 1 
} 

# Testing if the number of arguments is correct 
if [ $# != 3 ] 
then 
    usage 
     exit 
    fi 

### Declaring variables 
InputFasta=$1 MY ORIGINAL FASTA FILE WITH ALL THE SEQUENCES 
n=$2     NUMBER OF FILES I WANT MY FASTA FILE SPLIT 
Head=$3   PBS OPTIONS 
n4=$((n/4)) 
n41=$((n/4 + 1)) 
n2=$((n/2)) 
n21=$((n/2 + 1)) 
n43=$((n/4 * 3)) 
n431=$((n/4*3 + 1)) 


echo Loading Modules 
module load ucsc_utilities/20130122 
wait 


echo Split input FASTA in n FASTA file 
faSplit sequence ${InputFasta} ${n} ${InputFasta}_Split_S 
wait 



echo blastx command splitting it in 4 jobs to make it faster 
    for i in {000..0$n4} ; do echo "blastx -query ${InputFasta}_Split_S$i.fa -evalue 0.0001 -max_target_seqs 100 -db nr -num_threads 8 -outfmt '7 qseqid qlen sseqid pident length mismatch gapopen qstart qend sstart send ppos evalue bitscore score' -out ${InputFasta}_blastx$i.csv" ; done > ${InputFasta}_Job1.sh 


    for i in {0$n41..$n2} ; do echo "blastx -query ${InputFasta}_Split_S$i.fa -evalue 0.0001 -max_target_seqs 100 -db nr -num_threads 8 -outfmt '7 qseqid qlen sseqid pident length mismatch gapopen qstart qend sstart send ppos evalue bitscore score' -out ${InputFasta}_blastx$i.csv" ; done > ${InputFasta}_Job2.sh 


    for i in {$n21..$n43} ; do echo "blastx -query ${InputFasta}_Split_S$i.fa -evalue 0.0001 -max_target_seqs 100 -db nr -num_threads 8 -outfmt '7 qseqid qlen sseqid pident length mismatch gapopen qstart qend sstart send ppos evalue bitscore score' -out ${InputFasta}_blastx$i.csv" ; done > ${InputFasta}_Job3.sh 


    for i in {$n431..$n} ; do echo "blastx -query ${InputFasta}_Split_S$i.fa -evalue 0.0001 -max_target_seqs 100 -db nr -num_threads 8 -outfmt '7 qseqid qlen sseqid pident length mismatch gapopen qstart qend sstart send ppos evalue bitscore score' -out ${InputFasta}_blastx$i.csv" ; done > ${InputFasta}_Job4.sh 

wait 


echo Head the PBS commands to the Job files 
    for i in {1..4} 
     do 
     cat ${Head} ${InputFasta}_Job$i.sh | sed "s/BlastJob/BlastJob_$i/g" > ${InputFasta}_BlastJob$i.sh 
     done 

然後,我會將4個工作提交給barrine服務器。 什麼我應該得到的含有該4個作業:

blastx -query TEST_Split_S000.fa -evalue 0.0001 -max_target_seqs 100 -db nr -num_threads 8 -outfmt '7 qseqid qlen sseqid pident length mismatch gapopen qstart qend sstart send ppos evalue bitscore score' -out TEST_blastx000.csv 
blastx -query TEST_Split_S001.fa -evalue 0.0001 -max_target_seqs 100 -db nr -num_threads 8 -outfmt '7 qseqid qlen sseqid pident length mismatch gapopen qstart qend sstart send ppos evalue bitscore score' -out TEST_blastx001.csv 
blastx -query TEST_Split_S002.fa -evalue 0.0001 -max_target_seqs 100 -db nr -num_threads 8 -outfmt '7 qseqid qlen sseqid pident length mismatch gapopen qstart qend sstart send ppos evalue bitscore score' -out TEST_blastx002.csv 
blastx -query TEST_Split_S003.fa -evalue 0.0001 -max_target_seqs 100 -db nr -num_threads 8 -outfmt '7 qseqid qlen sseqid pident length mismatch gapopen qstart qend sstart send ppos evalue bitscore score' -out TEST_blastx003.csv 
blastx -query TEST_Split_S004.fa -evalue 0.0001 -max_target_seqs 100 -db nr -num_threads 8 -outfmt '7 qseqid qlen sseqid pident length mismatch gapopen qstart qend sstart send ppos evalue bitscore score' -out TEST_blastx004.csv 
blastx -query TEST_Split_S005.fa -evalue 0.0001 -max_target_seqs 100 -db nr -num_threads 8 -outfmt '7 qseqid qlen sseqid pident length mismatch gapopen qstart qend sstart send ppos evalue bitscore score' -out TEST_blastx005.csv 
blastx -query TEST_Split_S006.fa -evalue 0.0001 -max_target_seqs 100 -db nr -num_threads 8 -outfmt '7 qseqid qlen sseqid pident length mismatch gapopen qstart qend sstart send ppos evalue bitscore score' -out TEST_blastx006.csv 
blastx -query TEST_Split_S007.fa -evalue 0.0001 -max_target_seqs 100 -db nr -num_threads 8 -outfmt '7 qseqid qlen sseqid pident length mismatch gapopen qstart qend sstart send ppos evalue bitscore score' -out TEST_blastx007.csv 
............ 
blastx -query TEST_Split_S050.fa -evalue 0.0001 -max_target_seqs 100 -db nr -num_threads 8 -outfmt '7 qseqid qlen sseqid pident length mismatch gapopen qstart qend sstart send ppos evalue bitscore score' -out TEST_blastx050.csv 

但我在我的劇本有就是:

blastx -query TEST_Split_S{000..050}.fa -evalue 0.0001 -max_target_seqs 100 -db nr -num_threads 8 -outfmt '7 qseqid qlen sseqid pident length mismatch gapopen qstart qend sstart send ppos evalue bitscore score' -out TEST_blastx{000..050}.csv 

有人可以幫我解決這個問題? 如果我在我的腳本之外使用for循環可以正常工作。

感謝您的幫助

回答

3

括號擴展早期發生。

參數擴展稍後發生,因此您不能以控制大括號擴展的方式使用變量或其他參數。

the bash(1) man page:

擴展的順序是:括號擴展,波浪線擴展, 參數,變量和算術擴展和命令 取代(在左到右的方式進行),詞的拆分,和 路徑名稱擴展。

這會阻止像for i in {000..0$n4} ; do這樣的語句按預期工作。

此外,一定要記住-v-x參數來顯示執行的實際命令。

+0

治癒可能是'爲我在$(eval回聲{000..0 $ n4}); ...'; pre-eval用適當的值(50?)代替'0 $ n4',然後'eval'在'{000..050}'上重新進行大括號擴展,給出單獨的值。注意:'eval'通常是危險的。但是,如果'$ n4'這樣的變量保證包含數字,則這完全是一種良性使用。 – 2013-03-21 04:48:41

+0

感謝提示喬納森。我一定會嘗試使用eval。謝謝你的提示! – user1819854 2013-03-21 05:18:56

+0

感謝DigitalRoss的解釋。現在所有的工作都很好,我把-x命令用於調試。 – user1819854 2013-03-21 05:45:59