2017-03-02 30 views
0

目前我正在研究一個腳本,該腳本應該生成一些可以提交給羣集的PBS腳本。我的正常腳本運行良好,但現在我面臨着爲一個程序輸入兩個文件的問題。我的一個腳本例如看起來像:如何在一個循環中獲取兩個文件

#!/bin/bash 

echo -e "#!/bin/bash\n 
#SBATCH --job-name=whatever 
#SBATCH --export=NONE 
#SBATCH --nodes=1 
#SBATCH --cpus-per-task=8 
#SBATCH --mem=80G 
#SBATCH --partition=blabla 
#SBATCH --blabla" >> $1 

echo -e "touch log_file_$1\n" >> $1 

x=$(cd $(dirname ${BASH_SOURCE[0]}) && pwd) 

for file in /foo/bar/foo/bar/*; do 
rl=$(readlink -f $file) 
kw=${rl##*/} 
id=${kw%%.*} 
gz_weg=${kw%.*} 

if [ ! -d "$id" ]; then 
    mkdir "$id" 
fi 

echo "echo $kw >> log_file_$1" >> $1 
printf "foo-bar --mode barbar -e 0.001 --index /barz/barz/barz.index --inFile $rl --output $x/$id/$gz_weg.rma 2>> $x/log_file_$1 \n" >> $1 
echo "echo -e '"\\n"' >> log_file_$1" >> $1 
echo -e "\n" >> $1 
done 

不是一個美麗我猜,但它適用於我。但現在如上所述,我面臨着有兩個輸入文件的問題。他們都在同一個文件夾中,我試過類似的東西:

for file in /ifs/data/nfs_share/sukmb241/raw_data/samples/iceman_old/iceman.UDG.*/*.fastq.gz; do 

bs=$(basename $file) 

if [[ "$bs" == *R1* ]]; then 
    r1=$(readlink -f $file) 
    k1=${r1##*/} 
    id1=${k1%%.*} 
    gz_weg1=${k1%.*} 
fi 


if [[ "$bs" == *R2* ]]; then 
    r2=$(readlink -f $file) 
    k2=${r2##*/} 
    id2=${k2%%.*} 
    gz_weg1=${k2%.*} 
fi 


if [ ! -d "$id1" ]; then 
    mkdir "$id1" 
fi 

echo "echo $kw >> log_file_$1" >> $1 
printf "blablabla -in1 $r1 -in2 $r2 -f foo -r bar -l 25 -qt -q 20 -o $x/$id1/whatever -verbose 2>> $x/log_file_$1 \n" >> $1 
echo "echo -e '"\\n"' >> log_file_$1" >> $1 
echo -e "\n" >> $1 
done 
fi 

因爲這些文件僅在R1或R2的文件名中有所不同。但是,我意識到這將無法正常工作,因爲它只會給我一個文件。因此,如何解決-IN1指向包含R1的文件和-in2包含R2

感謝提前:)

回答

1

如果您保存您的論點事先變量的問題,那麼你就可以更換論點的文件列表,並消耗他們每次兩個:

out_file=$1 
set -- /ifs/data/nfs_share/sukmb241/raw_data/samples/iceman_old/iceman.UDG.*/*.fastq.gz 

while [[ -z $1 ]] 
do 
    # Get the next two filenames 
    file1=$1 
    file2=$2 
    # discard them from arguments 
    shift 2 

    # Then the rest of the script 
    bs1=... 
    # Use $out_file instead of $1 
done 

這可能會運行耗盡空間參數的風險,所以你可以通過修剪出來的路徑節省一點:

out_file=$1 
dirpath=/ifs/data/nfs_share/sukmb241/raw_data/samples/iceman_old/ 
cd "$dirpath" 
set -- iceman.UDG.*/*.fastq.gz 
cd "$OLDPWD" 
while [[ -z $1 ]] 
do 
    # Get the next two filenames 
    file1="$dirpath/$1" 
    file2="$dirpath/$2" 
    # discard them from arguments 
    shift 2 
    ... 

如果所有R1文件具有相應R2文件,那麼你就需要採取文件的兩個在一個時間 - 只是循環遍歷所有R1文件,然後採取相應的R2文件:

for file in /ifs/data/nfs_share/sukmb241/raw_data/samples/iceman_old/iceman.UDG.*/*R1*.fastq.gz; do 
    r1=$(readlink -f $file) 
    k1=${r1##*/} 
    id1=${k1%%.*} 
    gz_weg1=${k1%.*} 


    # Change R1 to R2 in filename 
    file=${file//R1/R2} 
    r2=$(readlink -f $file) 
    k2=${r2##*/} 
    id2=${k2%%.*} 
    gz_weg2=${k2%.*} 

    if [ ! -d "$id1" ]; then 
     mkdir "$id1" 
    fi 

    echo "echo $kw >> log_file_$1" >> $1 
    printf "blablabla -in1 $r1 -in2 $r2 -f foo -r bar -l 25 -qt -q 20 -o $x/$id1/whatever -verbose 2>> $x/log_file_$1 \n" >> $1 
    echo "echo -e '"\\n"' >> log_file_$1" >> $1 
    echo -e "\n" >> $1 
done 

file=${file//R1/R2}R2代替文件名中的R1,從而給出另一個文件名。

+0

你會很高興將它包含到我的腳本中,以便它可以工作嗎? Atm我沒有足夠的大腦能力來讓它工作。 – JFS31

+0

@ JFS31是否所有'R1'文件都有對應的'R2'文件? – muru

+0

是的,他們有。在每個文件夾中有兩個文件,例如:D0770_S23_L001_R1_001.fastq.gz和D0770_S23_L001_R2_001.fastq.gz,我需要處理。 – JFS31