본문으로 건너뛰기

쉘 스크립트 if 조건변수

· 약 2분

쉘 스크립트의 비교 변수 몇 가지를 알아보자.

연산자기능예시
-afile_exists[ -a /etc/passwd ]
-dfile_exists && is_dir[ -d /etc ]
-ffile_exists && is_file[ -f /etc/passwd ]
-sfile_exists && not empty[ -s /etc/passwd ]
-w해당 유저로 쓰기 가능[ -w test.txt ]
-x해당 유저로 실행 가능[ -x test.sh ]
-N마지막 파일 읽은 시점부터 변경점이 있는지[ -N test.txt ]
-O해당 유저의 파일인지[ -O test.txt ]
-G해당 그룹의 파일인지[ -G test.txt ]
-ntB파일보다 A파일이 새로운지[ A_file -nt B_file ]
-otB파일보다 A파일이 오래됬는지[ A_file -ot B_file ]

숫자 비교

연산자기능예시
-ltLess than[ 0 -lt 1 ]
-leLess than or Equal[ 1 -le 1 ]
-eqEqual[ 1 -eq 1 ]
-gtGreater than[ 1 -gt 0 ]
-geGreater than or Equal[ 1 -ge 1 ]
-neNot Equal to[ 1 -ne 0 ]