본문 바로가기

Develop/Linux

쉘 스크립트 완전정복 - 4 | 조건문, 비교연산자

출처 : http://tbr74.tistory.com/10?category=759026


쉘 스크립트 완전정복 - 4 | 조건문, 비교연산자

2018.07.24 21:09

조건문 if (이중 분기)

if [ condition ]

then

echo "when the condition is true"

else

echo "when the condition is false"

fi


기본 형식은 위와 같다.

condition 사이에는 공백이 있어야한다.




조건문 case (다중 분기)

case "$1" in

start)

echo "start";;

stop)

echo "stop";;

restart)

echo "restart";;

*)

echo "?";;

esac


기본 형식은 위와 같다. 문법이 상당히 어색하니 주의.

$1값에 따라 다중 분기를 한다.

그 외의 값은 *) 다음을 실행한다.

$1는 파라미터 변수다. 이에대한 설명은 여깄다.


그나저나, esac를 case문의 종료키워드로 쓰다니. 처음엔 눈을 의심했다.

이렇게되면 if~fi 의 fi가 FInish 의 약어가 아니라 그냥 if를 뒤집은것...





비교연산자

[산술 비교]

-eq equal

-ne not equal

-le less than equal to (<=)

-lt less than (<)

-gt greater than (>)

-ge greater than equal to (>=)

등이 있는데 얘들은 이제 조건문 내에서 사용할 수 있는것들이지


[파일 비교(File test operators)]

이 부분이 쉘 스크립트를 처음 접하는 나한테는 조금 생소하다.

-e File exists

-d file is a directory

-b file is a block device

-c file is a character device

-p file is a pipe






---

참고서적: 뇌를 자극하는 RedHat Fedora 리눅스 서버&네트워크