린아저씨의 잡학사전




1. 파일의 존재 여부 확인하기

test 명령어 -e 옵션 사용  /// test 명령어를 대괄호[] 로 대체 가능
확인할 파일 : /tmp/my_test_file.txt

#!/bin/bash

file_path='my_test_file.txt'

#파일이 있을 경우 메시지 출력
if test -e $file_path; then
  echo "$file_path : Found the file."
fi

-------------------------------

#파일이 없을 경우 메시지 출력
if ! test -e $file_path; then
  echo "$file_path : Not found the file."
fi

-------------------------------

#대괄호[] 사용
if [ -e $file_path ]; then
  echo "$file_path : Found the file."
fi

 

 

2) 디렉터리 존재 여부 확인하기

 

test 명령어 -d 옵션 사용  /// test 명령어를 대괄호[] 로 대체 가능

확인할 디렉터리 : /tmp/my_test_dir

#!/bin/bash

dir_path='my_test_dir'

#파일이 있을 경우 메시지 출력
if test -d $dir_path; then
  echo "$dir_path : Found the directory
fi

-------------------------------

#파일이 없을 경우 메시지 출력
if ! test -d $dir_path; then
  echo "$dir_path : Not found the directory."
fi

-------------------------------

#대괄호[] 사용
if [ -d $dir_path ]; then
  echo "$dir_path : Found the directory."
fi

 

당신에게 추천하는 콘텐츠

공유하기

facebook twitter kakaoTalk kakaostory naver band