본문 바로가기
Programming/shell

[shell] 파일, 디렉토리 존재유무 체크

by ciwhiz 2020. 5. 29.

아래 접은 글이 사용하던 shell 샘플.

더보기
#!/bin/bash 

echo "start Release Build!!!------------------" 

if [ -f "/경로이름/." ]; then   #뭐든 파일이 있는지 검사하기 위해 . 
  echo "file exist"    
else 
  echo "file not exist!!" 
  git submodule init && git submodule update 
fi 

cd 경로

git checkout 브랜치이름

git pull 

cd .. 

./gradlew prepare && ./gradlew clean assemble   # 이건 프로젝트 빌드스크립트명령어..

echo "end command build" 
exit 0

test 나 [ ](대괄호)를 사용해서 파일존재 유무를 체크할 수 있음.

if test -e 파일명 
if ! test -e 파일명 

디렉토리는 -d를 사용하면 됨.

if test -e 디렉토리명 
if ! test -e 디렉토리명 

[] 를 사용하면, 마찬가지지만 주의할 점은, 띄어쓰기!!, 대괄호 시작 뒤에 공백 한 칸이 필수!!

if [ -d 파일명 ]