IT/shell script, cmd
[Shell script/cmd] path 폴더 존재 유무 체크 확인 script
[하늘이]
2021. 7. 20. 00:01
반응형
Director 존재 여부 확인 shell script
파일 체크 script option : https://tldp.org/LDP/abs/html/fto.html
* "/path/to/dir" directory 가 존재하는 경우 echo 구문이 수행된다.
[ -d "/path/to/dir" ] && echo "Directory /path/to/dir exists."
* ! 을 붙여 부정을 체크한다. "/path/to/dir" directory 가 존재하지 않는 경우 echo 구문 출력.
[ ! -d "/path/to/dir" ] && echo "Directory /path/to/dir DOES NOT exists."
* 위 두개를 조합하면 아래와 같다.
[ -d "/path/to/dir" ] && echo "Directory /path/to/dir exists." || echo "Error: Directory /path/to/dir does not exists."
존재하는 경우 첫번째 echo 구문이 존재하지 않으면 두번째 echo 구문이 출력된다.
* 위 구문을 if 로 변경 시 아래와 같다.
if [ -d "/path/to/dir" ]
then
echo "Directory /path/to/dir exists."
else
echo "Error: Directory /path/to/dir does not exists."
fi
반응형