클라우드 아카이브

[Shell][반복문] while-do-done 명령어 본문

OS 및 스토리지/Linux

[Shell][반복문] while-do-done 명령어

Cloud Engineer 2021. 12. 26. 18:15

while-do-done 명령어란

C언어에서 while을 통한 반복문 구현과 같이 Shell 스크립트를 통해 반복문을 구현할 때 while-do-done 구문을 사용하여 구현이 가능합니다.

문법 구조

# 기본 문법 구조
while [조건]
do
   명령어1
   명령어2
done

예제1: 0부터 10까지의 숫자를 반복문으로 출력

#!/bin/bash
# while.sh

iterator=0
while [ $iterator -le 10 ]
do
   printf "숫자 출력: %d\n" $iterator
   ((iterator++))
done
# 출력
$sh read.sh
숫자 출력: 0
숫자 출력: 1
숫자 출력: 2
숫자 출력: 3
숫자 출력: 4
숫자 출력: 5
숫자 출력: 6
숫자 출력: 7
숫자 출력: 8
숫자 출력: 9
숫자 출력: 10

 

Comments