命令的输出作为变量

Output of command as variable

我正在编写一个 PBS 脚本来一次处理多个文件并执行多个操作。

我想先解压缩 .gz 文件(我稍后将其压缩),然后处理解压缩的文件。

以下将解压 $file 中的文件,但一旦解压文件,我希望变量 $file 引用解压后的版本:

for file in $READS/*2.fq.gz;
do
    gunzip $file
    # continue script with the gunzipped file  
done

类似于:

for file in $READS/*2.fq.gz;
do
    file=gunzip $file
    # continue script with the gunzipped file  
done

您想删除 $file:

上的 .gz 后缀
file=${file%.gz}

请注意,您可能想检查 gunzip 命令的结果以查看它是否失败:

for file in $READS/*2.fq.gz;
do
    if gunzip $file; then
        file=${file%.gz}
        # continue script with the gunzipped file
    else
        # gunzip failed (corrupt file?) and already output an error message
        # try to recover?
    fi
done