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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash to store result in variable for other lines in script to use
# 1  
Old 01-17-2017
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.

Code:
#!/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 Smilie.

Last edited by cmccabe; 01-17-2017 at 02:00 PM.. Reason: fixed format
# 2  
Old 01-17-2017
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?
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 01-17-2017
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
Code:
123.txt
456.txt
789.txt

123.txt would be stored in $filename because that is the oldest. Thank you very much Smilie.
# 4  
Old 01-17-2017
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.

Last edited by Corona688; 01-17-2017 at 02:40 PM..
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 01-17-2017
One way to extract the first value of that loop:

Code:
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.
This User Gave Thanks to Corona688 For This Post:
# 6  
Old 01-17-2017
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

Code:
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.

Code:
 (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 Smilie.

awk
Code:
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


Last edited by cmccabe; 01-17-2017 at 06:44 PM.. Reason: added not so perfect awk attempt
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash to match and store line as variable

The bash below loops through a specific directory dir and finds and writes the oldest folder to a variable called $filename. #!/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... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

Store result variable

Friends have the following problem: cat $PATH_DAT/mr.txt | nawk 'BEGIN { CantPnt=0; NumReg=0; FS="|" } { NumReg++ CantPnt=CantPnt+int($2) } END{ printf... (5 Replies)
Discussion started by: tricampeon81
5 Replies

3. Shell Programming and Scripting

Call a Perl script within a bash script and store the ouput in a .txt file

I'm attempting to write a bash script that will create a network between virtual machines. It accepts three arguments: an RSpec that describes the network topology, and two list of machines (servers and clients). I have a (working) Perl script that I want to call. This Perl script takes an RSpec... (6 Replies)
Discussion started by: mecaka
6 Replies

4. Shell Programming and Scripting

How to store result of grep into a variable?

In a directory i have a file *output* whose contents are changing for every simulation. zgrep "trace file is" *output* | zgrep g o/p: trace file is Int_01.1352176388z4f56ec33.0.trace.gz I want to extract "Int_01.1352176388z4f56ec33.0.trace.gz" from the above result into a variable. i... (2 Replies)
Discussion started by: twistedpair
2 Replies

5. Shell Programming and Scripting

Bash script to display result in table

My script gives the following result. Is it possible to display the same in table format ? 1. rex_best Latest feeds are not avaialable. The last feed was generated on 2012-05-17 File Name = ekb_best_20120517_010949_665.tar.gz The Number of entry elements = 4209539 2. rex_genre Latest... (2 Replies)
Discussion started by: kishorekumar87
2 Replies

6. Shell Programming and Scripting

How i can put the result of a command inside a bash variable?

#!/bin/bash #... for i in `ls -c1 /usr/share/applications` do name="cat $i | grep ^Name= | cut -d = -f2" echo $name #... done Now inside name as output is present: while i want only the result of the command. Ideally i would like obtain that information using only bash ... or... (8 Replies)
Discussion started by: alexscript
8 Replies

7. Shell Programming and Scripting

Script to store the variable in a table or array.

Hi, I have few variable say 10 ex:- l_pc_291334_01_0_01_00.cmp l_pc_441133_50_0_02_00.cmp l_pc_441133_55_0_02_00.cmp Each variable value is coming via loop on a table. I want to create a script that stores these value to a table or array ( But one by one not all at one time as... (4 Replies)
Discussion started by: amitkumar.b2
4 Replies

8. Shell Programming and Scripting

Search file for string and store last result to variable

Hi, I'm trying to search a text file for a string: "energy(sigma->0)=". To do so, I executed the grep command, which returned many matches, for example: energy without entropy = -112.16486170 energy(sigma->0) = -112.16520778 energy without entropy = -112.16488936 ... (5 Replies)
Discussion started by: gwr
5 Replies

9. Shell Programming and Scripting

How to store query multiple result in shell script variable(Array)

:) Suppose,I have one table A. Table A have one column. Table A have 10 rows. I want this 10 rows store into shell script variable. like #!/bin/ksh v_shell_var=Hi here in call oracle , through loop How can I store table A's 10 rows into v_shell_var (Shell Script Array). Regards, Div (4 Replies)
Discussion started by: div_Neev
4 Replies

10. Shell Programming and Scripting

When can a script deny to store any value in a variable

In a shell script, I am storing some values into variables. In between the code I am calling an PL/SQL precedure by connecting to ORACLE which generates a file. All this happens fine. The problem is, the values are not getting stored in variables after this PL/SQL code while perfectly before... (1 Reply)
Discussion started by: mitte_dino
1 Replies
Login or Register to Ask a Question