🌏 OS/Linux

[Linux] 리눅스 쉘 프로그래밍 (2)

heywantodo 2023. 3. 14. 10:01
728x90
반응형

리눅스 쉘 프로그래밍 (2)

1. 조건문

 

쉘 스크립트 조건문 

 

if-then-fi

조건 명령어, command 실행 결과에 따른 서로 다른 command를 실행

if grep 계정명 /etc/passwd &> /dev/null
then
	계정이 존재합니다
else
	계정 생성
fi

 

case

$var의 값에 따라 선택해서 명령어를 실행

echo -n "What do you want?"
read answer
case $answer in
	yes) echo "System restart.";;
	no) echo "shutdown the system.";;
	*) echo "entered incorrectly";;
esac

 

2. 반복문

 

쉘 스크립트 반복문

 

while

다음의 command가 성공하는 동안 do~done 사이의 명령어를 반복 실행

while 조건 명령어
do
	반복 명령어
done

 

until

다음의 command가 성공할 때까지 do~done 사이의 명령어를 반복 실행

until 조건 명령어
do
	반복 명령어
done

 

break

loop를 빠져나감 (if문만을 빠져나가는 것이 아니라 loop문 자체에서 빠져나감)

while xx
do
 while yy
do 
	A
	break
 done
	B
done
C

 

for

주어진 list만큼 do~done 사이의 명령어를 반복 실행

for item in List
do
	명령어
done
728x90
반응형