Home > Enterprise >  How to execute command without using the do command twice in the loop
How to execute command without using the do command twice in the loop

Time:12-20

These are my files

KIMS2021-01_R1.fastq.gz  KIMS2021-05_R1.fastq.gz  SRR1734376_1.fastq.gz  SRR6006897_1.fastq.gz  SRR6006901_1.fastq.gz  SRR6006905_1.fastq.gz
KIMS2021-01_R2.fastq.gz  KIMS2021-05_R2.fastq.gz  SRR1734376_2.fastq.gz  SRR6006897_2.fastq.gz  SRR6006901_2.fastq.gz  SRR6006905_2.fastq.gz
KIMS2021-02_R1.fastq.gz  KIMS2021-06_R1.fastq.gz  SRR1734377_1.fastq.gz  SRR6006898_1.fastq.gz  SRR6006902_1.fastq.gz  SRR6006906_1.fastq.gz
KIMS2021-02_R2.fastq.gz  KIMS2021-06_R2.fastq.gz  SRR1734377_2.fastq.gz  SRR6006898_2.fastq.gz  SRR6006902_2.fastq.gz  SRR6006906_2.fastq.gz
KIMS2021-03_R1.fastq.gz  SRR1734374_1.fastq.gz    SRR6006895_1.fastq.gz  SRR6006899_1.fastq.gz  SRR6006903_1.fastq.gz
KIMS2021-03_R2.fastq.gz  SRR1734374_2.fastq.gz    SRR6006895_2.fastq.gz  SRR6006899_2.fastq.gz  SRR6006903_2.fastq.gz
KIMS2021-04_R1.fastq.gz  SRR1734375_1.fastq.gz    SRR6006896_1.fastq.gz  SRR6006900_1.fastq.gz  SRR6006904_1.fastq.gz
KIMS2021-04_R2.fastq.gz  SRR1734375_2.fastq.gz    SRR6006896_2.fastq.gz  SRR6006900_2.fastq.gz  SRR6006904_2.fastq.gz

To get uniform file name for each pair i got this answer from my previous question which is this

for i in $(echo *.fastq*.gz); do echo ${i%_*}; done | uniq

My final command which i want to use after I get uniform file name

for i in $(ls *.fastq*.gz); do echo ${i%_*}; done | uniq; do STAR --runMode alignReads --outSAMtype BAM SortedByCoordinate --runThreadN 30 --genomeDir /run/media/punit/data3/Santosh_star_index --readFilesIn  <(gunzip -c ${i}_R1.fastq.gz ${i}_R2.fastq.gz ) --outFileNamePrefix ${i%};done

Now i understand i can;t use do twice in a loop

How to pass the uniform file name to my command without using do twice

CodePudding user response:

from my previous question which is this

For starters let's fix it! Do not do:

for i in $(anything); do

Just do:

for i in *.fastq*.gz; do

How to pass the uniform file name to my command

Shell most importantly works with pipes |. The output of one command is input to another - like in your pipe, the output of for loop is the input of uniq.

Read the output of uniq.

for i in *.fastq*.gz; do echo "${i%_*}"; done |
   uniq |
   while IFS= read -r file; do stuff with "$file"; done

Check your scripts with shellcheck.net . See https://mywiki.wooledge.org/BashFAQ/001

as a biologist its a pain for me do dig the basic nuances of the scripting my simple solution would be if i run the them as two sets

Sure! I do that when interactive scripting a lot.

tmp=$( for i in *.fastq*.gz; do echo "${i%_*}"; done )
tmp=$( uniq <<<"$tmp" )   # or tmp=$( echo "$tmp" | uniq )
tmp=$( echo "$tmp" | while IFS= read -r file; do stuff with "$file"; done )
echo "$tmp"

Remember about qoutes "! <<<"stuff" works like <file - inputs the content of "stuff" to the input of the command, does the same as echo "$tmp" |.

  • Related