본문 바로가기

Linux

Linux 스크립트 if문, 반복문, 트랩

728x90

if문

if 명령어
then
	명렁어의 종료 코드가 0인 경우 수행될 명령어
fi

 

if 명령어; then
	명렁어의 종료 코드가 0인 경우 수행될 명령어
fi

 

if then .. else

if 명령어; then
	명렁어의 종료 코드가 0인 경우 수행
else
	명령어 종료 코드가 0이 아닌 경우 수행
fi

if then .. else if .. else문

if 명령어1; then
	명렁어1의 종료 코드가 0인 경우 수행
elif 명령어2; then
	명렁어2의 종료 코드가 0인 경우 수행
elif 명령어3; then
	명렁어3의 종료 코드가 0인 경우 수행
else
	모든 명령어의 종료 코드가 0이 아닌 경우 수행
fi

 


case문

case $variable in
    value1) command1-0
    	    command1-1;;
    value2) command2-0
    	    command2-1;;
    *) command3-0
       command3-1;;
esac

 

예제

case $num in
    0) echo "zero";;
    1) echo "one";;
    2) echo "two";;
    3) echo "three";;
    *) echo "other";;
esac

비교문

test $num1 -eq $num2
참이면 종료코드 = 0

 

비교문 해석
num1 -eq num2 num1과 num2가 같다면 참
num1 -ne num2 num1과 num2가 다르다면 참
num1 -gt num2 num1이 num2보다 크다면 참
num1 -ge num2 num1이 num2보다 크거나 같다면 참
num1 -lt num2 num1이 num2보다 직다면 참
num1 -le num2 num1이 num2보다 작거나 같다면 참

 

 

 

예제

read -p 'input number: ' num

if test $num -eq 0; then
	echo 'You have input zero.'
fi

if test $num -gt 0; then
	echo 'you have input positive number.'
fi

if test $num -lt 0; then
	echo 'you have input negative number.'
fi

 

test 명령어 대신 [ ] 명령어를 사용할 수 있다.

주의할점은 [ 와 ] 앞뒤에는 반드시 공백이 있어야 한다.

read -p 'input number: ' num

if [ $num -eq 0 ] ; then
	echo 'You have input zero.'
fi

if [ $num -gt 0 ] ; then
	echo 'you have input positive number.'
fi

if [ $num -lt 0 ] ; then
	echo 'you have input negative number.'
fi

 


논리연산자

read -p "input number : " num

if [ $age -ge 0 -a $age -le 10 ]; then
        echo "0~10"
else
        echo "not 0~10"
fi

 

  • -a : and
  • -o : or

for문

for variable in (list of value)
do
    commands
    ..
done

for variable in (list of value); do
    commands
    ..
done

 

예제

for val in 1 2 3 4 5; do
	echo "$val: hello world!"
done

 

for val in {1..5}; do
	echo "$val: hello world!"
done

 

read num
for val in {1..$num}; do
	echo "$val: hello world!"
done

<< 3
>> {1..3} hello world!

 

read num
for val in $(seq 1 $num); do
	echo "$val: hello world!"
done

<< 3
>> 1: hello world!
>> 2: hello world!
>> 3: hello world!

 

read num
for val in $(seq 1 $num); do
	echo "$val: hello world!"
done

 

for filename in *; do
	echo "file : $filename"
done

 

# 파일의 내용 단어 단위로 읽어오기
for word in $(cat bible.txt); do
	echo $word
done

...
Christ
Jesus.
Ephesians
2:8
For
it
is
by
...

 


while 문

while command; do
	command의 종료코드가 0인 동안 반복할 명령어
done

 

cnt=0
while [ $cnt -lt 5 ]; do
    echo "$cnt: hello"
    cnt=$(($cnt + 1))
done

 

while true; do
    commands
done

 


반복문 제어

break

for num in {1..10} do
    if [ $num -eq 5 ]; then
    	break
    fi
    echo "$num:hello world!"
done

1:hello world!
2:hello world!
3:hello world!
4:hello world!

 

continue

for num in {1..10} do
    if [ $num -eq 5 ]; then
    	continue
    fi
    echo "$num:hello world!"
done


1:hello world!
2:hello world!
3:hello world!
4:hello world!
6:hello world!
7:hello world!
8:hello world!
9:hello world!
10:hello world!

 


매개변수

script.sh---
echo $# $0 $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10}
------------

./script.sh arg1 arg2 arg3 arg4 arg5 arg6 arg7 arg8 arg9 arg10

>> 3
>> ./script.sh
>> arg1 arg2 arg3 arg4 arg5 arg6 arg7 arg8 arg9 arg10

 

./script.sh arg1 arg2 arg3

       $0       $1    $2    $3

 

* 주의 : $10 (x) ${10} (o)

$10은 $1에 0을 붙인 문자로 확장된다.

 


옵션 처리

* 짧은 옵션(ex : -a, -b, -c)

while getopts abc myopt
do
    case $myopt in
    	a) echo 'option -a';;
        b) echo 'option -b';;
        c) echo 'option -c';;
    esac
done

 

* 옵션에 인자가 붙는 경우

while getopts ab:c: myopt
do
        case $myopt in
                a) echo 'option -a';;
                b) echo "option -b argument : $OPTARG";;
                c) echo "option -c argument : $OPTARG";;
        esac
done

<< ./options.sh -a -b 123 -c bithub
>> option -a
>> option -b argument : 123
>> option -c argument : bithub

 

인자를 받고 싶은 옵션문자에 콜론(:)을 붙여주고 $OPTARG를 통해 인자를 참조할 수 있다.

 


시그널 처리(트랩)

trap 'echo Interrupt signal received' SIGINT

while true; do
    sleep 1
done

 

Syntax : trap '시그널 수신 시 수행할 작업' 시그널종류

시그널 처리를 할 수 있다.

* 참고 : SIGKILL 시그널은 처리할 수가 없다. SIGKILL을 수신하면 프로세스는 반드시 종료된다.

 

trap 'echo Interrupt signal received' SIGINT # 트랩등록

while true; do
    sleep 1
done

trap -- SIGINT # 트랩해지

 

Syntax : trap -- 시그널종류

정의했던 트랩을 해지할 수 있다.