I would like to build a little helper function that can deal with fastq.gz and fastq.bz2 files.
I want to merge zcat and bzcat into one transparent function which can be used on both sorts of files:
zbzcat example.fastq.gz
zbzcat example.fastq.bz2
zbzcat() {
file=`echo $1 | `
## Not working
ext=${file##*/};
if [ ext == "fastq.gz" ]; then
exec gzip -cd "$@"
else
exec bzip -cd "$@"
fi
}
The extension extraction is not working correctly. Are you aware of other solutions
These are quite a lot of problems:
file=`echo $1 | `gives a syntax error because there is no command after|. But you don't need the command substitution anyways. Just usefile=$1.ext=${file##*/}is not extracting the extension, but the filename. To extract the extension useext=${file##*.}.$extbut the literal stringext.file.fastq.gz, then the extension isgz. So use the check$ext = gz. That the uncompressed files arefastqfiles is irrelevant to the function anyways.execreplaces the shell process with the given command. So after executing your function, the shell would exit. Just execute the command.By the way: You don't have to extract the extension at all, when using pattern matchting:
Alternatively, use
7z xwhich supports a lot of formats. Most distributions name the packagep7zip.