我对 bash 脚本还很陌生,所以如果这是显而易见的,我深表歉意!
我正在尝试创建一个 bash 脚本来遍历格式为 ID1.1.fq.stuff、ID1.2.fq.stuff、ID2.1.fq.stuff、ID2.2.fq 的一堆文件。该脚本旨在查找配对的文件(ID1、ID2 等的文件),然后将它们一起提交到名为 STAR 的程序进行下游处理。
我制作了以下 bash 脚本:
#/!/bin/sh
module load STAR
current_id = ""
current_file = ""
for fqfile in `ls path/*`; do
filename = ${fqfile%%.fq*}
id = ${filename%.*}
if $id == $current_id; then
STAR --readFilesIn $current_file $fqfile --outFileNamePrefix ./$id.bam
else
current_id = $id
current_file = $fqfile
fi
done
当我运行它时,我收到以下错误:
[path to $id, without file extensions]: No such file or directory
current_id: command not found
current_file: command not found
我究竟做错了什么?
谢谢你!
答案1
我使用了 bash 语法,因为问题被标记为巴什
原脚本有问题
- 迭代 ls 输出
- 忽略不匹配的 glob
- 不正确的施邦
- 没有使用双引号来防止通配和分词
- 没有引用 == 的右侧以防止全局匹配
#!/usr/bin/env bash
# Instructs bash to immediately exit if any command has a non-zero exit status
set -e
# Allows patterns which match no files to expand to a null string, rather than themselves
shopt -s nullglob
module load STAR
# Don't put spaces around '=' when assigning variables in bash.
current_id=""
current_file=""
# Iterating over ls output is fragile. Use globs
for fqfile in path/*.fq*; do
filename="${fqfile%%.fq*}"
id="${filename%.*}"
if [[ $id == "$current_id" ]]; then
STAR --readFilesIn "$current_file" "$fqfile" --outFileNamePrefix "./$id.bam"
else
current_id="$id"
current_file="$fqfile"
fi
done