Shell-Script

循環使用來自不同目錄的多個文件執行程序

  • October 3, 2019

所以我正在嘗試執行一個程序 iRep 並且通常它執行為-

iRep -f Bins/10000A-01-01_bin.* -s sam/10000A-01-01.sam.sorted.sam --sort -o 10000A-01-01_iRep_output

在山姆文件夾中 -

10000A-01-01.sam.sorted.sam
10000A-01-02.sam.sorted.sam
10000A-01-03.sam.sorted.sam

在 Bins 文件夾中 -

10000A-01-01_bin.1.fa
10000A-01-01_bin.2.fa
10000A-01-01_bin.3.fa

10000A-01-02_bin.1.fa
10000A-01-02_bin.2.fa
10000A-01-02_bin.3.fa

10000A-01-03_bin.1.fa
10000A-01-03_bin.3.fa
10000A-01-03_bin.5.fa
10000A-01-03_bin.7.fa

我想要一個循環,我可以在一個命令中完成所有操作,而不是單獨為每個樣本執行每個命令,例如

iRep -f Bins/10000A-01-01_bin.* -s sam/10000A-01-01.sam.sorted.sam --sort -o 10000A-01-01_iRep_output
iRep -f Bins/10000A-01-02_bin.* -s sam/10000A-01-02.sam.sorted.sam --sort -o 10000A-01-02_iRep_output
iRep -f Bins/10000A-01-03_bin.* -s sam/10000A-01-03.sam.sorted.sam --sort -o 10000A-01-03_iRep_output

知道我該怎麼做嗎?

#!/bin/sh

# Loop over the SAM files
for sam in sam/*.sam.sorted.sam; do
   # Extract the sample name by taking the basename of the SAM file
   # and removing the known filename suffix.
   sample=$(basename "$sam" .sam.sorted.sam)

   # Call iRep (as described in the question)
   iRep -f Bins/"$sample"_bin.* -s "$sam" --sort -o "$sample"_iRep_output
done

鑑於問題中的文件,這將最終執行

iRep -f Bins/10000A-01-01_bin.1.fa Bins/10000A-01-01_bin.2.fa Bins/10000A-01-01_bin.3.fa -s sam/10000A-01-01.sam.sorted.sam --sort -o 10000A-01-01_iRep_output
iRep -f Bins/10000A-01-02_bin.1.fa Bins/10000A-01-02_bin.2.fa Bins/10000A-01-02_bin.3.fa -s sam/10000A-01-02.sam.sorted.sam --sort -o 10000A-01-02_iRep_output
iRep -f Bins/10000A-01-03_bin.1.fa Bins/10000A-01-03_bin.3.fa Bins/10000A-01-03_bin.5.fa -s sam/10000A-01-03.sam.sorted.sam --sort -o 10000A-01-03_iRep_output

引用自:https://unix.stackexchange.com/questions/543308