워라밸 중독자

[Shell script] 파이프라인 작성시 쉘 스크립트에서 안전하게 config 변수 불러오기 본문

카테고리 없음

[Shell script] 파이프라인 작성시 쉘 스크립트에서 안전하게 config 변수 불러오기

Ufungi 2025. 3. 25. 09:35

현재 shell script에서 여러 하위 프로그램을 불러와서 사용할때

하위 프로그램의 변수를 고치고 싶다면

현재 shell script에서 config.yaml을 아래와 같이 전역 변수로 불러온다.

# Set the path to the config.yaml file as a global variable
export CONFIG_GLOBAL="절대경로/config.yaml"

# Load parameters from config file
echo "Loading parameters from config.yaml"
eval $(yq -r 'to_entries | .[] | .key + "=\"" + (.value | tostring) + "\""' ${CONFIG_GLOBAL} | sed -E 's/\(([^)]+)\)/$\1/g')

 

그리고 아래의 하위 프로그램에서 필요한 변수만 가져다 쓴다

# Load organism variable from config.yaml
organism=$(grep '^organism:' "$CONFIG_GLOBAL" | awk '{print $2}')

# Refine MSA based on organism type
if [[ "$organism" == "fungi" ]]; then
  mafft --globalpair --adjustdirection --maxiterate 3 --thread 2 "${ALN}" > shrunk.aln.bt
else
  mafft --adjustdirection --maxiterate 3 --thread 2 "${ALN}" > shrunk.aln.bt
fi

 

만약 config.yaml에 있는 모든 변수를 전역변수로 불러오게되면, 하위프로그램에서 사용하는 변수랑 꼬일 위험이 높다.

따라서 config만 전역변수로 불러오는 것이 안전하다.