如何使 bash 脚本与一个又一个命令一起工作?
How to make the bash script work with one command after another?
我有一个如下所示的 bash 脚本。首先,它将 sorted.bam 个文件作为输入,并使用 "stringtie" 工具将每个样本 gtf 作为输出。然后每个样本 gtf 的路径将被赋予 mergelist.txt。然后对它们使用 "stringtie merge" 得到 "stringtie_merged.gtf".
我总共有 40 个 sorted.bam 个文件。
for sample in /path/*.sorted.bam
do
dir="/pathto/hisat2_output"
dir2="/pathto/folder"
base=`basename $sample '.sorted.bam'`
"stringtie -p 8 -G gencode.v27.primary_assembly.annotation_nochr.gtf -o ${dir2}/stringtie_output/${base}/${base}_GRCh38.gtf -l ${dir2}/stringtie_output/${base}/${base} ${dir}/${base}.sorted.bam; ls ${dir2}/stringtie_output/*/*_GRCh38.gtf > mergelist.txt; stringtie --merge -p 8 -G gencode.v27.primary_assembly.annotation_nochr.gtf -o ${dir2}/stringtie_output/stringtie_merged.gtf mergelist.txt"
done
我用 ;
分隔命令 在 运行 所有 sorted.bam 文件的脚本之后,完成工作后我看到 mergelist.txt 只有 33 个样本的路径gtf的。这意味着其他 7 个示例 gtfs 的路径在合并中丢失 list.txt.
用 ;
分隔命令是正确的还是有其他方法?
脚本应该首先使用一个命令,输出需要在文本文件中给出路径,然后使用另一个命令。
多行有什么问题,因为你已经有不止一行了:
dir="/pathto/hisat2_output"
dir2="/pathto/folder"
for sample in /path/*.sorted.bam ;do
base=$(basename ${sample} '.sorted.bam')
stringtie -p 8 -G gencode.v27.primary_assembly.annotation_nochr.gtf -o ${dir2}/stringtie_output/${base}/${base}_GRCh38.gtf -l ${dir2}/stringtie_output/${base}/${base} ${dir}/${base}.sorted.bam
ls ${dir2}/stringtie_output/*/*_GRCh38.gtf > mergelist.txt
stringtie --merge -p 8 -G gencode.v27.primary_assembly.annotation_nochr.gtf -o ${dir2}/stringtie_output/stringtie_merged.gtf mergelist.txt
done
无论如何,我看不出在循环中使用第二个 stringtie 命令有什么意义,之后它应该可以正常工作。
如果 stringtie 能够处理 STDIN,您可以在没有 mergelist.txt 的情况下使用:
stringtie --merge -p 8 -G gencode.v27.primary_assembly.annotation_nochr.gtf -o ${dir2}/stringtie_output/stringtie_merged.gtf <<< $(echo ${dir2}/stringtie_output/*/*_GRCh38.gtf)
你应该双引号你的变量并使用 $( command )
而不是反引号
base=$( basename $sample '.sorted.bam' )
:
你有一个 space 文件名??
更喜欢:
base=$( basename "$sample.sorted.bam" ) # with or without space
如果你有spaces,你必须双引号:
stringtie -p 8 \
-G gencode.v27.primary_assembly.annotation_nochr.gtf \
-o "$dir2/stringtie_output/$base/$base_GRCh38.gtf" \
-l "$dir2/stringtie_output/$base/$base" \
"$dir/$base.sorted.bam"
ls "$dir2"/stringtie_output/*/*_GRCh38.gtf > mergelist.txt
...
你没有用semi-colons分隔命令;您调用了一个嵌入了 semi-colons 的命令。考虑简单的脚本:
"ls; pwd"
此脚本不调用ls
,然后调用pwd
。相反,shell 将搜索 PATH 以查找名为 ls; pwd
的文件(即名称中带有 semi-colon 和 space 的文件),可能找不到一个并以错误消息响应。您需要删除双引号。
我有一个如下所示的 bash 脚本。首先,它将 sorted.bam 个文件作为输入,并使用 "stringtie" 工具将每个样本 gtf 作为输出。然后每个样本 gtf 的路径将被赋予 mergelist.txt。然后对它们使用 "stringtie merge" 得到 "stringtie_merged.gtf".
我总共有 40 个 sorted.bam 个文件。
for sample in /path/*.sorted.bam
do
dir="/pathto/hisat2_output"
dir2="/pathto/folder"
base=`basename $sample '.sorted.bam'`
"stringtie -p 8 -G gencode.v27.primary_assembly.annotation_nochr.gtf -o ${dir2}/stringtie_output/${base}/${base}_GRCh38.gtf -l ${dir2}/stringtie_output/${base}/${base} ${dir}/${base}.sorted.bam; ls ${dir2}/stringtie_output/*/*_GRCh38.gtf > mergelist.txt; stringtie --merge -p 8 -G gencode.v27.primary_assembly.annotation_nochr.gtf -o ${dir2}/stringtie_output/stringtie_merged.gtf mergelist.txt"
done
我用 ;
分隔命令 在 运行 所有 sorted.bam 文件的脚本之后,完成工作后我看到 mergelist.txt 只有 33 个样本的路径gtf的。这意味着其他 7 个示例 gtfs 的路径在合并中丢失 list.txt.
用 ;
分隔命令是正确的还是有其他方法?
脚本应该首先使用一个命令,输出需要在文本文件中给出路径,然后使用另一个命令。
多行有什么问题,因为你已经有不止一行了:
dir="/pathto/hisat2_output"
dir2="/pathto/folder"
for sample in /path/*.sorted.bam ;do
base=$(basename ${sample} '.sorted.bam')
stringtie -p 8 -G gencode.v27.primary_assembly.annotation_nochr.gtf -o ${dir2}/stringtie_output/${base}/${base}_GRCh38.gtf -l ${dir2}/stringtie_output/${base}/${base} ${dir}/${base}.sorted.bam
ls ${dir2}/stringtie_output/*/*_GRCh38.gtf > mergelist.txt
stringtie --merge -p 8 -G gencode.v27.primary_assembly.annotation_nochr.gtf -o ${dir2}/stringtie_output/stringtie_merged.gtf mergelist.txt
done
无论如何,我看不出在循环中使用第二个 stringtie 命令有什么意义,之后它应该可以正常工作。
如果 stringtie 能够处理 STDIN,您可以在没有 mergelist.txt 的情况下使用:
stringtie --merge -p 8 -G gencode.v27.primary_assembly.annotation_nochr.gtf -o ${dir2}/stringtie_output/stringtie_merged.gtf <<< $(echo ${dir2}/stringtie_output/*/*_GRCh38.gtf)
你应该双引号你的变量并使用 $( command )
而不是反引号
base=$( basename $sample '.sorted.bam' )
:
你有一个 space 文件名??
更喜欢:
base=$( basename "$sample.sorted.bam" ) # with or without space
如果你有spaces,你必须双引号:
stringtie -p 8 \
-G gencode.v27.primary_assembly.annotation_nochr.gtf \
-o "$dir2/stringtie_output/$base/$base_GRCh38.gtf" \
-l "$dir2/stringtie_output/$base/$base" \
"$dir/$base.sorted.bam"
ls "$dir2"/stringtie_output/*/*_GRCh38.gtf > mergelist.txt
...
你没有用semi-colons分隔命令;您调用了一个嵌入了 semi-colons 的命令。考虑简单的脚本:
"ls; pwd"
此脚本不调用ls
,然后调用pwd
。相反,shell 将搜索 PATH 以查找名为 ls; pwd
的文件(即名称中带有 semi-colon 和 space 的文件),可能找不到一个并以错误消息响应。您需要删除双引号。