사용자 도구

사이트 도구


linux:shell
#!/bin/bash
 
# 디렉토리 존재 유무 확인
if [ ! -d 디렉토리명 ] ; then
  mkdir 디렉토리명
fi
 
# 파일 존재 유무 확인
if [ ! -e 디렉토리명 ] ; then
  touch 파일명
fi
 
if test -f "manage.py"; then
  echo ls -l manage.py
fi
 
file_name="my_file.txt"
# 파일이 있을 경우 메세지 출력
if test -e $file_name
  then echo "$file_name 파일을 찾았습니다."
fi
 
# 파일이 없을 경우 메세지 출력(! 사용)
if ! test -e $file_name
  then echo "$file_name 파일이 없습니다."
fi
 
# 디렉토리의 존재 여부는 -d 옵션을 사용하면 된다.
 
dir_name="/var/log/"
# 디렉토리가 있을 경우 메세지 출력
if test -d $dir_name
  then echo "$dir_name 디렉토리를 찾았습니다."
fi
 
# test 명령은 대괄호([ ])로 대체해서 쓸 수도 있다. 바로 위의 if 조건문 예시를 아래과 같이 써도 된다.
 
if [ -d $dir_name ]
  then echo "$dir_name 디렉토리를 찾았습니다."
fi
linux/shell.txt · 마지막으로 수정됨: 2025/04/15 10:05 저자 127.0.0.1