쉘 스크립트 작성(2)

Developer/Linux 2019. 6. 16. 16:26 Posted by JAKE_SLEEPY
명령 실행

if-then 구문 사용


if command
then
    command set
fi


if문은 그 줄에 정의된 명령을 실행한다. 

이때 명령이 제대로 실행이 되었다면 반환값은 0이다.

하지만, 명령이 제대로 실행되어지지 않았다면 반환값은 0이 아닌 수이다.




#!/bin/bash

if mkdir my_dir
then
    echo "my directory is created! 1"
    echo "my directory is created! 2"
    echo "my directory is created! 3"
fi



if-then-else 구문 사용


if command
then
    command set 1
else
    command set 2
fi

#!/bin/bash

if mkdir my_dir
then
    echo "my directory is created! 1"
    echo "my directory is created! 2"
    echo "my directory is created! 3"
else
    ls
    echo "my directory is already created!"
fi


if-then-elif 구문 사용


if command 1
then
    command set 1
elif command 2
then
    command set 2
else
    command set 3
fi



elif 문을 쓰면 바로 뒤에 나오는 else 문은 앞에 나온 if-then 구문 코드 블록에 속한 것이 아닌 elif 문에 속한 것!

즉, elif에 대한 else를 의미.


if command1
then
    command set 1
elif command2
then
    command set 2
elif command3
then
    command set 3
elif command4
then
    command set 4
fi



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

쉘 스크립트 작성(4)  (0) 2019.06.18
쉘 스크립트 작성(3)  (0) 2019.06.16
쉘 스크립트 작성  (0) 2019.06.16
쉘 스크립트 파일 만들기  (0) 2019.06.09
변수 배열  (0) 2019.06.09