BASH脚本中的gzip能否在文件完全解压缩之前给出退出状态?如何预防呢?

时间:2019-03-11 15:06:20

标签: bash gzip exitstatus

我正在尝试编写一个脚本,以按顺序收集有关.fastq文件的一些统计信息。在我的脚本中,我以循环(gzip / gzip -d的形式解压缩和重新压缩每个文件,应用命令查找每个文件的统计信息,同时使用zip文件将其解压缩。 command "${file_name//.fastq.gz/.fastq}"

执行此操作时,脚本有时无法收集某些文件的统计信息(例如,行数为零)。但是,它似乎是随机执行的,并且不止一次运行脚本,有时它会收集统计信息,有时不会针对同一文件。

我认为这是因为gzip在文件完全解压缩之前正在返回退出状态,这意味着我的脚本会继续运行,有时会从半压缩的文件中收集统计信息。为此,我看到了针对同一文件返回的文件大小的不同统计信息(但对于诸如标题行计数之类的统计信息却没有,这似乎不是零,也不是我期望的值)。

是否有最佳方法告诉BASH等待文件完全解压缩?我尝试使用until [ -f "${file_name//.fastq.gz/.fastq}" ]until [ command "${file_name//.fastq.gz/.fastq}" != 0 ]的变体,但是我仍然不能总是以这种方式获得正确的结果(我已经通过解压缩文件并手动应用每个命令来进行检查),然后将值分别与脚本进行比较时间。

我在下面发布了脚本,并在四个文件上链接了一张图片,显示了两次不同脚本运行的输出,以突出显示此问题。作为记录,我一直在Sun Grid Engine上运行此脚本,并且没有返回任何错误消息。

#!/bin/bash
#$ -cwd
#$ -l h_rt=01:00:00
#$ -l h_vmem=1G
#$ -o fastq_qc_stats_job_out_file.txt
#$ -e fastq_qc_stats_job_error_file.txt

#First, make a file to store the output
if echo "${PWD}/" | grep -iq "/[a-Z]*_[0-9]*/"  # if in numbered batches (i.e. the data to be analysed is split across multiple numbered files)
then batch=`echo "${PWD}/" | grep -o "/[a-Z]*_[0-9]*/" |  cut -d '_' -f 2 | cut -d '/' -f 1` # get the batch number to name the output file,
else batch=`basename $PWD`; fi # otherwise just use the final part of the directory name.
header_line=`echo {'FILE','RUN','SIZE','READ_COUNT','MEAN_READ_LENGTH'} | sed 's/ /,/g'` # make a header line
echo $header_line > "QC_FASTQ_stats_${batch}.csv" # make a .csv file with the header line (by batch)
#Now loop through the FASTQ files and add the following information for each of them

for file in `ls *.fastq.gz`
do gzip -d $file # unzip the file
f="${file//.fastq.gz/.fastq}"
accession=`echo ${f} | cut -d '.' -f 1 | cut -d '_' -f 1`
filesize=`du -h ${f} | awk '{print $1}'`
readcount=`grep -E '^@[EDS]RR' ${f} | grep -E ' length=' | wc -l`
averagelength=`grep ' length=' ${f} | awk '{print $NF}' | cut -d '=' -f 2 | awk '{ total += $1 } END { print total/NR }'` # calculates mean
filestats=`echo $file $accession $filesize $readcount $averagelength | sed 's/ /,/g'`
echo $filestats >> "QC_FASTQ_stats_${batch}.csv" # add stats for each .fastq file to the .csv file
gzip ${f} # re-zip the file
done

An example of the output variation when run twice for the same files - see 4th file

0 个答案:

没有答案