justaguy
justaguy

Reputation: 3022

bash to verify file type integrity and create log

I am trying to use bash to verify the integrity of specific downloads .bam files. There are two parts (bash 1) runs the command to verify the .bam files which creates .txt files, and creates a process.log. That part works perfect, what I am getting an error in is checking each of the .txt files for a string (SUCCESS) and if it is found then in the process.log that file is verified if it is not found then that file is corrupt. Currently the terminal displays the status and then gives an error. Thank you :).

bash part 1

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"
 echo "Start verifying $(date) - File: $file"
 value=$( grep -ic "(SUCCESS)" /home/cmccabe/Desktop/NGS/API/5-4-2016/bam_validation/*.txt )

bash part 2

 if [ $value -eq 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"

erorr

Start verifying Thu May  5 12:49:10 CDT 2016 - File: 
/home/cmccabe/Desktop/loop.sh: line 11: [: too many arguments
bam is corrupt, check log for reason
End bam verify: Thu May  5 12:49:10 CDT 2016 - File: /home/cmccabe/Desktop/NGS/API/5-4-2016/NA19240.bam
/home/cmccabe/Desktop/loop.sh: line 18: syntax error near unexpected token `done'
/home/cmccabe/Desktop/loop.sh: line 18: `done >> "$logfile"'

file that is created to check for SUCCESS

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)

Upvotes: 0

Views: 420

Answers (1)

Inian
Inian

Reputation: 85570

The error is due to simplest of the reasons, in bash if you use a single parentheses [] to evaluate a condition, it is nothing but an implicit way of using the bash test expression which expands the $value as a string containing spaces, special characters as separate parameters. Here you have left the variable ambiguous which could be expanded to multiple parameters for some error cases.

All you need to do is enclose that variable within double quotes, so that it is treated as one single string.

if [ "$value" == 1 ]; then

Upvotes: 1

Related Questions