C shellcsh1970Unix1978 2BSD  BSD UNIX [2][3][4]UNIX V6  /bin/sh Bourne shell (UNIX V7) 
C shell
作者 ビル・ジョイ
初版 1978年
最新版

tcsh 6.18.00 / 2012年1月14日 (12年前) (2012-01-14)[1]

リポジトリ ウィキデータを編集
プログラミング
言語
C
対応OS BSD, UNIX, Linux, macOS
種別 Unixシェル
ライセンス BSDライセンス
テンプレートを表示

Unixcsh1980使CUnix

macOS  Red Hat Linux cshtcshtcsh "csh"  "tcsh" tcsh

DebianUbuntucshtcsh2BSDcsh[5][6]tcsh[7][8]csh使update-alternatives

tcshTenex "t"  Tenex [9]tcshcsh[10]tcshtcsh11[11]

設計目標と機能

編集

C shell の主たる設計目標は、C言語に似せることと、対話型利用での改良であった。

C言語風のスタイル

編集

UnixCC shell C

CcshC8090AT&TshC shell 
Bourne shell C shell
#!/bin/sh
if [ $days -gt 365 ]
then
   echo This is over a year.
fi
#!/bin/csh
if ( $days > 365 ) then
   echo This is over a year.
endif

sh[testshifthenthenifelsetest "test"  "[" testshshALGOL 68 [12]

cshC使C

2110
Bourne shell C shell
#!/bin/sh
i=2
j=1
while [ $j -le 10 ]
do
   echo '2 **' $j = $i
   i=`expr $i '*' 2`
   j=`expr $j + 1`
done
#!/bin/csh
set i = 2
set j = 1
while ( $j <= 10 )
   echo '2 **' $j = $i
   @ i *= 2
   @ j++
end

shshexpr使C shell  @ "at-sign-ment" 

switch
Bourne shell C shell
#!/bin/sh
for i in d*
do
   case $i in
      d?) echo $i is short ;;
      *) echo $i is long ;;
   esac
done
#!/bin/csh
foreach i ( d* )
   switch ( $i )
      case d?:
         echo $i is short
         breaksw
      default:
         echo $i is long
   endsw
end

shのスクリプトでは、";;" で各ケースの終りを示す。通常は空文を許さないため、これはケースの終りを目立たせるためである。

対話型利用のための改良点

編集

C shell cdpathUnix



2 "!!"  "!$" 



/



 C shell  "fgrep"  "f" 







 "~" 



Esc使

cdpath

PATHcdcdcdpath調



1980使sh1C shell Ctrl-Z  C shell fg 



PATHC shell "rehash" 使

スクリプト言語としての C Shell

編集

C shell は行単位で操作する。各行を字句解析して空白、括弧、パイプやリダイレクトを表す記号、セミコロン、アンパサンド等で区切られた単語の並びとして認識する。

基本構文

編集

"echo" 





Unix使
* 

? 1

[...] 

[!...] 

csh便Unix
abc{def,ghi} abcdef  abcghi 

~ 

~user  user 

 "*/*.c" 

Unix1使execWindowsUnicode32KCmain()CMS-DOSMS-DOS128



cshcsh//csh使
> file  file 

>& file  file 

>> file  file 

>>& file  file 

< file file 

<< string string 



1
; 12

&& 102

|| 102



2
| 

|& 



 "$" 使




\ 

"string" 

'string' 



使
`command`  command 




command & command 




( commands ) commands 

制御構造

編集

csh  if switchwhile foreach repeat 

if

編集

if 文には2つの形式がある。短い形式は1行で済むが、式が真の場合に実行できるコマンドはひとつだけである。

if () コマンド

長い形式は thenelseendif というキーワードを使いコマンドの並んだブロックを形成でき、その中でさらに条件分岐を入れ子にすることもできる。

if (式1) then
  コマンド11
  コマンド12
  コマンド13
  ...
else if (式2) then
  コマンド21
  コマンド22
  コマンド23
  ...
else
  コマンドn1
  コマンドn2
  コマンドn3
  ...
endif

elseif が同じ行に出現する場合、csh はそれを入れ子というよりも連鎖として扱う。つまり endif はひとつでよい。

switch

編集

switch 文は文字列をパターンの一覧と比較する。パターンにはワイルドカード文字を含んでもよい。どれもマッチしない場合 default アクションを実行し、マッチすればその部分を実行する。

switch (文字列)
  case パターン1:
    コマンド列11
    コマンド列12
    コマンド列13
         :
  breaksw
  case パターン2:
    コマンド列21
    コマンド列22
    コマンド列23
         :
  breaksw
     :
  default:
    コマンド列n1
    コマンド列n2
    コマンド列n3
         :
endsw

while

編集

while 文は式を評価する。その結果が真なら続くコマンド群を実行し、再び式の評価に戻る。

while ()
  コマンド1
  コマンド2
  コマンド3
  ...
end

foreach

編集

foreach 文は値の一覧(通常はワイルドカードで生成されたファイル名一覧)をとり、それぞれの値について値をループ変数に設定し、続くコマンド群を実行する。

foreach ループ変数 (値1 値2 値3 ... 値n)
  コマンド1
  コマンド2
  コマンド3
  ...
end

repeat文

編集

repeat文は「整数値」で指定された回数だけ「コマンド」(ひとつのコマンド)を繰り返し実行する。

repeat 整数値 コマンド

変数

編集

csh  setenv exec 

 set  @  csh 使csh 使

 csh 

C shell C32 $name 

