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