이 bash 스크립트가 작동하지 않는 이유는 무엇입니까(for 루프 내부의 변수 할당)?

이 bash 스크립트가 작동하지 않는 이유는 무엇입니까(for 루프 내부의 변수 할당)?

나는 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

관련 정보