1993.1.4
SUBJECT: C shell, Bourne shellÀÇ Â÷ÀÌÁ¡ ºñ±³
MEANING OF THIS MEMO
C shell°ú Bourne shellÀÇ syntax ¹× ¿©Å¸ Â÷À̸¦ ºñ±³ÇØ º¸¾Ò´Ù.
CONTENTS:
0. Metacharacter
1. Quote
2. Variable
3. Argument
4. Operators
5. Redirection
6. ÇÁ·Î±×·¥ ±¸¹®µé ¡¡
1) if¹®
2) while¹®¡¡
3) case and switch¹®¡¡
4) for¹®¡¡
DESCRIPTION:
0. Metacharacter
* metacharacter´Â shell¿¡ ÀÇÇØ ³ª¸§´ë·ÎÀÇ °ªÀ̳ª Àǹ̸¦ °®´Â
special character¸¦ ¸»ÇÑ´Ù.
* file name handling¿¡ ¾²ÀÌ´Â metacharacter¸¦ wildcard¶ó ÇÑ´Ù.
--------------------------------
Shell C-Shell
--------------------------------
*
?
[]
[-]
[! ]
\
;
|
&
(cmd)
cmd && cmd
cmd || cmd
---------------------------------
1. Quote
* single, double quotation mark(', ")´Â encloseµÈ characterÀÇ Æ¯Á¤ Àǹ̵éÀ» ¾ø¾Ö¹ö¸°´Ù.
backslash(\)´Â ¹Ù·Î µÞ characterÀÇ Æ¯Á¤ Àǹ̵éÀ» ¾ø¾Ö¹ö¸°´Ù.
* ¾Æ·¡ Ç¥´Â quote½Ã¿¡µµ metacharacterÀÇ Àǹ̸¦ ±×´ë·Î À¯ÁöÇÏ´Â character¸¦ º¸¿© ÁØ´Ù.
Áï °¢ quote¿¡¼ ¿¹¿ÜÀûÀÎ °æ¿ìÀÌ´Ù.
+-----------------------------+--------------------------+
| Shell | C-Shell |
+=========+=============================+==========================+
| ' ' | ' | ! |
+---------+-----------------------------+--------------------------+
| " " | " ` $ \ | ` $ ! |
+---------+-----------------------------+--------------------------+
| | double quote³»¿¡¼ÀÇ \´Â | ¶Ç´Ù¸¥ \¸¦ Æ÷ÇÔÇÏ¿© |
| \ | $, `, ", ¶Ç´Ù¸¥ \ ÀÇ | ¸ðµç special meaningÀ» |
| | special meaningÀ» ¾ø¾ÖÁÜ | ¾ø¾ÖÁÜ |
+---------+-----------------------------+--------------------------+
* ShellÀÇ °æ¿ì
- Shell¿¡¼ metacharacterÀÇ Àǹ̸¦ Áö¿ö¹ö¸®´Â °¡Àå °·ÂÇÑ °ÍÀº single quoter(')ÀÌ´Ù.
Æò¹üÇÑ Àǹ̷Π»ç¿ëÇϰíÀÚ ÇÏ´Â metacharacter°¡ µé¾î°£ stringÀ» »ç¿ëÇÏ´Â °æ¿ì´Â
single quoter¸¦ ¾²´Â °ÍÀÌ Á¦ÀÏ È®½ÇÇÑ ¹æ¹ýÀÌ µÉ °ÍÀÌ´Ù.
- double quoter ³»¿¡¼µµ special Àǹ̸¦ À¯ÁöÇÏ´Â ¼¼°¡Áö(` $ \).
double quoter ³»¿¡¼ À̵éÀ» ÀÏ¹Ý ¹®ÀڷΠǥÇöÇÒ ¶§ ¾Õ¿¡ backslash(\)¸¦ ³Ö¾î ÁØ´Ù.
- ´ÙÀ½ character¸¦ quoteÇÏ´Â ackslash´Â double quote³»¿¡¼ÀÇ °æ¿ì
$, `, ", ¶Ç´Ù¸¥ \ ÀÇ special meaningÀ» ¾ø¾ÖÁØ´Ù.
* C-ShellÀÇ °æ¿ì
- C-Shell¿¡¼ metacharacterÀÇ Àǹ̸¦ Áö¿ö¹ö¸®´Â °¡Àå °·ÂÇÑ °ÍÀº
backslash(\)ÀÌ´Ù.
- ´ÙÀ½À¸·Î °·ÂÇÑ °ÍÀÌ single quoter(')·Î
bang(!)¸¸ÀÌ single quoter¿¡¼µµ °íÀ¯ÀÇ Àǹ̸¦ ¼öÇàÇÑ´Ù.
ÀÌ Àǹ̸¦ ¾ø¾Ö°í ´ÜÁö ´À³¦Ç¥(°¨Åº»ç)ÀÇ ÀÇ¹Ì µûÀ§·Î¸¸ Ç¥ÇöÇÏ·Á¸é
backslash¸¦ ¾Õ¿¡ µÖ¾ß ÇÑ´Ù.
<% echo '!'> ¸¦ ¼öÇàÇÏ¸é ¶ó´Â message°¡ »ý±ä´Ù.
<% echo '\!'>·Î Çϸé !ÀÌ Á¦´ë·Î echoµÉ °ÍÀÌ´Ù.
- double quoter ³»¿¡¼µµ ƯÁ¤ Àǹ̰¡ Á¦°ÅµÇÁö ¾Ê´Â ¼¼°¡Áö (' $ !) ¹®ÀÚµéÀ»
¿ø·¡ ÀǹÌ(single quotation mark, dollar sign, °¨Åº»ç)·Î »ç¿ëÇÏ·Á¸é
backslash¸¦ ¾Õ¼¼¿î double quoterº¸´Ù´Â single quoter ³»¿¡ backslash¸¦ ¾Õ¼¼¿ö
»ç¿ëÇÏ´Â °ÍÀÌ ÁÁ´Ù(!?).
2. Variable
--------------------------------------------------------------------
Shell C-Shell
--------------------------------------------------------------------
name=value¡¡ set name=value
export name setenv name value
name=`expr¡¡$name + $name` @ name=$name + $name
read name set name=$<
echo $name
--------------------------------------------------------------------
3. Argument
-------------------------------------------------------------------------
Shell C-shell ³» ¿ë
-------------------------------------------------------------------------
$$ : ÇöÇà ½©ÀÇ ÇÁ·Î¼¼½º ¹øÈ£¡¡¡¡¡¡
$name : º¯¼öÀÇ °ª
$# $#argv : argumentÀÇ °¹¼ö
$? $status : ÃÖ±Ù ¸í·É¾îÀÇ »óÅ Ãâ·Â
$* : Àμö ´ëÀÀ¡¡¡¡
$argv[*]
$argv
$- : ÇöÇà ½©ÀÇ¡¡flag¡¡¡¡¡¡¡¡¡¡¡¡
$! : ¹é±×¶ó¿îÀÇ ÃÖ±Ù ÇÁ·Î¼¼½º ¹øÈ£¡¡
$?name : º¯¼öÀ̸§ÀÇ ¼±¾ð ¿©ºÎ
$#name : º¯¼öÀ̸§ÀÇ ¾îÈÖ °¹¼ö
$< : Ç¥ÁØÀÔ·ÂÀ¸·ÎºÎÅÍ ÀÔ·Â ´ëÀÀ
-------------------------------------------------------------------------
4. Operators
+-----------------------------+----------------+----------------+
| Operator Á¾ ·ù | C shell | Bourne shell |
+=============+===============+================+================+
| | | < -lt |
| | Number only | > -gt |
| | | <= -le |
| | | >= -ge |
| Relational |---------------+----------------+ |
| Operator | Number or | == | -eq |
| | String | != | -ne |
| |---------------+----------------+----------------+
| | String only | =~ |
| | | !~ |
+-----------------------------+---------------------------------+
| | && -a |
| Logical Operator | || -o |
| | ! |
+-----------------------------+---------------------------------+
| | + |
| | - |
| Arithmetic Operator | * |
| | / |
| | % |
+-----------------------------+---------------------------------+
| | = |
| | += |
| Assignment Operator | -= |
| | *= |
| | /= |
| | %= |
+---------------------------------------------------------------+
5. Redirection +------------------+--------------------+
| Shell | C-Shell |
+===========================+==================+====================|
| Standard In | < file | < file |
| to file | 0< file | |
+---------------------------+------------------+--------------------+
| Standard Out | > file | > file |
| to file | 1> file | |
+---------------------------+------------------+--------------------+
| Standard Error | 2> file | >& file |
| to file | | |
+---------------------------+------------------+--------------------+
| Stdout and Error | ¨ç > file 2>&1 | ( > out) >& err |
| to file | ¨è 1> out 2> err | |
+---------------------------+------------------+--------------------+
| Append stdout to file | >> file | >> file |
+---------------------------+------------------+--------------------+
| Append stdout and error| | >>& file |
| to file | | |
+---------------------------+------------------+--------------------+
¨çÀÇ °æ¿ì, stdout°ú error¸¦ fileÀ̶ó´Â µ¿ÀÏÇÑ ÈÀÏ·Î º¸³»¶ó´Â °ÍÀÌ´Ù.
ÀÌ·± °æ¿ì´Â admin°ü·Ã script¿¡¼ ¸¹ÀÌ º¼ ¼ö Àִµ¥,
¿©±â file¿¡ /dev/console, /dev/nullÀ» ³Ö¾î ¸¹ÀÌ »ç¿ëÇÑ´Ù.
¨èÀÇ °æ¿ì, stdoutÀº outÀ̶õ ÈÀÏ·Î º¸³»°í
error´Â errÀ̶ó´Â ÈÀÏ·Î º¸³»¶ó´Â °Í.
exam) µ¿ÀÏÇÑ ÀÇ¹Ì :
- iflayer xpkt0 hdlc0 zss0 > /dev/console 2>&1
- iflayer xpkt0 hdlc0 zss0 > /dev/console 2> /dev/console
6. ÇÁ·Î±×·¥ ±¸¹®µé ¡¡
1) if¹®
------------------------------------------------------------------------------
| Shell ¡¡ C-Shell
-------------+----------------------------------------------------------------
| if list if ( expr ) then
if | then command
| command command
±¸ ¹® | command ...
| ... endif
| fi
-------------+----------------------------------------------------------------
| if list if ( expr ) then
| then command
if-else | command command
| command command
±¸ ¹® | ... ...
| else else
| command command
| ... ...
| fi endif
-------------+----------------------------------------------------------------
| if list if ( expr ) then
| command command
if-elif | ... ...
| elif list else if ( expr ) then
| then command
±¸ ¹® | command ...
| ... else
| else command
| command ...
| ... endif
| fi
------------------------------------------------------------------------------
2) while¹®¡¡
-------------------------------------------------
Shell C-Shell
-------------------------------------------------
while list while ( list )
do command
command ...
... end
done
-------------------------------------------------
3) case and switch¹®¡¡
-------------------------------------------------------------------------
Shell C-Shell
-------------------------------------------------------------------------
case name in switch ( $variable )
pattern ) case constant :
command command
... ...
;; braaksw
pattern | pattern ) case constant :
command command
;; breaksw
* ) command default :
... command
;; breaksw
esac endsw
-------------------------------------------------------------------------
4) for¹®¡¡
-------------------------------------------------------------------------
Shell C-Shell
-------------------------------------------------------------------------
for name in word foreach variable ( list )
do command
command command
... ...
done end
-------------------------------------------------------------------------
Revision History
Created on Jan. 4 ,1993
Created on Jan. 9 ,1993