CCCC shell 
// C groups from the left
// prints 4
int i = 10 / 5 * 2;
printf( "%d\n", i );
// prints 5
i = 7 - 4 + 2;
printf( "%d\n", i );
// prints 16
i = 2 >> 1 << 4;
printf( "%d\n", i );
# C shell groups from the right
# prints 1
@ i = 10 / 5 * 2
echo $i
# prints 1
@ i = 7 - 4 + 2
echo $i
# prints 0
@ i = ( 2 >> 1 << 4 )
echo $i

C shell での括弧はビットシフト演算子と入出力リダイレクトを混同しないために使用している。どちらの言語でも括弧を使えば評価順序を明確化できる。なお先述した通り、シェル変数の値は文字列であり、@ 文などの式の中でだけ文字列を数値に変換して評価し、結果を文字列に変換して変数に格納している。

批判

編集

csh 1980cshUnixshUnixsh1990cshPOSIXcsh使[13]1POSIX KornShell C shell [14][15]



setsetenvalias set setenv  alias 使set setenv  alias if  endifswitch  endsw end 



Bourne shell 使csh 1C C shell C Bourne shell 使



1970[16]使C shell 2009Unix[17] C shell 

C shell  foreach  grep foreach 使csh

'myfile' 'mytext'  C shell myfile 調
# Works as expected
if ( ! -e myfile ) then
   echo mytext > myfile
endif
# Always creates an empty file
if ( ! -e myfile ) echo mytext > myfile

また、エラーメッセージが貧弱だという点もよく批判されている。例えば "0 event not found" というメッセージからは何が問題なのかもわからない。

影響

編集

Unixkshbashshcsh2tcshcshtcsh

1986 On Command: Writing a Unix-Like Shell for MS-DOS[18] "SH" shcshSHUnixcatcpgrep 2530SH C shell 

1988Hamilton Laboratories OS/2 Hamilton C shell [19]1992 Windows NT [20]Windows[21]OS/22003Hamilton C shell  Nicole Hamilton cshUnix[22]C shell UnixPCPCPC

 C shell 使HTK[23]csh使

脚注

編集


(一)^ Zoulas, Christos (Jan 14 2012), tcsh-6.18 is now available, http://mx.gw.com/pipermail/tcsh/2012-January/004523.html 2012116 

(二)^ Harley Hahn, Harley Hahn's Guide to Unix and Linux.

(三)^ Berkeley Engineering Lab Notes, Volume 1, Issue 2, October 2001.

(四)^ An Introduction to the C shellbyBill Joy.

(五)^ https://packages.debian.org/buster/csh

(六)^ https://packages.ubuntu.com/focal/csh

(七)^ https://packages.debian.org/buster/tcsh

(八)^ https://packages.ubuntu.com/focal/tcsh

(九)^ Ken Greer (3 October 1983). "C shell with command and filename recognition/completion". Newsgroup: net.sources. 20101229

(十)^ tcsh(1) man page

(11)^ Fixes file in tcsh-6.17.00.

(12)^ Re: Late Bloomers Revisited USENET post to comp.lang.misc by Piercarlo "Peter" Grandi, Dept of CS, UCW Aberystwyth, UK, Dec 17, 1989.

(13)^ IEEE Standard for Information Technology, Portable Operating System Interface (POSIX), Part 2: Shell and Utilities, Volume 2. IEEE Std 1003.2-1992, pp. 766-767. ISBN 1-55937-255-9.

(14)^ Csh Programming Considered Harmful by Tom Christiansen

(15)^ Top Ten Reasons not to use the C shell by Bruce Barnett

(16)^ David Gries (1971). Compiler Construction for Digital Computers. John Wiley & Sons. ISBN 0-471-32776-X.

(17)^ Bill Joy in Conversation with Brent Schlender, Churchill Club, Santa Clara, CA, Feb 11, 2009.

(18)^ Holub, Allen (1986, 1987). On Command: Writing a Unix-Like Shell for MS-DOS (Second ed.). M&T Books, Redwood City, CA. ISBN 0-934375-29-1 

(19)^ Hamilton, Douglas. Hamilton C shell Announcement. IBM Personal Systems Developer (Summer 1989): 119121. http://hamiltonlabs.com/archives/Hamilton-C-Shell-Announcement-Douglas-A-Hamilton-IBM-Personal-Systems-Developer-Summer-1989.pdf 20101122. 

(20)^ Hamilton C shell for Windows Release Notes 4.0, retrieved June 19, 2010.

(21)^ Oliver, Robert (2009919). Hamilton C Shell 2009  The Missing Shell for Windows. 20102102010624

(22)^ Hamilton C shell Quick Reference. Hamilton Laboratories, Wayland, MA. (1988 - 1990 (Revised July 10, 1990)). http://hamiltonlabs.com/archives/1990-07-10-Hamilton-C-shell-Quick-Reference.pdf 20101122 

(23)^ htk

参考文献

編集

G.P.UNIX C SHELL 198710ISBN 4-89362-029-0https://www.personal-media.co.jp/book/lang/029.html 

Paul DuBois csh & tcsh鹿20022ISBN 4-87311-073-4https://www.oreilly.co.jp/books/4873110734/ 

Arnold RobbinsNelson H. F. Beebe20061ISBN 4-87311-267-2https://www.oreilly.co.jp/books/4873112672/ 

Wang, Paul (1988). An Introduction to Berkeley UNIX. Wadsworth Pub. Co.. ISBN 0-534-08862-7 

Arick, Martin R. (1993). UNIX C Shell Desk Reference. John Wiley & Sons. ISBN 0-471-55680-7 

Introduction to C Shell Programming. Canisius College Computer Science Department. 2010623

関連項目

編集

外部リンク

編集