Bash to store result in variable for other lines in script to use

I can not figure out how to capture the $filename variable store by the bash .

#!/bin/bash

# oldest folder stored as variable for analysis, version log created, and quality indicators matched to run
dir=/home/cmccabe/Desktop/NGS/test

find "$dir" -maxdepth 1 -mindepth 1 -type d -printf '%T+\t%P\0' | sort -rz |
while read -r -d $'\t' time && read -r -d '' filename
do
    printf "The oldest folder is $filename, created on $time and analysis done using v1.4 by $USER at $(date "+%D %r")\n" >> /home/cmccabe/medex.logs/folder.log
    awk -v FL="$filename" '
         FNR == 1 {filenum++}
         filenum==1 && index($0, FL) { 
              match($0, "_0*([0-9]+)/")
              FNUM=substr($0,RSTART+1,RLENGTH-2)
              gsub(/^0+/,"", FNUM)
          }
          filenum==2 && $0 ~ FNUM".pdf$"' /home/cmccabe/s5_files/downloads/list /home/cmccabe/s5_files/pdf/pdf > /home/cmccabe/s5_files/pdf/output
    break
done

echo $filename

So after running the bash , $filename is 12345R12 .

So, the result of the echo is 12345R12 . Currently after the bash runs the $filename is not stored (maybe because of the done).

The result stored in $filename needs to be available for my script to execute. Currently, I manually copy it in but I thought that this might be a better way, but I can not seem to get the syntax correct. Thank you :).

You can't get it right now because it's behind several pipes, and therefore in a subshell. But it's ambiguous anyway -- it's in a loop so there could be many different values. Which value of $filename do you want saved?

1 Like

Yes it is a loop . Basically, the bash loops through dir , which may have 3 files in it, but the oldest $filename in dir is returned by the bash . That is the variable I am trying to store in $filename .

So, if the 3 files were

123.txt
456.txt
789.txt

123.txt would be stored in $filename because that is the oldest. Thank you very much :).

Really? I don't think anything would be stored in $filename, that all happens in another shell. You may be looking at the result of something which happened earlier.

If there were anything in it, I'd expect the last value the variable ever held, not the first.

1 Like

One way to extract the first value of that loop:

find "$dir" -maxdepth 1 -mindepth 1 -type d -printf '%T+\t%P\0' | sort -rz > /tmp/$$
while read -r -d $'\t' time && read -r -d '' filename
do
    printf "The oldest folder is $filename, created on $time and analysis done using v1.4 by $USER at $(date "+%D %r")\n" >> /home/cmccabe/medex.logs/folder.log
    awk -v FL="$filename" '
         FNR == 1 {filenum++}
         filenum==1 && index($0, FL) { 
              match($0, "_0*([0-9]+)/")
              FNUM=substr($0,RSTART+1,RLENGTH-2)
              gsub(/^0+/,"", FNUM)
          }
          filenum==2 && $0 ~ FNUM".pdf$"' /home/cmccabe/s5_files/downloads/list /home/cmccabe/s5_files/pdf/pdf > /home/cmccabe/s5_files/pdf/output
    break
done < /tmp/$$

read filename < /tmp/$$
rm -f /tmp/$$

If your data is formatted differently than you showed, it won't work.

1 Like

I will give that a try. Alternatively since the results of the bash have the $filename value in it (in bold), maybe that could be extracted and stored as $filename .

log stored by the bash at /home/cmccabe/medex.logs/folder.log

The oldest folder is R_2017_01_13_12_11_56_user_S5-00580-24-Medexome, created on 2017-01-17+11:31:02.5035483130 and analysis done using v1.4 by cmccabe at 01/17/17 12:41:03 PM

So the R_2017_01_13_12_11_56_user_S5-00580-24-Medexome would be extracted and saved as $filename for the below processes to use.

 (these are just two of 50 that all use the $filename variable)

I am going to repost as a new detail came up that I overlooked.  Thanks again :).


mkdir -p /home/cmccabe/Desktop/NGS/API/$filename/{bam/{validation,coverage},bedtools,fastqc,hgmd,igv,panel/{reads,},panel/20x/{base,gene,percent},panel/{reads,},panel/30x/{base,gene,percent},pdf,picard,torrent,vcf/overall/{annovar,stats},vcf/panel/{annovar,}}

# create analysis log
echo "analysis done using v1.4 by $USER at $(date "+%D %r")" > /home/cmccabe/Desktop/NGS/API/$filename/analysis.txt

Thank you very much :).

awk

awk -F" is " '{print $NF}' log
R_2017_01_13_12_11_56_user_S5-00580-24-Medexome, created on 2017-01-17+11:31:02.5035483130 and analysis done using v1.4 by cmccabe at 01/17/17 12:41:03 PM