3.7.1 while ±¸¹®
Á¶°ÇºÎ ¹Ýº¹À» ½ÇÇàÇÏ´Â whileÀº while (expression) ¿¡¼ expressionÀÌ ¸¸Á·ÇÏ´Â µ¿ÇÑ while ~ end ±¸¹®À» ¹Ýº¹ÇÑ´Ù. While ±¸¹®À» ¹þ¾î³ª´Â °ÍÀº expressionÀÌ ¸¸Á·ÇÏÁö ¾Ê°Å³ª break ¹®À» »ç¿ëÇÏ´Â °ÍÀÌ´Ù.
»ç¿ë ¹æ¹ý - while
while (expression)
command(s)
. . .
end
1. expressionÀÌ ÂüÀÎ µ¿¾È¸¸ loop ½ÇÇàÇϸç, expressionÀÌ ÃʱâºÎÅÍ °ÅÁþÀ̸é ÀüÇô ¼öÇàÇÏÁö ¾Ê´Â´Ù.
2. end´Â while ¹Ýº¹ ¹®ÀÇ ³¡À» ³ªÅ¸³½´Ù.
¿¹ Á¦ 1 :
< number_count >
#!/bin/csh -f
if ($#argv < 2) then
echo ¡°add_count number1 number2¡±
exit
endif
if ($1 < $2) then
set num1 = $1
set num2 = $2
else
set num1 = $2
set num2 = $1
endif
set count = $num1
while ($count <= $num2)
@ sum += $count
@ count += 1
end
echo ¡°The sum of from $num1 to $num2 are $sum¡±
# End of file
¿¹ Á¦ 2 :
< host_check >
#!/bin/csh -f
if ($#argv < 1) then
echo ¡°host_check hostA hostB hostC ¡¦.¡±
endif
set ps = ¡°/usr/ps -elfo s,user,pcpu,pmen,args¡±
while (1)
foreach testhost ($argv)
set testload = `rup $testhost | cut -c50- | awk ¡®{print $2}¡¯ | tr -d¡®.'`
if ($status == 0) then
echo $testhost
rsh $testhost $ps | egrep ¡®(^O | ^R)¡¯ | egrep -v ¡®(csh|aux|grep)¡¯
else
echo ¡°$testhost down¡±
endif
end
set sleep_time = 300
set count_time = $sleep_time;
@ count_time /= 5
while ($count_time > 0)
sleep 5
@ count_time -= 1
echo -n ¡°.¡±
end
echo ¡°¡±
end # while (1)
# End of file
3.7.2 shift ±¸¹®
¸ñ·Ï º¯¼ö³ª argument¸¦ Çϳª¾¿ °Ë»çÇϰíÀÚ ÇÒ ¶§ ÁÖ·Î »ç¿ëÇÑ´Ù. Shift¸¦ ÇÏ¸é µÎ ¹øÂ° ÀÖ´Â º¯¼ö°¡ ù ¹øÂ° º¯¼ö·Î ¿Å°Ü¿Â´Ù.
shift¸¦ ÇÑ ¹øÇϸé,
1) var_name¿¡ Àִ ù ¹øÂ° ¸ñ·Ï(argvÀÎ °æ¿ì argv[1])ÀÌ »èÁ¦µÈ´Ù.
2) ±× ÀÚ¸®¿¡ µÎ ¹øÂ° ´Ü¾î(argvÀÎ °æ¿ì argv[2])°¡ ¿Â´Ù.
3) $#var_name (argvÀÎ °æ¿ì $#argv) °ªÀº ÀÚµ¿ÀûÀ¸·Î Çϳª ÁÙ¾îµç´Ù.
¸Å shift ¿¬»ê¸¶´Ù À̰ÍÀÌ ¹Ýº¹µÈ´Ù.
»ç¿ë ¹æ¹ý :
shift [variable_name]
1. Àμö(argument) ¾øÀÌ shift¸¦ »ç¿ëÇϸé argv ¸¦ °¡Áö°í ¿¬»êÇÑ´Ù.
2. Àμö·Î variable_nameÀ» ÁÖ¸é º¯¼ö variable_nameÀ» °¡Áö°í ¿¬»êÇÑ´Ù.
3. $#variable_name(¶Ç´Â $#argv)°¡ 0 À̸é error°¡ ¹ß»ýÇÑ´Ù.
¿¹ Á¦ :
< host_check >
#!/bin/csh -f
if ($#argv < 1) then
echo ¡°host_check hostA hostB hostC ¡¦.¡±
endif
set sleep_time = 300
set hostname = ¡°¡±
while ($#argv)
set option = $argv[1]
if ($option == ¡°-t¡±) then
shift
set sleep_time = $argv[1]
else
set hostname = ¡°$hostname $argv[1]¡±
endif
shift
end
set ps = ¡°/usr/ps -elfo s,user,pcpu,pmen,args¡±
set count_time = $sleep_time;
@ count_time /= 5
while (1)
foreach testhost ($hostname)
set testload = `rup $testhost | cut -c50- | awk ¡®{print $2}¡¯ | tr -d¡®.'`
if ($status == 0) then
echo $testhost
rsh $testhost $ps | egrep ¡®(^O | ^R)¡¯ | egrep -v ¡®(csh|aux|grep)¡¯
else
echo ¡°$testhost down¡±
endif
end
while ($count_time > 0)
sleep 5
@ count_time -= 1
echo -n ¡°.¡±
end
echo ¡°¡±
end # while (1)
# End of file
¿©·¯ °¡Áö °æ¿ì¿¡ µû¶ó º°µµÀÇ ¸í·ÉÀ» ½ÇÇàÇÏ´Â °æ¿ì if ~ else if ~ else if ~ endif ±¸¹®À» ¿¬¼ÓÀûÀ¸·Î »ç¿ëÇÒ ¼öµµ ÀÖÁö¸¸ ÀÌ·¸°Ô µÇ¸é ÇÁ·Î±×·¥À» ÀÌÇØÇÏ±â ¾î·Æ´Ù. ÀÌ·² °æ¿ì switch ±¸¹®À» »ç¿ëÇϸé ÁÁ´Ù.
Switch ±¸¹®Àº switch ~ endsw »çÀÌ¿¡ case ±¸¹®À» »ç¿ëÇÏ¿© °¢°¢ÀÇ case¿¡ µû¸¥ ½ÇÇà ¸í·ÉÀ» ÇÁ·Î±×·¥ ÇÑ´Ù. ±×¸®°í °¢°¢ÀÇ case´Â case ~ breaks·Î ±¸ºÐÇÑ´Ù.
»ç¿ë ¹æ¹ý
switch (string)
case pattern1:
command(s)
. . .
breaksw
case pattern2:
command(s)
breaksw
. . .
default:
command(s)
breaksw
endsw
1. string¿¡´Â ¸í·É, ÆÄÀÏ À̸§, º¯¼ö µîÀÌ ¿Ã ¼ö ÀÖ´Ù.
2. stringÀ» °¢ pattern°ú ºñ±³ÇÏ¿© ÀÏÄ¡ÇÏ´Â °ÍÀ» ã´Â´Ù.
3. ÀÏÄ¡ÇÑ patternÀ» ãÀ¸¸é breaksw³ª endsw ¹®Àå »çÀÌÀÇ ¸ðµç ¸í·ÉÀ» ½ÇÇàÇÑ´Ù.
4. endsw´Â switch¹® ÀüüÀÇ ³¡À» ÀǹÌÇϰí breaksw´Â case pattenÀÇ ³¡À» ³ªÅ¸³½´Ù.
5. string°ú case pattern¿¡ ÀÏÄ¡ÇÏ´Â °ÍÀÌ ¾øÀ¸¸é default¹®Àå ´ÙÀ½À» ½ÇÇàÇÑ´Ù.
6. default ¹®ÀåÀÌ ¾øÀ¸¸é switch¹®À» ºüÁ® ³ª°£´Ù.
¿¹ Á¦ :
< option_check >
#!/bin/csh -f
set sign = ¡°$¡±
set group =¡°¡±
set hostname = ¡°¡±
while ($#argv)
set option = $argv[1]
switch($option)
case -d:
shift
set debug = $argv[1]
breaksw
case -g:
shift
set group = ¡°$group $sign$argv[1]¡±
breaksw
case -h:
shift
set hostname = ¡°$hostname $argv[1]¡±
set hostset = ¡®1¡¯
breaksw
case -t:
shift
set sleep_time = $argv[1]
breaksw
endsw
shift
end
if ($?debug) echo ¡°Debug Mode : $debug¡±
if ($?group) echo ¡°Group name : $group¡±
if ($?hostname) echo ¡°Hostname : $hostname¡±
if ($?sleep_time) echo ¡°Sleep Time : $sleep_time¡±
#End of program
¿¹ Á¦ 2 :
< host_check >
#!/bin/csh -f
if ($#argv < 1) then
echo ¡°host_check hostA hostB hostC ¡¦.¡±
endif
set sleep_time = 300
set hostname = ¡°¡±
while ($#argv)
set option = $argv[1]
switch ($option)
case -t:
shift
set sleep_time = $argv[1]
breaksw
default:
set hostname = ¡°$hostname $argv[1]¡±
breaksw
endsw
shift
end
set ps = ¡°/usr/ps -elfo s,user,pcpu,pmen,args¡±
set count_time = $sleep_time;
@ count_time /= 5
while (1)
foreach testhost ($hostname)
set testload = `rup $testhost | cut -c50- | awk ¡®{print $2}¡¯ | tr -d¡®.'`
if ($status == 0) then
echo $testhost
rsh $testhost $ps | egrep ¡®(^O | ^R)¡¯ | egrep -v ¡®(csh|aux|grep)¡¯
else
echo ¡°$testhost down¡±
endif
end
while ($count_time > 0)
sleep 5
@ count_time -= 1
echo -n ¡°.¡±
end
echo ¡°¡±
end # while (1)
# End of file
3.9 È帧 º¯°æ (break, continue, goto, exit)
3.9.1 break
Break ±¸¹®Àº foreach, while ±¸¹® ³»¿¡¼ ÀÌ ±¸¹®À» °Á¦·Î ¹þ¾î³ª°íÀÚ ÇÒ ¶§ »ç¿ëÇÑ´Ù.
1. °¡Àå ¾ÈÂÊ¿¡ ÀÖ´Â loopÀÇ end ´ÙÀ½ ¹®ÀåÀ» ¼öÇàÇÑ´Ù.
2. break°¡ µé¾î ÀÖ´Â loop¸¸À» ºüÁ® ³ª°£´Ù.
3. °èÃþ ±¸Á¶¿¡¼ ÇÑ ´Ü°èÀÇ ·çÇÁ¸¸ ºüÁ® ³ª°£´Ù.
3.9.2 continue
Continue ±¸¹®Àº foreach, while ±¸¹® ³»¿¡¼ ±¸¹®³»ÀÇ ±â´ÉÀ» ¼öÇàÇÏÁö ¾Ê°í ´ÙÀ½ ·çÇÁ·Î °¡°íÀÚ ÇÒ ¶§ »ç¿ëÇÑ´Ù.
1. continue°¡ ÀÖ´Â loopÀÇ end ¹®ÀåÀ» ¼öÇàÇÑ´Ù.
2. continue ºÎÅÍ end ¹®Àå »çÀÌÀÇ ±â´ÉÀº ¼öÇàÇÏÁö ¾Ê´Â´Ù.
3.9.3 goto
Goto ±¸¹®Àº ÇÁ·Î±×·¥ÀÇ ¼öÇà Áß °Á¦·Î ¾î¶² À§Ä¡·Î ºÐ±âÇϰíÀÚ ÇÒ ¶§ »ç¿ëÇÑ´Ù. ±×·¯³ª goto¸¦ ¸¹ÀÌ »ç¿ëÇϸé ÇÁ·Î±×·¥À» ÀÌÇØÇϰųª ¼öÁ¤Çϱâ Èûµé¾î Áö¹Ç·Î »ç¿ëÇÏÁö ¾Ê´Â °ÍÀÌ ÁÁ´Ù.
1. goto °¡ °¡¸®Å°´Â label·Î ¹«Á¶°Ç ºÐ±âÇÑ´Ù.
2. goto °¡ ºÐ±âÇÒ À§Ä¡´Â label µÚ¿¡ ÄÝ·ÐÀ» ºÙÇô Ç¥½Ã(label:)ÇÑ´Ù.
if³ª whileµî¿¡ ÀÇÇÑ ¿©·¯°³ÀÇ loop¸¦ ÇÑ ¹ø¿¡ ºüÁ®³ª¿À°íÀÚ ÇÒ ¶§´Â ÀÌ ¹æ¹ýµµ À¯¿ëÇÏ´Ù.
¿¹ Á¦ :
#!/bin/csh -f
@ count = 0
while (1)
@ count++
if ($count == 10) continue
echo $count
if ($count == 20) break
end
< ½¬¾î°¡´Â °÷ >
ÇöÀç »ç¿ëÇÏ´Â ´ëºÎºÐÀÇ CPU´Â ÆÄÀÌÇÁ¶óÀÎÀ» Á¦°øÇÏ¸ç ¼öÇà¼Óµµ¸¦ ¿Ã¸®±â À§ÇØ ´ÙÀ½ ½ÇÇà¿¡ ÇÊ¿äÇÑ ÀνºÆ®·°¼Ç°ú µ¥ÀÌÅ͸¦ ½Ã½ºÅÛÀÇ main memory·ÎºÎÅÍ CPU cache¿¡ ¹Ì¸® °¡Á®¿Í ÀúÀåÇØ ³õ´Â´Ù.
±×·¯³ª goto ¹®À» ¸¸³ª¸é ÀÌ·¸°Ô ¹Ì¸® °¡Á®¿Í ÀúÀåÇØ ³õÀº °ÍÀÌ ¼Ò¿ë¾ø°í, goto°¡ Áö½ÃÇÏ´Â label ÀÌÈÄÀÇ ÀνºÆ®·°¼ÇÀ» ½ÇÇàÇϱâ À§ÇØ ½ÇÇàÄÚµå¿Í DataµîÀ» memory·Î ºÎÅÍ CPU cache·Î ´Ù½Ã °¡Á®¿Í¾ß ÇÑ´Ù. »õ·Î¿î ÀνºÆ®·°¼ÇÀ» cache·Î °¡Á®¿À´Â µ¿¾È CPU´Â ÀÛ¾÷´ë±â¿¡ µé¾î°¡°í, ÀÌ ½Ã°£ µ¿¾È CPU´Â ÀÛ¾÷À» ÇÏÁö ¾ÊÀ¸¹Ç·Î CPUÀÇ clockÀÇ ³¶ºñ¸¦ °¡Á®¿Â´Ù. ÀÌ·± °úÁ¤À¸·Î ÀÎÇØ ½Ã½ºÅÛÀÇ ¼º´ÉÀº ÀúÇÏÇÏ°Ô µÈ´Ù.
Goto ¹®À» »ç¿ëÇϸé ÇÁ·Î±×·¥ÀÇ ÀÌÇØµµ ¾î·ÆÁö¸¸ ½Ã½ºÅÛ¿¡¼ ½ÇÇà ½Ã¿¡µµ ÀÌ¿Í °°Àº ¹®Á¦°¡ ¹ß»ýÇÑ´Ù.
¾î¶² program ¿¡¼ ÀÚ±â ÀÚ½ÅÀ» È£ÃâÇÏ´Â °ÍÀ» ȸ±Í¶ó°í ºÎ¸¥´Ù. C shell program¿¡¼´Â UNIX ¸í·ÉÀ̳ª ´Ù¸¥ script¸¦ È£ÃâÇÒ ¼ö ÀÖÀ¸¹Ç·Î, Shell Program¿¡¼µµ ÀÚ±â ÀÚ½ÅÀ» È£ÃâÇÏ¿© »ç¿ëÇÒ ¼ö ÀÖ´Ù.
ȸ±ÍÀÇ ¹æ¹ýÀº ¶È°°Àº ¹æ¹ýÀ¸·Î ¿©·¯ ¹ø °°Àº ÀÏÀ» ¹Ýº¹ÇÒ ¶§ ÀÌ ±â¹ýÀ» »ç¿ëÇÑ´Ù. cp³ª rm ¸í·É¾î¿¡ ¡°-r¡± optionÀ» »ç¿ëÇÏ¿© sub µð·ºÅ丮¸¦ ã¾Æ°¡¸ç °°Àº ÀÏÀ» ½ÇÇàÇÒ °æ¿ìµµ ÀÌ ¹æ¹ýÀ» »ç¿ëÇϸç, °è½Â(ÆÑÅ丮¾ó)ÀÇ ¿¬»ê¿¡ À¯¿ëÇÏ´Ù.
»ç¿ë ¹æ¹ý :
script ³»¿¡ ½ÇÇà ¸í·ÉÀ¸·Î script À̸§À» »ç¿ëÇÑ´Ù.
´ÙÀ½ÀÇ µÎ ¿¹Á¦ Áß Ã³À½ÀÇ °ÍÀº ȸ±Í¸¦ »ç¿ëÇÑ °ÍÀ̸ç, µÎ ¹øÂ°ÀÇ °ÍÀº while ±¸¹®À» »ç¿ëÇÑ °ÍÀÌ´Ù. ÀϹÝÀûÀ¸·Î ÇÁ·Î±×·¥¿¡¼ ȸ±Í¸¦ »ç¿ëÇÑ °æ¿ì´Â ½ÇÇà ÄÚµå´Â ÁÙ¾îµå³ª ½ÇÇà ½Ã°£ÀÌ ¸¹ÀÌ °É¸°´Ù. (ShellÀº ¾Æ´Ñ°Í °°´Ù.)
¿¹ Á¦ 1 :
<program in factorial1>
#!/bin/csh -f
set number = $argv[1]
@ i = $number
@ j = $i - 1
if ($j > 0) then
@ i = $i * `factorial $j`
echo $i
else
echo 1
endif
# End of file
¿¹Á¦ 2 :
< Program in factorial2 >
#!/bin/csh -f
set number = $argv[1]
@ i = $number
@ j = $i - 1
while ($j > 0)
@ i = $i * $j
@ j--
end
echo ¡°Input number $number factorial is $I¡±
# End of file