개발/Linux
Shell Script 연산자
테리
2023. 5. 3. 13:36
막상 쉘 스크립트를 작성해 보면 혼돈이 되는 비교 연산만 정리
- 문자열 비교
[ str ] - str이 null이 아니면 true
[ -n str ] - str의 사이즈가 0 이상이면 true
[ -z str ] - str의 사이즈가 0이면 true, null 체크
[ stra = strb ] - stra와 strb가 같으면 true
[ stra != strb ] - stara와 strb가 다르면 true - 비교 연산
[ idx -ge 100 ] - idx가 100보다 크면 true
[ idx -gt 100 ] - idx가 100보다 크거나 같으면 true
[ idx -le 100 ] - idx가 100보다 작으면 true
[ idx -lt 100 ] - idx가 100보다 작거나 같으면 true - 파일 비교
[ -r filename ] - filename이 읽기 가능하면 true
[ -w filename ] - filename이 쓰기 가능하면 true
[ -x filename ] - filename이 실행 가능하면 true
[ -s filename ] - filename의 사이즈가 0 이상이면 true
[ -d filename ] - filename이 디렉토리 형식이면 true
[ -f filename ] - filename이 파일 형식이면 true
[ -h filename ] - filename이 링크 파일 형식이면 true - 조건문 결합
[ cond1 -a cond2 ] - cond1과 cond2 모두 true이면 true
[ cond1 -o cond2 ] - cond1과 ocnd2 중 하나라도 true이면 true