프로그래밍/Linux

shell script 작성법 - if문 사용법 (기본 사용법)

kugancity 2018. 11. 12. 11:02
반응형


기본 사용법 




if [ 조건식 ]; then          // if문 시작

    ~

elif

    ~

else

    ~

fi    


또는 



if [ 조건식 ]

then                    // if문 시작

    ~

elif

    ~

else

    ~

fi                         // if문 종료




if 와 [ 사이는 반드시 한 칸을 띄어써야 한다. 


조건식에 들어가는 조건은 &&(and) 나 ||(or) 로 연결하여 사용 가능하다 



다양한 조건을 연결하여 사용하는 예시 


 if [ $is_sorted_file -eq 0 ] && [ $today_cnt -gt 0 ] && [ $today_cnt_sorted -eq 0 ]; then

         echo "there is new today news:"$entry
 elif [ $is_sorted_file -gt 0 ]; then
                echo "this file is sorted file:"$entry
        elif [ $today_cnt -eq 0 ]; then
                echo "there is no today news:"$entry
        elif [ $today_cnt_sorted -gt 0 ]; then
                echo "new news is already added in sorted file"
        else
                echo "what case?"
    fi



문자열 비교




if [ "$var" != "" ]; then

echo variable is not null

fi


To compare the contents of a variable to a fixed string:


if [ "$var" == "value" ]; then

echo is the same

fi


To determine if variable’s contents are not equal to a fixed string:


if [ "$var" != "value" ]; then

echo not the same

fi


728x90
반응형