You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让Bash脚本按顺序执行命令?附批量处理sorted.bam场景

Hey there! Let's get your Bash script sorted so it runs all those StringTie steps in perfect order. Here's a complete, sequential script plus breakdowns of how it ensures each task finishes before the next starts:

Complete Sequential Bash Script for StringTie Processing
#!/bin/bash

# Set your core variables first (update these paths to match your system!)
REF_ANNOT="gencode.v27.primary_assembly.annotation_nochr.gtf"
INPUT_BAM_DIR="/path"  # Folder holding all your .sorted.bam files
OUTPUT_GTF_DIR="/pathto/folder"  # Folder to store individual sample GTFs
MERGE_LIST="${OUTPUT_GTF_DIR}/mergelist.txt"

# Prep: Create output folder if it doesn't exist, clear old mergelist
mkdir -p "${OUTPUT_GTF_DIR}"
> "${MERGE_LIST}"  # Wipes existing mergelist to avoid old entries

# Step 1: Process each .sorted.bam file one at a time
for sample in "${INPUT_BAM_DIR}"/*.sorted.bam
do
    # Extract the sample name without the .sorted.bam suffix
    base=$(basename "${sample}" ".sorted.bam")
    output_gtf="${OUTPUT_GTF_DIR}/${base}.gtf"
    
    # Run StringTie for the current sample
    echo "Processing sample: ${base}"
    stringtie -p 8 -G "${REF_ANNOT}" -o "${output_gtf}" "${sample}"
    
    # Stop immediately if StringTie fails for this sample
    if [ $? -ne 0 ]; then
        echo "ERROR: StringTie failed for ${sample} — exiting script"
        exit 1
    fi
    
    # Add the finished GTF path to the mergelist
    echo "${output_gtf}" >> "${MERGE_LIST}"
done

# Step 2: Run StringTie merge ONLY after all samples are processed
echo "Starting StringTie merge of all GTFs..."
stringtie --merge -G "${REF_ANNOT}" -o "${OUTPUT_GTF_DIR}/stringtie_merged.gtf" "${MERGE_LIST}"

# Verify merge success
if [ $? -eq 0 ]; then
    echo "All steps completed successfully! Merged GTF is at ${OUTPUT_GTF_DIR}/stringtie_merged.gtf"
else
    echo "ERROR: StringTie merge failed"
    exit 1
fi

Why This Script Runs Sequentially

  • Bash’s default behavior: By design, Bash executes commands one after another—each stringtie run for a sample will finish completely before the script appends to mergelist.txt or moves to the next sample.
  • Error checking with $?: We added checks for the exit status of every stringtie command. If any sample fails to process, the script stops immediately, so you don’t end up with an incomplete mergelist or a failed merge later.
  • Explicit path handling: Using full, defined paths for all files ensures there’s no confusion about where outputs are stored, which guarantees the merge step can find every generated GTF.

Quick Notes

  • Update the placeholder paths (/path, /pathto/folder) to match your actual directory structure.
  • Adjust the -p 8 flag to match the number of CPU cores you want to allocate to StringTie.
  • The mkdir -p command creates your output folder if it doesn’t already exist, preventing "no such file or directory" errors.

内容的提问来源于stack exchange,提问作者beginner

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:14:04