C ShellÀº »ê¼ú ¿¬»ê, ¹®ÀÚ¿ ºñ±³, bit ¿¬»ê ±×¸®°í ³í¸®¿¬»êÀ» ÇÒ ¼ö ÀÖ´Ù. C ShellÀÇ ¿¬»êÀÚµµ C¾ð¾î¿Í µ¿ÀÏÇÑ ¹®¹ý°ú ¿¬»ê ¿ì¼± ¼øÀ§¸¦ °®´Â´Ù.
C ShellÀÇ ¾àÁ¡Àº ÀÌ ¿¬»ê ºÎºÐÀÌ´Ù. C ShellÀº ³»ºÎ¿¡¼ º¯¼ö °ªÀ» stringÀ¸·Î ÀúÀåÇÏ°í ¿¬»ê ½Ã¿¡ ¼ýÀÚ·Î º¯È¯ÇÏ´Â °úÁ¤À» °ÅÄ¡±â ¶§¹®¿¡ ¿¬»êÀº ¸¹Àº ½Ã°£À» ÇÊ¿ä·Î ÇÑ´Ù. ¸¸ÀÏ ¿¬»êÀ» ¸¹ÀÌ ÇÏ´Â °úÁ¤À» Shell°ú °°Àº script ¾ð¾î·Î ÀÛ¼ºÇÏ°í ½Í´Ù¸é awk³ª perlÀÇ »ç¿ëÀ» ±ÇÇÑ´Ù. ¶ÇÇÑ C Shell¿¡¼´Â ½Ç¼ö ¿¬»êÀ» ÇÒ ¼ö ¾ø´Ù.
3.4.1 ¿¬»êÀÚÀÇ »ç¿ë
»ç¿ë¹æ¹ýÀº »ê¼ú¿¬»êÀ» ½ÃÀÛÇÏ´Â ±âÈ£ÀÎ @ µÚ¿¡ ¼öÇàÇϰíÀÚ ÇÏ´Â »ê¼ú½ÄÀ» ÀÔ·ÂÇÑ´Ù. ´ÙÀ½Àº »ê¼ú¿¬»êÀÇ »ç¿ë ¹æ¹ý°ú »ç¿ë ½Ã ÁÖÀÇÁ¡¿¡ ´ëÇÑ °ÍÀÌ´Ù.
»ç¿ë ¹æ¹ý :
@ variable_name operator expression
1. @¿Í variable_name »çÀÌ¿¡´Â ¹Ýµå½Ã "°ø¹é"À» ³Ö¾î¾ß ÇÑ´Ù.
2. variable_nameÀº c shell º¯¼öÀ̰í expressionÀº ¼ö½ÄÀÌ´Ù.
3.4.2 Assignment Operator
C Shell¿¡¼ ´ÙÀ½°ú °°ÀÌ ¿¬»êÀ» ¼öÇàÇÑ´Ù.
1. @ ¿¬»êÀÚ¸¦ »ç¿ëÇÏ¿© »çÄ¢¿¬»êÀ» ÇÒ ¼ö ÀÖ´Ù.
2. =,+,-,+=,-=,*=,/= ÀÇ ¾Õ µÚ¿¡´Â ¹Ýµå½Ã °ø¹éÀÌ ÀÖ¾î¾ß ÇÑ´Ù.
3. ³ª´°¼À ¿¬»êÀº ¸òÀ̳ª, ³ª¸ÓÁö¸¦ ÁöÁ¤ÇÏ¿© ¿¬»êÇÑ´Ù.
4. º¯¼ö À̸§°ú ++ ¶Ç´Â -- »çÀÌ¿¡´Â °ø¹éÀÌ À־ ¾ÊµÈ´Ù.
5. @ variable_name[index] = expression °ú °°ÀÌ ¸ñ·Ï º¯¼ö¸¦ »ç¿ëÇÒ ¼ö ÀÖ´Ù
6. @ ¿¬»êÀÚ¸¦ ¹®ÀÚ¿ µ¥ÀÌÅÍ¿Í ÇÔ²² »ç¿ëÇÒ ¼ö ¾ø´Ù.
¿¹ Á¦ 1 :
% @ count = 6 ÀÏ ¶§
Symbol |
Meaning |
Sample |
%echo $count |
= |
º¯¼ö¿¡ ¿ìº¯ °ªÀ» ÀúÀåÇÑ´Ù. |
@ count = 3 |
3 |
+= |
º¯¼ö¿¡ ¿ìº¯ °ªÀ» ´õÇÑ´Ù. |
@ count += 6 |
9 |
-= |
º¯¼ö¿¡ ¿ìº¯ °ªÀ» »«´Ù. |
@ count -= 5 |
4 |
*= |
º¯¼ö¿¡ ¿ìº¯ °ªÀ» °öÇÑ´Ù. |
@ count *= 8 |
32 |
/= |
º¯¼ö¿¡ ¿ìº¯ °ªÀ¸·Î ³ª´« ¸ò |
@ count /= 5 |
6 |
%= |
º¯¼ö¿¡ ¿ìº¯ °ªÀ¸·Î ³ª´« ³ª¸ÓÁö |
@ count %= 4 |
2 |
++ |
º¯¼ö¿¡ 1 À» ´õÇÑ´Ù. |
@ count++ |
3 |
-- |
º¯¼ö¿¡ 1À» »«´Ù. |
@ count-- |
2 |
Sample ³¿¡ ÀÖ´Â ¿¬»êÀ» ¼öÇà ÈÄ ±× °á°ú´Â ¡°echo $count¡±¿Í °°ÀÌ ÇÏ¸é ¾Ë ¼ö ÀÖ´Ù.
¿¹ Á¦ 2 :
% @ num = 20 ÀÏ ¶§
Symbol |
Meaning |
Sample |
%echo $r |
+ |
µ¡¼À |
@ r = $num + 6 |
26 |
- |
»¬¼À |
@ r = $num - 3 |
17 |
* |
°ö¼À |
@ r = $num * 5 |
100 |
/ |
Á¤¼ö ³ª´°¼À(¸ò °è»ê) |
@ r = $num / 6 |
3 |
% |
Á¤¼ö ³ª¹µ¼À(³ª¸ÓÁö °è»ê) |
@ r = $num % 6 |
2 |
¡¡
3.4.3 Bitwise and Logical Operators
C Shell¿¡¼´Â bit ¿¬»ê°ú ³í¸® ¿¬»êÀ» Áö¿øÇÑ´Ù.
1. bit ¿¬»ê¿¡¼ ºóÀÚ¸®´Â "0"À¸·Î ä¿öÁø´Ù.
2. >>, <<, &, |, ^ ¿¬»êÀÚ¸¦ Æ÷ÇÔÇÏ´Â ½ÄÀº ¹Ýµå½Ã °ýÈ£·Î ¹¾î¾ß ÇÑ´Ù.
3. ¿¬»êÀÚ°¡ ¾Æ´Ñ ÀÔÃâ·Â ÀçÁöÁ¤, ÈÄÀ§ ÀÛ¾÷, ÆÄÀÌÇÁ µîÀÇ ºÎÈ£·Î ÀÎ½ÄµÉ ¼ö Àֱ⠶§¹®ÀÌ´Ù.
»ç¿ë ¹æ¹ý :
Bitwise shift operator : >> (shift right), << (shift left)
Bitwise unary operator : ~ (1's complement), ! (logic negation)
Bitwise logic operator : | (OR), ^ (XOR), & (AND)
Logical operator : || (logic OR), && (logic AND)
¿¹ Á¦ 1 :
% @ aa = 20
% @ bb = (~ $aa)
% echo $bb
-21
% @ aa = 25
% @ bb = (! $aa)
% echo $bb
0
% @ bb = (! (! $aa))
% echo $bb
1
¿¹ Á¦ 2 :
% @ aa = 5
% @ bb = ($aa << 1)
% echo $bb
% @ bb = ($aa << 4)
% echo $bb
(Âü°í) 4ÀÇ ¹è¼ö·Î °è¼Ó ½ÇÇàÀ» ÇØº¸¶ó ¾î¶² °á°ú¸¦ ¾òÀ» ¼ö Àִ°¡ ?
3.4.4 Comparison Operators
C Shell¿¡´Â ¼ýÀÚ, ¹®ÀÚ¿, patternÀÇ ¼¼ °¡Áö ÇüÅÂÀÇ ÀÚ·á¿¡ ´ëÇÑ ºñ±³ ¿¬»êÀÚ°¡ ÀÖ´Ù. °¢°¢ÀÇ ¿¬»êÀÚ´Â ´ÙÀ½°ú °°´Ù.
1. ¼ýÀÚ ºñ±³ ¿¬»êÀÚ : ==, !=, >, >=, <, <=
2. ¹®ÀÚ¿ ºñ±³ ¿¬»êÀÚ : ==, !=
3. Pattern matching ¿¬»êÀÚ : =~, !~
4. Pattern matchingÀÇ meta character´Â ÆÄÀÏÀ̸§ È®Àå¿¡ »ç¿ëµÈ °Í°ú µ¿ÀÏÇÏ´Ù.
* : Match any string(including null)
[ ] : Define a character set
? : Match a single character
¿¹ Á¦ :
Symbol |
Meaning |
Sample |
Result |
== |
equal to |
$num == 5 |
True (@num = 5) |
!= |
not equal to |
$num != 5 |
False |
> |
grater than |
$num > 3 |
True |
>= |
grater than or equal to |
$num >=3 |
True |
< |
less than |
$num < 3 |
False |
<= |
less than or equal to |
$num <=3 |
False |
== |
equal to |
$str == xyz |
True ($str = xyz À϶§) |
!= |
not equal to |
$str != abc |
True($str = xyz À϶§) |
=~ |
matches |
$pat =~ [abc]* |
True ($patÀÌ abc·Î ½ÃÀÛ) |
!~ |
does not match |
$pat !~ *c |
False ($patÀÌ c·Î Á¾·á) |
if ±¸¹®À» »ç¿ëÇÑ Á¶°ÇºÎ ½ÇÇàÀº ÇÁ·Î±×·¥ÀÇ ÀÛ¼º¿¡ °¡Àå ¸¹ÀÌ »ç¿ëÇÏ´Â ±â´ÉÀ¸·Î, ¾î¶² Á¶°ÇÀ» ¸¸Á·ÇÒ °æ¿ì, ƯÁ¤ ¸í·É¾î³ª ÀÛ¾÷À» ½ÇÇàÇϰíÀÚ ÇÒ ¶§ »ç¿ëÇÑ´Ù.
3.5.1 if ±¸¹®
If ±¸¹®ÀÇ »ç¿ëÀº if ~ endif ·Î ³¡³ª´Â °£´ÜÇÑ ±¸¹®¿¡¼ if ~ else if ~ else if ¡¦ endif·Î ³¡³ª´Â º¹ÀâÇÑ ±¸¹®±îÁö »ç¿ëÇÒ ¼ö ÀÖ´Ù. ±×¸®°í if ±¸¹® ¾È¿¡¼ ¶Ç if ±¸¹®À» »ç¿ëÇÏ´Â °èÃþ ±¸Á¶µµ »ç¿ëÇÒ ¼ö ÀÖ´Ù.
»ç¿ë ¹æ¹ý :
1. if (expression) command
2. if (expression) then
command(s)
endif
3. if (expression) then
command(s)
else
command(s)
endif
4. if (expression) then
command(s)
else if (expression) then
command(s)
. . .
else
command(s)
endif
5. expression¿¡´Â comparison operator¸¦ »ç¿ëÇÑ´Ù.
¿¹ Á¦ 1 :
1. editor¸¦ »ç¿ëÇÏ¿© host_check ÆÄÀÏ¿¡ ´ÙÀ½ÀÇ ³»¿ëÀ» ÀÔ·ÂÇÑ´Ù.
< host_check >
#!/bin/csh -f
set ps = ¡°/usr/ps ?elfo s,user,pcpu,pmen,args¡±
set testhost = $argv[1]
set testload = `rup $testhost | cut -c53-56`
if ($status == 0) then
echo $testhost
rsh $testhost $ps | grep ¡°^O ¡° | egrep -v ¡®(csh|aux|grep)¡¯
else
echo ¡°$testhost down¡±
endif
2. ÆÄÀÏÀ» ½ÇÇàÇÒ ¼ö ÀÖµµ·Ï mode¸¦ º¯°æÇÑ´Ù.
% chmod 755 host_check
3. ÆÄÀÏÀ» ½ÇÇàÇÑ´Ù,
% host_check test_hostname
% host_check borisu
4. ÀÌ ÇÁ·Î±×·¥ÀÇ ½ÇÇà °á°ú´Â testhost°¡ µ¿À۵ǰí ÀÖÀ¸¸é ÇöÀç ½ÇÇàÁßÀÎ ÀÛ¾÷À» º¸¿©ÁÖ°í, ½Ã½ºÅÛÀÌ ¹ÝÀÀÇÏÁö ¾ÊÀ¸¸é ¡°testhost down¡±À̶ó´Â ¸Þ½ÃÁö¸¦ º¸¿©ÁØ´Ù.
5. rupÀ¸·Î ½Ã½ºÅÛÀ» checkÇϴµ¥ ¹®Á¦°¡ ÀÖÀ¸¸é ÀÌ ºÎºÐÀ» ´ÙÀ½°ú °°ÀÌ ¼öÁ¤ÇÏ¸é ¸¹Àº ¹®Á¦°¡ ¾ø¾îÁø´Ù.
rup $testhost | cut ?c50- | awk ¡®{print $2}¡¯ | tr ?d ¡®.¡¯
3.5.2 File Inquiry Operator
C Shell¿¡¼´Â ÆÄÀÏÀÇ Á¸Àç À¯¹«³ª Ư¼ºÀ» Check ÇÒ ¼ö ÀÖ´Â ±â´ÉÀ» Á¦°øÇÑ´Ù. ÀÌ ±â´É°ú if ±¸¹®À» Á¶ÇÕÇÏ¿© ´Ù¾çÇÑ ±â´ÉÀ» ÇÏ´Â ÇÁ·Î±×·¥À» ÀÛ¼ºÇÒ ¼ö ÀÖ´Ù.
»ç¿ë ¹æ¹ý
if ( -operator filename ) ...
°¢ OperatorÀÇ Àǹ̴ ´ÙÀ½°ú °°´Ù.
³í¸® ºÎÁ¤À» »ç¿ëÇÒ ¶§´Â ¿¬»êÀÚ ! ¸¦ ¾ÕÀÇ operator¿Í ÇÔ²² »ç¿ëÇÑ´Ù.
if (! -e file_name) then
¿¹ Á¦ :
<file_check >
#!/bin/csh -f
set testfile = $1
if (! ?e $testfile) then
echo ¡°$testfile is not exist¡±
exit
endif
if (-z $testfile) then
echo ¡°$testfile is blank file¡±
else if (-f $testfile) then
@ mode = 0
if (-r $testfile) @ mode += 4
if (-w $testfile) @ mode += 2
if (-x $testfile) @ mode += 1
if ($mode == 7) echo ¡°$testfile mode is Read, Write, Execute¡±
if ($mode == 6) echo ¡°$testfile mode is Read, Write¡±
if ($mode == 4) echo ¡°$testfile mode is Read Only¡±
else if (-d $testfile) then
echo $testfile is directory
else
echo "$testfile is not plan_file and directory"
echo "$testfile is special file"
endif
# End of File
ÀÏÁ¤ Ƚ¼ö·Î ¾î¶² ±¸¹®À» ¹Ýº¹ ½ÇÇàÇϰíÀÚ ÇÒ ¶§ foreach ~ end ±¸¹®À» »ç¿ëÇÑ´Ù. foreach ±¸¹®¿¡¼ ¹Ýº¹ Ƚ¼ö´Â º¯¼ö listÀÇ °³¼ö¿Í °°´Ù. ¹Ý¸é¿¡ while ±¸¹®(´ÙÀ½¿¡ ¼³¸í)Àº Á¶°ÇÀÌ ¸¸Á·ÇÏ´Â ÇÑ while ~ end »çÀÌÀÇ ±¸¹®À» ¹Ýº¹ÇÑ´Ù.
»ç¿ë ¹æ¹ý :
foreach name ( wordlist )
command(s)
. . .
end
1. ¹Ýº¹ ½ÇÇàÇÒ ¶§ ¸¶´Ù wordlist¿¡ ÀÖ´Â ´ÙÀ½ ´Ü¾î¸¦ name¿¡ ÇÒ´çÇÑ´Ù.
2. end´Â foreach ¹Ýº¹ ¹®ÀÇ ³¡À» ³ªÅ¸³½´Ù.
3. foreach ±¸¹®ÀÇ ½ÇÇàÁß ºüÁ® ³ª¿À·Á¸é break¸¦ »ç¿ëÇÑ´Ù.
(Break ´Â foreach, while µîÀÇ ±¸¹®ÀÇ ½ÇÇà Áß Áß°£¿¡¼ ÀÌ ±¸¹®À» ¹þ¾î³ª°íÀÚ ÇÒ ¶§ »ç¿ëÇÑ´Ù.)
4. wordlist ´Â º¯¼öµéÀÇ ¸®½ºÆ®³ª ÁýÇÕ º¯¼ö¸¦ »ç¿ëÇÑ´Ù.
¿¹ Á¦ 1 :
´ÙÀ½ÀÇ ÇÁ·Î±×·¥Àº argument·Î ¿©·¯ ´ëÀÇ hostnameÀ» ÀÔ·Â ¹Þ¾Æ °¢ ½Ã½ºÅÛ¿¡¼ ÇöÀç CPU¿¡¼ ½ÇÇà(State Çʵ尡 O(On CPU)³ª R(Runnable)ÀÎ ÇÁ·Î¼¼½º)µÇ°í ÀÖ´Â ÀÛ¾÷À» º¸¿©ÁØ´Ù.
< host_check >
#!/bin/csh -f
set ps = ¡°/usr/ps -elfo s,user,pcpu,pmen,args¡±
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
# End of file
¿¹ Á¦ 2 :
´ÙÀ½ÀÇ ÇÁ·Î±×·¥Àº argument·Î ÀÔ·ÂµÈ µð·ºÅ丮³ª ÇöÀç µð·ºÅ丮(argumenet°¡ ¾øÀ»¶§)¿¡ Æ÷ÇÔµÈ ÆÄÀÏÀÇ °³¼ö¿Í µð·ºÅ丮 °³¼ö¸¦ ¼¼¾î ±× °á°ú¸¦ º¸¿©ÁØ´Ù.
< file_count >
#!/bin/csh -f
# Counts number of files and directoris in argument1
# or current directory
# Usage : numfile [directory]
if ($#argv == 0) then
set dir = "."
else
set dir = $argv[1]
endif
if (! -d $dir) then
echo $0\: $dir not a directory
exit 1
endif
echo $dir\:
@ fcount = 0
@ dcount = 0
cd $dir
foreach file (*)
if (-f $file) then
@ fcount++
else if (-d $file) then
@ dcount++
endif
end
echo $fcount files $dcount directories
# End of file
(ÁÖÀÇ) À§ÀÇ ¿¹Á¦¿¡¼ foreach file (*)¿¡¼ (*)ÀÇ Àǹ̴ ÇöÀç µð·ºÅ丮¿¡ Æ÷ÇÔµÈ ÆÄÀϰú µð·ºÅ丮ÀÇ ¸®½ºÆ®¸¦ ÀǹÌÇÑ´Ù. foreach file (`ls`)·Î ½ÇÇàÇÏ¿©µµ µÇ³ª ls¸¦ »ç¿ëÇϸé ls ½ÇÇàÀ» À§ÇÑ sub shellÀÌ ¸¸µé°í ±× °á°ú¸¦ ¹Þ¾Æ ¼öÇàÇϹǷΠ¼Óµµ°¡ ´Ê´Ù. (*)´Â sub shellÀ» ¸¸µéÁö ¾Ê°í ¹Ù·Î ½ÇÇàÇϹǷΠ¼Óµµ°¡ ºü¸£°í ½Ã½ºÅÛÀÇ ÀÚ¿øµµ Àû°Ô »ç¿ëÇÏ°Ô µÈ´Ù.