'ShellScript'에 해당되는 글 1건

  1. 2010.05.23 [if연산] test 관련 연산자
2010. 5. 23. 11:37

■ 파일 관련 test 연산자

연산자

설명 (다음의 경우 )

-e file

파일이 존재하는 경우

-d file

파일이 존재하고 디렉토리인 경우

-f file

파일이 정규 파일인 경우

-b file

파일이 존재하고 블록 디바이스 파일인 경우

-c file

파일이 존재하고 문자 디바이스 파일인 경우

-p file

파일이 존재하고 파이프나 이름이 있는 파이프(FIFO 파일) 경우

-S file

파일이 존재하고 소켓인 경우

-L file

파일이 존재하고 심볼릭 링크인 경우

-r file

파일이 존재하고 읽을 있는 경우

-w file

파일이 존재하고 쓰기 가능한 경우

-x file

파일이 존재하고 실행할 있는 경우

-u file

파일이 존재하고 SetUID 비트가 설정된 경우

-g file

파일이 존재하고 SetGID 비트가 설정된 경우

-k file

파일이 존재하고 Sticky 비트가 설정된 경우

-s file

파일이 존재하고 파일이 아닌 경우

fileA -nt fileB

fileA fileB보다 나중에 생성된 경우

fileA -ot fileB

fileA fileB보다 이전에 생성된 경우

fileA -ef fileB

fileA fileB 같은 파일을 가리키고 있는 경우

 

■ 문자열 관련 test 연산자

연산자

설명

stringA = stringB

stringA stringB 같은 경우

stringA != stringB

stringA stringB 같지 않은 경우

stringA < stringB

stringA stringB보다 사전순서로 먼저 나오는 경우

stringA > stringB

stringA stringB보다 사전순서로 나중에 나오는 경우

-z string

string 길이가 0인경우

-n string

string NULL 아닌 경우

 

■ 산술연산 test 구문

연산자

설명

exprA -eq exprB

산술연산 exprA exprB 같은 경우(-eq: equal)

exprA -ne exprB

산술연산 exprA exprB 같지 않은 경우(-n: not equal)

exprA -lt exprB

산술연산 exprA exprB보다 작은 경우(-lt: less than)

exprA -le exprB

산술연산 exprA exprB보다 작거나 같은 경우(-le: less equal)

exprA -gt exprB

산술연산 exprA exprB보다 경우(-gt: great than)

exprA -ge exprB

산술연산 exprA exprB보다 크거나 같은 경우(-ge: great equal)

exprA -a exprB

exprA 참이고 exprB 참인경우(-a: and)

exprA -o exprB

exprA 참이거나 exprB 참인경우(-o: or)

 


test 구문에서 사용되는 연산자

 ① 논리 부정 연산자(NOT)

다른 연산자와 함께 사용되며 파일 test에 이용된다.

if [ ! -s file ] ; then

echo "file size is zero"

fi


② 괄호
(Grouping)

실행 순서를 변경하기 위해 사용하며 괄호의 양쪽에는 공백이 필요하다.
그리고 의미없는 순수한 괄
호의 의미를 사용하기 위해 괄호 앞에 백슬래쉬를 넣는다.

if find / ( -perm -4000 ) -o ( -perm -2000 ) -type f > /dev/null ; then

echo "SetUID or SetGID Found"

fi


③ 논리곱 연산자
(AND)와 논리합 연산자(OR)

fi [ ( "$VAR" -ge 0 ) -a ( "$VAR" -lt 10 ) ] ; then

echo "$VAR is small number"

fi

if [ -n "$str" -o -r $HOME/.profile ] ; then

echo "Hello, Test."

fi


④ 연산자 우선 순위

괄호 > 논리부정연산자(NOT) > 논리곱 연산자(-a) > 논리합 연산자(-o)

'Linux > ShellScript' 카테고리의 다른 글

ANSI Escape(ESC) character  (0) 2010.11.02
쉘내부 명령어  (0) 2010.10.28
[script]directory tree로 보여주기  (0) 2010.05.29
[Script] 파일 점검 프로그램  (0) 2010.05.23
쉘(Shell) 환경  (0) 2010.05.16
Posted by devanix