리눅스의 쉘스크립트에서 if를 사용하는 방법에 대해서 알아보도록 하겠습니다.
드디어 if 사용법에 대한 부분입니다.
두번째 포스팅에서 제가 급한 마음에 while을 먼저 사용해 보기는 하였는데, 지금 생각하면 지금 설명하는 if의 사용법과 if,while등과 같은 흐름제어를 이해하기 위해서, 그전에 exit, test 명령어를 먼저 이해를 하고 while을 접하는게 더 좋지 않았을까 생각이 듭니다..
우선 exit와 test에 관한 설명과 사용법에 대해서 알아 보겠습니다.
1.exit
exit의 man페이지에 설명을 보면 아래와 같습니다.
Exit the shell.
Exits the shell with a status of N. If N is omitted, the exit status
is that of the last command executed.
즉, 마지막 커맨드가 실행된 뒤에 받는 상태값입니다. exit의 값은 숫자로 받게되는데, 커맨드가 성공적으로 실행이 되었을 때에는 '0'이 되게 됩니다.
exit값은 "echo $?" 로 확인이 가능합니다.
아래처럼 커맨드(pwd)가 정상적으로 실행되면 exit값은 '0'이 되고, 커맨드(ls /hello)가 실패하게 되면 '0'이 아닌 값이 됩니다.
$pwd
/test/gh
$echo $?
0
$ls /hello
ls: cannot access /hello: No such file or directory
$echo $?
2
$
더 간단하게 true,false를 실행하여 exit의 값을 확인할 수 도 있습니다.
$true
$echo $?
0
$false
$echo $?
1
$
2. test
test의 man페이지의 설명을 보면 아래와 같습니다.
Evaluate conditional expression.
Exits with a status of 0 (true) or 1 (false) depending on
the evaluation of EXPR. Expressions may be unary or binary. Unary
expressions are often used to examine the status of a file. There
are string operators as well, and numeric comparison operators.
즉, 값들을 비교하여 exit값을 0 또는 1을 되돌려주는 명령어입니다.
사용방법은 test expression 또는 [ expression ] 형태로 사용됩니다. 2번째 방식을 사용할 때에는 [와] 앞뒤로 스페이스공간이 있어야 합니다.
따라서 [a=b]는 틀린 표현이고, [ a=b ] 로 표현하여야 합니다...
test는 if나 while에서 항상 사용되게 됩니다.
아래 커맨드들을 보시면 if와 test에 관해서 잘 이해를 할 수 있습니다.
즉,if는 exit값이 0일 경우에 then이후의 명령어를 실행한다는 것을 알 수 있습니다.
'[ 1 = 1 ]', 'test 1 =1' 그리고 'true' 일 경우의 결과가 모두 동일합니다.
$if true; then echo "it's true"; fi
it's true
$if [ 1 = 1 ]; then echo "it's true"; fi
it's true
$if test 1 = 1; then echo "it's true"; fi
it's true
$if false; then echo "it's true"; fi
$if [ 1 != 1 ]; then echo "it's true"; fi
$if test 1 != 1; then echo "it's true"; fi
test의 사용에 관한 비교문법은 아래와 같습니다.
File operators:
-a FILE True if file exists.
-b FILE True if file is block special.
-c FILE True if file is character special.
-d FILE True if file is a directory.
-e FILE True if file exists.
-f FILE True if file exists and is a regular file.
-g FILE True if file is set-group-id.
-h FILE True if file is a symbolic link.
-L FILE True if file is a symbolic link.
-k FILE True if file has its `sticky' bit set.
-p FILE True if file is a named pipe.
-r FILE True if file is readable by you.
-s FILE True if file exists and is not empty.
-S FILE True if file is a socket.
-t FD True if FD is opened on a terminal.
-u FILE True if the file is set-user-id.
-w FILE True if the file is writable by you.
-x FILE True if the file is executable by you.
-O FILE True if the file is effectively owned by you.
-G FILE True if the file is effectively owned by your group.
-N FILE True if the file has been modified since it was last read.
FILE1 -nt FILE2 True if file1 is newer than file2 (according to
modification date).
FILE1 -ot FILE2 True if file1 is older than file2.
FILE1 -ef FILE2 True if file1 is a hard link to file2.
All file operators except -h and -L are acting on the target of a symbolic
link, not on the symlink itself, if FILE is a symbolic link.
String operators:
-z STRING True if string is empty.
-n STRING
STRING True if string is not empty.
STRING1 = STRING2
True if the strings are equal.
STRING1 != STRING2
True if the strings are not equal.
STRING1 < STRING2
True if STRING1 sorts before STRING2 lexicographically.
STRING1 > STRING2
True if STRING1 sorts after STRING2 lexicographically.
Other operators:
-o OPTION True if the shell option OPTION is enabled.
! EXPR True if expr is false.
EXPR1 -a EXPR2 True if both expr1 AND expr2 are true.
EXPR1 -o EXPR2 True if either expr1 OR expr2 is true.
arg1 OP arg2 Arithmetic tests. OP is one of -eq, -ne,
-lt, -le, -gt, or -ge.
Arithmetic binary operators return true if ARG1 is equal, not-equal,
less-than, less-than-or-equal, greater-than, or greater-than-or-equal
than ARG2.