![이 bash 스크립트가 작동하지 않는 이유는 무엇입니까(for 루프 내부의 변수 할당)?](https://linux55.com/image/222125/%EC%9D%B4%20bash%20%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8A%B8%EA%B0%80%20%EC%9E%91%EB%8F%99%ED%95%98%EC%A7%80%20%EC%95%8A%EB%8A%94%20%EC%9D%B4%EC%9C%A0%EB%8A%94%20%EB%AC%B4%EC%97%87%EC%9E%85%EB%8B%88%EA%B9%8C(for%20%EB%A3%A8%ED%94%84%20%EB%82%B4%EB%B6%80%EC%9D%98%20%EB%B3%80%EC%88%98%20%ED%95%A0%EB%8B%B9)%3F.png)
나는 bash 스크립팅을 처음 접했기 때문에 이것이 분명하다면 사과드립니다!
ID1.1.fq.stuff, ID1.2.fq.stuff, ID2.1.fq.stuff, ID2.2.fq 형식의 파일 묶음을 반복하는 bash 스크립트를 만들려고 합니다. 이 스크립트는 쌍을 이루는 파일(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 출력 반복
- 일치하지 않는 글로브 무시
- 잘못된 스팽
- 와일드카드와 단어 분리기를 방지하기 위해 큰따옴표를 사용하지 않습니다.
- 전역 일치를 방지하기 위해 == 오른쪽에 대한 참조가 없습니다.
#!/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