处理unix循环中的路径

时间:2016-03-30 22:12:55

标签: loops unix path samtools

相对unix新手。我有许多目录(Sample*/),我想在其中使用samtools合并所有raw.sort.bam文件。我有工作代码在每个目录中执行此操作,但我想通过运行父目录中的代码立即处理所有目录。我的问题是我被迫用一个完整的路径调用samtools,而我在查找这个路径在unix循环中的工作方式时遇到了麻烦。

首先,这是我在每个目录中合并和转换的工作代码:

/home/user/pathtosamtools/sam merge -o all.sort.bam *raw.sort.bam

现在,我的非工作代码在从父目录运行时尝试对所有目录执行此操作:

for f in `ls Sample*/`; do /home/user/pathtosamtools/sam merge -o $f all.sort.bam Sample*/*raw.sort.bam; done

错误:

[bam_merge_core_ext] fail to open file all.sort.bam
[bam_header_read] bgzf_check_EOF: Invalid argument
[bam_header_read] invalid BAM binary header (this is not a BAM file).
Segmentation fault

提前致谢。

1 个答案:

答案 0 :(得分:0)

Don't parse ls

for dir in Sample*/; do 
    /home/user/pathtosamtools/sam merge -o "$dir/all.sort.bam" "$dir"/*raw.sort.bam
done

你在for循环中提供了glob模式。

为安全起见,always quote your variables

相关问题