반응형
Notice
Recent Posts
Recent Comments
Link
Today
Total
07-05 05:44
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Archives
관리 메뉴

iOS 개발 기록 블로그

Linux if문 사용하기 본문

Linux

Linux if문 사용하기

crazydeer 2022. 3. 22. 16:58
반응형

if문

if [ 조건 ]; then

  실행문

elif [ 조건 ]; then

  실행문

else

  실행문

fi

 

주의사항)

공백 처리를 잘해줘야 한다

 

문자열 비교)

"문자열1" = "문자열2" 두 문자열이 같으면 TRUE
"문자열1" != "문자열2" 두 문자열이 같지 않으면 TRUE

 

숫자 비교

숫자1 -eq 숫자2 두 숫자가 같으면 TRUE
숫자1 -ne 숫자2 두 숫자가 같지 않으면 TRUE
숫자1 -gt 숫자2 숫자1이 숫자2보다 크면 TRUE
숫자1 -ge 숫자2 숫자1이 숫자2보다 크거나 같으면 TRUE
숫자1 -lt 숫자2 숫자1이 숫자2보다 작으면 TRUE
숫자1 -le 숫자2 숫자1이 숫자2보다 작거나 같으면 TRUE
!숫자1 숫자1이 거짓이라면 TRUE

 

예제) 아래 보기와 같이 숫자 두 개를 입력하도록 하고 입력한 두 숫자의 크기를 비교하시오

 

보기)

$ sh CompareNumber.sh

 

Enter the first number: 200

Enter the second number: 250

 

200 is smaller than 250

 

쉘 스크립트)

#!/bin/bash

 

echo " "

echo -n "Enter the first number: "

read f_num

echo - "Enter the second number: "

read s_num

 

if [ $f_num -gt $s_num ]; then

  echo "$f_num is bigger than $s_num"

 

elif [ $f_num -lt $s_num ]; then

  echo "$f_num is smaller than $s_num"

 

else

  echo "$f_num and $s_num are the same numbers"

반응형