Bash executes first part of script but not second


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash executes first part of script but not second
# 1  
Old 05-05-2016
Bash executes first part of script but not second

My bash below verifies the integrity of all .bam in a directory and writes the out output to a .txt file. That is part one of the script that works. The second part of the bash searches each one of the .txt files for a string "(SUCCESS)" and if found display a message and if it is not found displays a different message.process.log created in the first bash. Currently, the script executes the first bash but stalls and does not end, it never closes executing the second bash. I can not seem to fix it and need some expert help. Thank you very much Smilie.

Bash part one (working as expected)
Code:
logfile=/home/cmccabe/Desktop/NGS/API/5-4-2016/process.log
for f in /home/cmccabe/Desktop/NGS/API/5-4-2016/*.bam ; do
 echo "Start bam validation creation: $(date) - File: $f"
 bname=`basename $f`
 pref=${bname%%.bam}
 bam validate --in $f --verbose 2> /home/cmccabe/Desktop/NGS/API/5-4-2016/bam_validation/${pref}_validation.txt
 echo "End bam validation creation: $(date) - File: $f"
done >> "$logfile"

Bash part two (doesn't work as expected)
Code:
For file in /home/cmccabe/Desktop/NGS/API/5-4-2016/bam_validation/*.txt; do
echo "Start verifying $(date) - File: $file"
value=$( grep -ic "(SUCCESS)" )
if [ "$value" == 1 ]; then
 echo "bam file is verified and complete"
 else
 echo "bam is corrupt, check log for reason"
 echo "End bam verify: $(date) - File: $f"
 fi
 done >> "$logfile"

example of .txt file
Code:
Number of records read = 24723078
Number of valid records = 24723078

TotalReads(e6)  24.72
MappedReads(e6) 24.57
PairedReads(e6) 0.00
ProperPair(e6)  0.00
DuplicateReads(e6)  7.33
QCFailureReads(e6)  0.00

MappingRate(%)  99.38
PairedReads(%)  0.00
ProperPair(%)   0.00
DupRate(%)  29.66
QCFailRate(%)   0.00

TotalBases(e6)  4332.46
BasesInMappedReads(e6)  4325.68
Returning: 0 (SUCCESS)


Last edited by cmccabe; 05-05-2016 at 10:13 PM.. Reason: fixed format
# 2  
Old 05-05-2016
First, the line:
Code:
For file in /home/cmccabe/Desktop/NGS/API/5-4-2016/bam_validation/*.txt; do;

needs to be changed to:
Code:
for file in /home/cmccabe/Desktop/NGS/API/5-4-2016/bam_validation/*.txt; do

Then, note that the command:
Code:
value=$( grep -ic "(SUCCESS)" )

is reading standard input looking for the string (SUCCESS). Maybe you intended to use:
Code:
value=$( grep -ic "(SUCCESS)" "$file" )

This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 05-06-2016
Quote:
Originally Posted by cmccabe
My [ICODE][...]

Bash part one (working as expected)
Code:
logfile=/home/cmccabe/Desktop/NGS/API/5-4-2016/process.log
for f in /home/cmccabe/Desktop/NGS/API/5-4-2016/*.bam ; do
 echo "Start bam validation creation: $(date) - File: $f"
 bname=`basename $f`
 pref=${bname%%.bam}
 bam validate --in $f --verbose 2> /home/cmccabe/Desktop/NGS/API/5-4-2016/bam_validation/${pref}_validation.txt
 echo "End bam validation creation: $(date) - File: $f"
done >> "$logfile"

Bash part two (doesn't work as expected)
Code:
For file in /home/cmccabe/Desktop/NGS/API/5-4-2016/bam_validation/*.txt; do
echo "Start verifying $(date) - File: $file"
value=$( grep -ic "(SUCCESS)" )
if [ "$value" == 1 ]; then
 echo "bam file is verified and complete"
 else
 echo "bam is corrupt, check log for reason"
 echo "End bam verify: $(date) - File: $f"
 fi
 done >> "$logfile"

[...]
Also, notice that in your working snippet $f was the name of the file in the loop. In your refactored version, the variable you chose for a file is file but the $f has not been substituted.

Another version could be:
Code:
    if $(grep -iq "(SUCCESS)" "${doc}"); then
        echo "The verification of the bam file is completed."
    else
        echo "The bam file is corrupted. Check log for reason."
        echo "End of bam file verification: $(date) - File: ${doc}"
    fi

This User Gave Thanks to Aia For This Post:
# 4  
Old 05-06-2016
You don't use your variable file. Shouldn't it be an argument for grep?

BTW, it doesn't make much sense if you just say "the script doesn't work". You need to explain, in which way it doesn't work.
This User Gave Thanks to rovf For This Post:
# 5  
Old 05-06-2016
I will try out those changes and I apologize for not explaining "the script doesn't work". The bash executes and creates the .txt files but when each .txt file is searched the bash stalls. There is no error it just does not finish completely. I will change the script, execute, and post back. Thank you very much Smilie.
# 6  
Old 05-06-2016
Quote:
Originally Posted by cmccabe
I will try out those changes and I apologize for not explaining "the script doesn't work". The bash executes and creates the .txt files but when each .txt file is searched the bash stalls. There is no error it just does not finish completely. I will change the script, execute, and post back. Thank you very much Smilie.
It probably stalls, because it is waiting for input from standard input, since you did not provide the file name in the grep command, per Don's note.
This User Gave Thanks to gandolf989 For This Post:
# 7  
Old 05-06-2016
Thank you all for your help, everything works now Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove original file from directory after bash executes

The below bash works great, except I can not seem to delete the original file $f from the directory. Thank you :) For example, after the bash executes there are 8 files in the directory: 123.txt (original file) 123_remove.txt 123_index.txt 123_final.txt 456.txt (original file)... (11 Replies)
Discussion started by: cmccabe
11 Replies

2. Shell Programming and Scripting

Will shell script executes in sequence

I have a shell script scheduled in cron job to run at every 1 minute which transfers files to remote machine and then move the files to backup folder. cd /u01/app/ftp_tmp sftp user@hostname <<-EOF cd /home/user/ftp mput * bye EOF mv /u01/app/ftp_tmp/* /u01/app/ftp_bkp Now the problem is... (6 Replies)
Discussion started by: Bhavi
6 Replies

3. Shell Programming and Scripting

Killing a bash process and running the second part of script

I want to run a script that calls remote ssh and if it gets hung, I want to be able to kill that part of the script and run another command in the script for example I have a statement like this: if ]; then export tapes=$(for tape in $(su - nacct -c 'ssh remote1 "cat... (1 Reply)
Discussion started by: newbie2010
1 Replies

4. UNIX for Dummies Questions & Answers

Script partially executes??

Hi All, I am calling a shell script from another shell script, however, it only executes part of it (the echo commands only). What could be some causes for that? For example: ShellScriptA.sh: ... ... ... . ShellScriptB.sh ShellScriptB.sh contents: echo date echo... (7 Replies)
Discussion started by: DBnixUser
7 Replies

5. Shell Programming and Scripting

script executes some time but not always.

I have following script to send email when a USB is attached. #!/bin/bash NUMBER=`/bin/cat /u/numberoflines` LINES=`/usr/bin/wc -l < /var/log/messages` DIFFERENCE=$(($LINES-$NUMBER)) if ; then tail -n $DIFFERENCE /var/log/messages |while read line do if $( echo $line | grep --quiet... (2 Replies)
Discussion started by: kashif.live
2 Replies

6. Shell Programming and Scripting

FTP script - 'quit' never executes

I have wrote a script to get a very large encrypted file from a remote ftp server to a local debian box. The file downloads successfully, but the script never exits, or quits. None of code executes after the ftp get command. The file is approx 291M Here is the code: !/bin/sh... (3 Replies)
Discussion started by: jstrahm
3 Replies

7. Windows & DOS: Issues & Discussions

Batch script executes twice

Hi, Batch script gets executed without any error, but on execution some of the partial contents of the batch file gets appended at the end of the file which is currently in execution, hence the script tries to execute again for the second time , which should not happen. How to get it... (5 Replies)
Discussion started by: milink
5 Replies

8. Shell Programming and Scripting

My script executes too long than expected

Below is my script code which filters failed logins from existing and non-existing users from the server. The problem is, it takes longer to execute and complete than expected. Anyone here can edit the script to speed up its execution. #!/bin/bash LIST=`cat $1` for i in $LIST do... (10 Replies)
Discussion started by: linuxgeek
10 Replies

9. Shell Programming and Scripting

root executes a script as another user

Hi All, Am using the below command to start my application using the root user su - bin -c "/home/bin/test/start.sh" but am getting the error becaue i have set some environment varibales in bin's .profile when i execute the command start.sh by logging directly into bin account it's... (9 Replies)
Discussion started by: ravi.sri24
9 Replies

10. Shell Programming and Scripting

root executes a script as another user

we have this script that stops, starts and monitor process scheduler. prcs_control. this script runs perfectly when executed by ps_user. we are now creating a new script that will run this script and is executed by root. this script needs to execute the prcs_control as ps_user because root can... (1 Reply)
Discussion started by: tads98
1 Replies
Login or Register to Ask a Question