#!/bin/bashn=1
while :
do
printf"The current value of n=$n\n"if[$n== 3 ]then
echo"good"elif[$n== 5 ]then
echo"bad"elif[$n== 7 ]then
echo"ugly"elif[$n== 10 ]then
exit 0
fi((n++))done
2. 반복문 for (Using For Loop) 문법
lists 의 갯수만큼 lists 의 내용들을 variable_name 에 대입, do 와 done 사이의 commands(명령)을 반복합니다.
1
2
3
4
5
#!/bin/bashfor variable_name in lists
do
commands
done
condition(조건)을주어 true 일 동안 do 와 done 사이의 commands(명령)을 반복합니다.
1
2
3
4
5
#!/bin/bashfor(( condition ))do
commands
done
예제.1 - Reading static values
1
2
3
4
5
#!/bin/bashfor color in Blue Green Pink White Red
do
echo"Color = $color"done
예제.2 - Reading Array Variable
1
2
3
4
5
6
7
8
#!/bin/bashColorList=("Blue Green Pink White Red")for color in$ColorListdo
if[$color=='Pink'];then
echo"My favorite color is $color"fi
done
예제.3 - Reading Command-line arguments
$ vi for3.sh
1
2
3
4
for myval in$*do
echo"Argument: $myval"done
예제.4 - Finding odd and even number using three expressions
1
2
3
4
5
6
7
8
9
10
#!/bin/bashfor((n=1; n<=5; n++ ))do
if(($n%2==0 ))then
echo"$n is even"else
echo"$n is odd"fi
done
예제.5 - Reading file content
$ vi for5.sh
1
2
3
4
5
6
i=1
for var in`cat weekday.txt`do
echo"Weekday $i: $var"((i++))done