#!/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
The current value of n=1
The current value of n=2
The current value of n=3
good
The current value of n=4
The current value of n=5
bad
The current value of n=6
The current value of n=7
ugly
The current value of n=8
The current value of n=9
The current value of n=10
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
Color = Blue
Color = Green
Color = Pink
Color = White
Color = Red
예제.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
My favorite color is Pink
예제.3 - Reading Command-line arguments
$ vi for3.sh
1
2
3
4
for myval in$*do
echo"Argument: $myval"done
$ ./for3.sh I like ShellScript
Argument: I
Argument: like
Argument: ShellScript
예제.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
1 is odd
2 is even
3 is odd
4 is even
5 is odd
예제.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