2007年06月22日 情報科学類 コンピュータリテラシ 筑波大学 システム情報工学研究科 コンピュータサイエンス専攻, 電子・情報工学系 新城 靖 <yas@is.tsukuba.ac.jp>
このページは、次の URL にあります。
http://www.coins.tsukuba.ac.jp/~yas/coins/literacy-2007/2007-06-22
あるいは、次のページから手繰っていくこともできます。
http://www.coins.tsukuba.ac.jp/~yas/
http://www.cs.tsukuba.ac.jp/~yas/
例: 大量のファイルを削除する
% rm -rf dir &
[1] 7433
%
コマンドの末尾に 「&」を付けると、バックグランドで実行され
る。ジョブ番号とプロセス番号が表示され、すぐにプロンプトが洗われる。
終了すると次のような表示がなされる。
[1] Done rm -rf dir
% date ; sleep 10 ; date; who
Fri May 19 06:00:52 JST 2006
Fri May 19 06:01:02 JST 2006
yas ttyp0 May 18 22:42 (sharon.hlla.is.t)
%
例: /bin と /usr/bin にあるファイルをしらべてホーム・ディレクトリにある commands というファイルに保存する。
% cd
% (cd /bin; ls; cd /usr/bin; ls ) > commands
% ls -ld /
drwxrwxr-t 34 root wheel 1190 May 12 09:34 /
% echo $status
0
% ls -ld /aaaa
ls: /aaaa: No such file or directory
% echo $status
1
%
コマンドが成功したか失敗したかを調べ、その結果に応じて次のコマンドを実
行するかしないかを決めたい。
command1 が成功したら、command2 を実行する。 (できれば、両方とも実行したい。)
command1 && command2
command1 が失敗したら、command2 を実行する。
(どちらか一方が実行できればよい)
command1 || command2
別名。1つのものに2つ以上の名前をつける。
例:ls -l
の代わりに ll
というコマンドを使えるようにする。
% ls -l
total 80
-rw-r--r-- 1 yas prof 281 Jun 22 13:14 Imakefile
-rw-r--r-- 1 yas prof 1072 Jun 22 13:31 Makefile
-rw-r--r-- 1 yas prof 14044 Jun 22 13:31 index.html
-rw-r--r-- 1 yas prof 10681 Jun 22 13:30 index.html-m4
-rw-r--r-- 1 yas prof 10129 Jun 22 13:14 index.html-m4.~1~
% ll
tcsh: ll: Command not found.
% alias ll ls -l
% ll
total 80
-rw-r--r-- 1 yas prof 281 Jun 22 13:14 Imakefile
-rw-r--r-- 1 yas prof 1072 Jun 22 13:31 Makefile
-rw-r--r-- 1 yas prof 14044 Jun 22 13:31 index.html
-rw-r--r-- 1 yas prof 10681 Jun 22 13:30 index.html-m4
-rw-r--r-- 1 yas prof 10129 Jun 22 13:14 index.html-m4.~1~
%
alias の機能
% alias
% alias ll
% unalias ll
~/.cshrc
(csh run command) に含まれて
いる命令を実行する。
alias の定義は、tcsh (csh) が終了すると、消えてしまう。 複数の端末で、複数の tcsh を実行しても、定義した端末だけ有効になるだけである。 他の端末では、定義されない。
毎回手作業で定義したくないなら、ファイル~/.cshrc
に定義を含める
とよい。csh は、実行されると、まずそのファイルに含まれているコマンドを
実行する。
tcsh は、csh にコマンドラインの編集機能がついたものである。tcsh は、
csh 用の設定ファイル ~/.cshrc
を参照する。
#
# standard ~/.cshrc for coins users
#
if( -r /usr/local/lib/standard/cshrc ) then
source /usr/local/lib/standard/cshrc
endif
演習などで、このファイルを消してしまった時には、次のようにすると回復できる。
% cd ~
% mv .cshrc cshrc-bad
% cp /usr/local/lib/standard/cshrc-home .cshrc
%
'\!*'
」のよ
うに書く。「\」は、「\」か「¥」(ASCII で 5c(16進数))
例:引数で与えられたディレクトリを ls -l -t で調べて、そのうちの先頭の 10行だけを表示する。
% alias new10 'ls -l -t \!* | head'
% new10 /bin
total 8824
-rwxr-xr-x 1 root wheel 40408 Feb 19 18:09 launchctl
-r-xr-xr-x 1 root wheel 896720 Aug 23 2005 ksh
-rwxr-xr-x 2 root wheel 491340 Aug 23 2005 zsh
-rwxr-xr-x 2 root wheel 491340 Aug 23 2005 zsh-4.2.3
-r-sr-xr-x 1 root wheel 24736 Aug 22 2005 rcp
-r-xr-xr-x 1 root wheel 14440 Aug 22 2005 domainname
-r-xr-xr-x 2 root wheel 18104 Aug 22 2005 [
-r-xr-xr-x 2 root wheel 18104 Aug 22 2005 test
-r-xr-xr-x 1 root wheel 13964 Aug 22 2005 sleep
%
「!*
」と書きたいが、単に「!
」を使うと、ヒストリが展開されてしまうので、
「\!
」と書く。また、「*
」がファイル名に展開されないように、
シングルクォート「''
」で括る。
「\!:1
」のように、引数を区別する機能もある。
alias では、パイプ「|」もよく使われる。
% which md
/usr/bin/md
% man md
md(1) BSD General Commands Manual md(1)
NAME
md -- process raw dependency files produced by cpp -MD
SYNOPSIS
md [-d] [-f] [-m makefile] [-u makefile] [-o outputfile] [-v] [-x]
[-D c|d|m|o|t|D]
...
% alias md mkdir
% which md
md: aliased to mkdir
%
alias を無効にして、元のコマンドを使いたい時には、コマンド名の
前に「\」をつける。または、コマンド名をフルパスで打つ。
% md
usage: mkdir [-pv] [-m mode] directory ...
% \md
usage: md -f -Dcdmot -m makefile -o outputfile -v ...
% /usr/bin/md
usage: md -f -Dcdmot -m makefile -o outputfile -v ...
%
% alias ls ls -F
しかし、行の先頭以外では、Alias loop というエラーになる。
% alias ls 'dirs; ls \!*'
% ls
tcsh: Alias loop.
%
この時には、alias を展開させないために、コマンド名の前に「\」を使う。
% alias ls 'dirs; \ls \!*'
% ls
~/public_html/coins/literacy-2006/2006-06-23
Imakefile index.html index.html-m4.~1~
Makefile index.html-m4
%
コマンド名をフルパスで書く方法もある。
% alias ls 'dirs; /bin/ls \!*'
% ls
~/public_html/coins/literacy-2006/2006-06-23
Imakefile index.html index.html-m4.~1~
Makefile index.html-m4
%
tcsh は、csh に terminal での編集機能や補完機能を付けたもの。シェル・ スクリプトを書く時には、多くのシステムで備わっている /bin/csh を使うこ とが多い。(csh はあるが tcsh がないシステムもある。)
#!/bin/csh
や #!/bin/csh -f
と書き加える。
chmod +x
で実行可能属性を付ける
~/bin
に置く)
csh
ではなくて、sh
(/bin/sh
)で実
行される。sh
と csh
は、文法に共通性もあるので、問題ない
場合も多い。
問題がある場合、どうしても csh
で実行させたい場合、テキスト・エ
ディタで、行の先頭に「#!/bin/csh
」と書き、csh で実行させる。
#!/bin/csh -f
」と、-f
を付けると、~/.cshrc
を読
み込まないので速くなる。ただし、~/.cshrc で定義した設定(aliasや環境変数
の設定)は効かないことがある。
ただし、環境変数は、今の状態を引き継ぎたいことが多いので、
~/.cshrc
を読み込ませない方が都合がよいことが多い。
% cat add
#!/bin/csh -f
@ x = $1 + $2
echo $x
% ./add 10 20
30
% ./add 100 200
300
%
引数は、$1,$2,...
や $argv[1],$argv[2],...
で参照でき
る。$*
では、全ての引数を参照できる。shift
コマンドで
引数をずらすことができる。$#
で引数の数がわかる。
csh では @
で簡単な計算ができる。空白で区切る必要がある。
The Unix Super Text 39.3節 参照。
-x
オプションを付けて実行すると、画面にスクリプトを表示さ
ながら実行する。
% csh -f -x run
-n
オプションを付けて実行して、構文のチェックだけ行う。
シェルに1行ずつ与えて実行してみる方法もある。
~/.cshrc
などを設定して、~/bin
をpath シェル変数(PATH
環境変数)に含まれるようにすることを奨める。そして自分で作成したプログ
ラムやスクリプトを、~/bin
に置くと./
などで実行する必
要はない。
coins では、標準で ~/bin
が path
に含まるように設定されている。
ただし、ファイルを作成し、chmod +x
した後で、1度だけ
rehash
コマンドを打つ必要がある。rehash
と打たれるまで、
tcsh
は、コマンドが追加されたことに気がつかない。
% emacs ~/bin/newcommand
% chmod +x ~/bin/newcommand
% rehash
% newcommand
% emacs ~/bin/newcommand
% newcommand
% emacs ~/bin/newcommand
% newcommand
%
chmod
も rehash
も、シェルごとに1度だけやればよい。端末
をたくさん開いていた時には、作成したスクリプトをすぐに使いたい時にはそ
れぞれのシェルで rehash
コマンドを実行する。
rehash
は、「ハッシュ表を作り直す」ことを意味する。csh は、コマ
ンドを打つたびにpath
で指定されたディレクトリにあるコマンドを探
すのではなく、ハッシュ表と呼ばれる、高速に検索するための仕組みを使って
検索している。
rehash
は、新しいシェルが実行される時には自動的に行われている。
次にログインした時、chmod +x
した後に開いた端末では
rehash
を実行する必要はない。
/bin/sh
) は、Bourne SHell と呼ばれる。対話的な利用では、csh
を使う人でも、シェルスクリプトを作成する時には、sh を使う人も多い。
実際には、Unix オリジナルの sh ではなく、bash (GNU Bourne-Again SHell) という高機能のプログラムが広く使われている。
まず、端末で何かコマンドを実行しなさい。以下の例では、 /Applications/Calculator.app を実行している。
% open /Applications/Calculator.app
(^p で1行もどす。^a で、行頭に移動して echo と打ち、^e して > run-calc と打つ)
% echo open /Applications/Calculator.app > run-calc
% csh run-calc
%
tcsh の機能で、^p (Control+P) で1行戻して、echo でファイルに落とす。
「|」があれば、「'シングルクォート'」で括る(
|''
) などして、エスケープする 。
いちいち csh と打たないでもいいようにするために、chmod
コマンドで
実行可能属性をつける。
% chmod +x run-calc
% ./run-calc
%
必要ならば、エディタで1行目に「#!/bin/csh」か「#!/bin/sh」を入れる。
#!/bin/csh
open /Applications/Calculator.app
echo
でファイルに落すのではなく、alias
コマンドでエイリアスを定義する。
~/.cshrc
に alias のコマンド行を追加する。こ
の時には、echo
でファイルに保存してもよい。
echo
コマンドではなく、history
コ
マンドを使う。
% history -h
(ヒストリの表示して、何行必要か数える。history コマンドの分、1行多く保存する。)
% history -h 5 > run-xxx
% emacs run-xxx
(最後の history コマンドをテキスト・エディタで削る。)
そのプログラムが、一般のプログラム(機械語)かシェル・スクリプトかは、 file コマンドを使うと調べることができる。
% file /usr/bin/apropos
/usr/bin/apropos: Bourne shell script text executable
% ls -l /usr/bin/apropos
-rwxr-xr-x 1 root wheel 2408 Aug 22 2005 /usr/bin/apropos
% lv /usr/bin/apropos
%
if
, if then else endif
foreach
while
switch
exec
eval
exit
source
次の機能を確認しなさい。
jobs
コマンド
fg
コマンド
bg
コマンド
%
によるジョブの指定方法
stop
コマンド (%
による指定)
kill
コマンド (%
による指定)
(1) csh (tcsh) のエイリアスを定義しなさい。エイリアスは、~/.cshrc に保 存しなさい。定義したエイリアスについて、次のことを書きなさい。
(2) 次のような条件を満たすシェル・スクリプトを作成しなさい。
~/bin
に起きなさい。
作成したシェル・スクリプトについて、次のことを書きなさい。
授業で例題として示したものとは別のシェル・スクリプトを作成しなさい。
(3) [加点] 制御構造を含むシェル・スクリプトを作成しなさい。