2007年05月11日 情報科学類 コンピュータリテラシ 筑波大学 システム情報工学研究科 コンピュータサイエンス専攻, 電子・情報工学系 新城 靖 <yas@is.tsukuba.ac.jp>
このページは、次の URL にあります。
http://www.coins.tsukuba.ac.jp/~yas/coins/literacy-2007/2007-05-11
あるいは、次のページから手繰っていくこともできます。
http://www.coins.tsukuba.ac.jp/~yas/
http://www.cs.tsukuba.ac.jp/~yas/
;; This buffer is for notes you don't want to save, and for Lisp evaluation. ;; If you want to create a file, visit that file with C-x C-f, ;; then enter the text in that file's own buffer. (+ 10 20)^J 30
% ls -l > file1シェルは、記号> があると、標準入力をキーボードから指定され たファイルに切り替える。![]()
% cat < file1シェルは、記号![]()
|
があると、次の動作を行う (パイプ(pipe)機能)。
% ps aux | headファイルを介しても可能だけれども、作成したファイルを消さなければならない。<プロセスのリスト> %
![]()
% ps aux > fileパイプは、3つ以上のプロセスを結びつけることもできる。% head < file
<プロセスのリスト> % rm file
![]()
% ps AUX > ps-resultAUX は、間違い。ps コマンドは、エラー・メッセージを標準出力で はなく標準エラー出力に書き出す。ps: X: no such user % cat ps-result
%
![]()
>&
をつかうと、エラー・メッセージも含めてファイルに出
力することもできる。
% ps AUX >& ps-result% cat ps-result
ps: X: no such user %
![]()
|&
を使うと、エラー・メッセージも含めてパイプに出力す
ることもできる。
% ps AUX |& head![]()
操作 | シェル変数 | 環境変数 |
代入 | set name=value | setenv name value |
参照 | $name | $name |
削除 | unset name | unsetenv name |
一覧表示 | set | printenv |
単独表示 | echo $name | echo $name |
printenv name |
シェル変数は、子プロセスへ引き渡されないが、環境変数は、子プロセスへ引 き渡される。環境変数は、子プロセスの動作を変更するために用いる。
% dateマニュアルの ENVIRONMENT VARIABLES に記述されている。Wed May 9 23:03:07 JST 2007 % echo $TZ
tcsh: TZ: Undefined variable. % setenv TZ EST
% date
Wed May 9 09:03:20 EST 2007 % unsetenv TZ
% date
Wed May 9 23:03:25 JST 2007 %
![]()
% man dateDATE(1) BSD General Commands Manual DATE(1) NAME date -- display or set date and time SYNOPSIS ... ENVIRONMENT VARIABLES The following environment variables affect the execution of date : TZ The timezone to use when displaying dates. See environ(7) for more information. ...
% set... path (/home1/prof/yas/bin /usr/local/bin /usr/local3/bin /bin /usr/X11R6/bin /sbin /usr/bin /usr/sbin /usr/local3/ImageMagick/bin) ... % echo $path[1]
/home1/prof/yas/bin % echo $path[2]
/usr/local/bin %
![]()
シェル変数 | 環境変数 |
path | PATH |
home | HOME |
term | TERM |
user | USER |
""
で括ると1単語として扱われる。
% set as="Application Support"% ls -ld ~/Library/$as
ls: /home1/prof/yas/Library/Application: No such file or directory ls: Support: No such file or directory % ls -ld ~/Library/"$as"
drwx------ 11 yas prof 374 May 9 00:27 /home1/prof/yas/Library/Application Support %
![]()
< > | $ { } ( ) [ ] & ; ^ " * ? ~ ' ` 空白
など。
コマンドの引数として渡したい時には、解釈を避ける(エスケープ(escape))た めの特殊な形式を使う。
形式 | エスケープ対象 | エスケープしないメタキャラクタ |
\x | 「\ 」の直後の一文字 | なし |
'str' | 「'」でくくられた文字列 | ! |
"str" | 「"」でくくられた文字列 | !, $ |
\
は、ASCII 5c(16進)の文字。表示は、「\」か「¥」。
ダブルクォート ""
の内部では、$
は解釈され、シェル変数や
環境変数の置き換えられる。シングルクォート
''
では、$
は解釈さない。
% echo "$home"/home1/prof/yas % echo '$home'
$home %
![]()
補完(completion)では、人間が目で確認するが、置き換えでは人間が確認する ことはない(見つからなければエラーになる)。
例:/usr/bin にある a で始まるファイルをすべて ls コマンドに引き渡したい。
% cd /usr/bin% ls a*
a2p ant as atq automake-1.6 aclocal appleping asa atrm automator aclocal-1.6 appletviewer at atstatus autoreconf addftinfo apply at_cho_prn autoconf autoscan afmtodit apropos atlookup autoheader autoupdate alias ar atos autom4te auval amlint arch atprint automake awk %
![]()
パタン | 意味 |
* | 任意の文字列(空でもよい)(.で始まるものを除く) |
? | 任意の1文字 |
[str] | strのなかの1文字。たとえば [aA] は、a か A とマッチする。「-」があると、ASCIIでその間の文字を意味する。たとえば [0-9](数字)や[a-zA-Z](アルファベット)がよく使われる。 |
{str1,str2,...} | 「,」で区切られたパタン str1, str2, ... を順にファイル名置換した結果を並べたもの |
~username | ユーザusernameのホーム・ディレクトリの絶対パス。 |
~/ | 自分自身のホーム・ディレクトリ |
~ | 自分自身のホーム・ディレクトリ |
% historyシェル変数 history の個数だけ記憶している。2 22:43 cd public_html/coins/literacy-2007/2007-05-11 3 22:43 ps 4 22:43 ps x ... 100 6:20 ls 101 6:20 history %
![]()
^P
/ ^N
で、ヒストリを
さかのぼれる。
その他に、次のような方法もある。tcsh では、ほとんど使われない。編集機能 がない csh では有効であった。
% calMay 2007 S M Tu W Th F S 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 % cal > cal-2007-05
% ls -l cal-2007-05
-rw-r--r-- 1 yas prof 136 May 9 22:56 cal-2007-05 % cat cal-2007-05
May 2007 S M Tu W Th F S 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 %
![]()
動作例:
% bcbc コマンドは、プロンプトを表示しないが、ユーザが打ち込んだ数式を行単位 で計算して表示する。終了するには、入力の終わり(end of file) を意味するbc 1.06 Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc. This is free software with ABSOLUTELY NO WARRANTY. For details type `warranty'. 10+20
30 ^D %
![]()
^D
(Control+D) を押す。コントロール・キーを押しながら、
D キーを押す。
次のような機能が利用できる。
% cat > expressions次に作成した数式を、 キーボードの代わりにファイルからデータを読み込ませる。10+20
2^40
^D % cat expressions
10+20 2^40 %
![]()
% bc < expressions30 1099511627776 %
![]()
% ps aux | lv![]()
% ps aux | head![]()
% ls -l aux | lv![]()
% ls cal-2007-567ls: cal-2007-567: No such file or directory % cal 5 2007 > cal-2007-567
% cal 6 2007 >> cal-2007-567
% cal 7 2007 >> cal-2007-567
% cat cal-2007-567
May 2007 S M Tu W Th F S 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 June 2007 S M Tu W Th F S 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 July 2007 S M Tu W Th F S 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 %
![]()
% ls /ここまで打ったら、行末で^Dを押してみる。![]()
% ls /^Dすると、ls と同じような表示がなされる。![]()
% ls / Applications/ Network/ bin/ home2/ sbin/ Desktop DB System/ cores/ mach.sym tmp@ Desktop DF Users/ dev/ mach@ usr/ Developer/ Volumes/ etc@ mach_kernel var@ Library/ automount/ home1/ private/ % ls /uから始まるものは、1個しかないので 「u」とうち、タブ・キーを打つ。タブ キーの代わりに (^I)でもよい。![]()
% ls /u^Iすると、sr/ が補完される。![]()
% ls /usr/^D を打つ![]()
% ls /usr/^Dすると、/usr の内容が表示される。![]()
% ls /usr/ X11R6/ epkg@ info/ libexec/ local@ share/ bin/ include/ lib/ local3@ sbin/ standalone/ % ls /usr/「lo タブ」とうつと、![]()
% ls /usr/lo^I次のように「cal/」が補完される。![]()
% ls /usr/local同様に、^D と打つと、一覧が表示される。![]()
% ls /usr/local^D/b タブとうつ。local3@ local@ % ls /usr/local
% ls /usr/local/b^Iin/「in/」が補完される。![]()
% ls /usr/local/bin/ここで ^D を押しても数が多いので参考にならない。emくらいを入れ てタブを押す。![]()
% ls /usr/local/bin/em^Iビープ音がなり、補完されない。 ^Dで様子をみる。![]()
% ls /usr/local/bin/em emacs* emacsclient* emcws-22.0.50* % ls /usr/local/bin/ema タブとうつ。![]()
% ls /usr/local/bin/ema^I「cs」が補完される。![]()
% ls /usr/local/bin/emacs
% ps aux | lvlv については、手引き 2.2.10 参照。![]()
% head /etc/services# # Network services, Internet style # # Note that it is presently the policy of IANA to assign a single well-known # port number for both TCP and UDP; hence, most entries here have two entries # even if the protocol doesn't support UDP operations. # # The latest IANA port assignments can be gotten from # # http://www.iana.org/assignments/port-numbers % tail /etc/services
nimhub 48002/tcp # Nimbus Hub nimgtw 48003/udp # Nimbus Gateway nimgtw 48003/tcp # Nimbus Gateway # Carstein Seeberg
# 48004-48555 Unassigned com-bardac-dw 48556/udp # com-bardac-dw com-bardac-dw 48556/tcp # com-bardac-dw # Nicholas J Howes # 48557-49150 Unassigned # 49151 IANA Reserved % ![]()
% grep root /etc/passwdps aux, ps auxw の結果を grep で検索し、特定の文字列 (たとえば sh や自分のログイン名)が現れるものを表示しなさい。root:*:0:0:System Administrator:/var/root:/bin/sh daemon:*:1:1:System Services:/var/root:/usr/bin/false % cat /etc/passwd | grep root
root:*:0:0:System Administrator:/var/root:/bin/sh daemon:*:1:1:System Services:/var/root:/usr/bin/false %
![]()
grep コマンドでよく使うオプション
1行に1つ、なにかの名前(人名、果物の名前)を含むファイルを作成しなさい。 sort コマンドを使って、並べ替えなさい。
ls -l の結果は、ファイル名の順番でソートされている。これをファイルの大 きさでソートしなさい。
sort コマンドでよく使うオプション
% lsパイプに出力する時にも、画面と同様の表示をさせるには、-C オプションを付 ければよい。% ls | cat
% ls | lv
![]()
% ls -C% ls -C | cat
% ls -C | lv
![]()
% set![]()
% echo $homeその他に、path, shell, status, user 等のシェル変数を表示しなさい。/home1/prof/yas %
![]()
% ps U $userこの方法と U に自分のログイン名を打つ方法、x オプションを使う方法を比較 しなさい。出力結果や打ちやすさ覚えやすさを比較しなさい。![]()
% ps U ログイン名% ps x
![]()
% set u=/Applications/Utilities% echo $u
/Applications/Utilities % ls $u
Activity Monitor.app Java AirPort Admin Utility.app Keychain Access.app AirPort Setup Assistant.app NetInfo Manager.app Audio MIDI Setup.app Network Utility.app Bluetooth File Exchange.app ODBC Administrator.app ColorSync Utility.app Print Center.app Console.app Printer Setup Utility.app DigitalColor Meter.app System Profiler.app Directory Access.app Terminal.app Disk Utility.app VoiceOver Utility.app Grab.app X11.app Grapher.app iPod Software Updater.localized Installer.app %
![]()
% printenv![]()
例:
% printenv HOMEその他に、SHELL, TERM, NNTPSERVER, PRINTER 等の値を表示しなさい。% echo $HOME
![]()
% echo $path利用可能なコマンドの一覧を表示しなさい。% echo $PATH
% ls -ld $path
![]()
% ls $path > commands% lv commands
![]()
% ls $path | lv存在しないディレクトリについてのエラー・メッセージは、標準エラー出力に 出力され、ファイルやパイプには現れない。![]()
% history次の方法で再実行してみないさい。![]()
^P
/^N
で選択して % ps uxwUSER PID %CPU %MEM VSZ RSS TT STAT STARTED TIME COMMAND yas 12144 0.0 -0.0 27396 520 p0 S 8:38AM 0:00.17 grotty yas 10897 0.0 -0.1 31872 1148 p0 Ss 10:42PM 0:02.05 -tcsh yas 11759 0.0 -0.1 31896 1200 p1 Ss+ 6:22AM 0:00.50 -csh yas 12137 0.0 -0.0 27292 436 p0 S 8:38AM 0:00.04 man tcsh yas 12138 0.0 -0.0 27804 556 p0 S 8:38AM 0:00.01 sh -c (cd /usr/share/man && /usr/bin/tbl /usr/share/man/man1/tcsh. yas 12139 0.0 -0.0 27804 388 p0 S 8:38AM 0:00.01 sh -c (cd /usr/share/man && /usr/bin/tbl /usr/share/man/man1/tcsh. yas 12140 0.0 -0.0 27360 392 p0 S 8:38AM 0:00.02 /usr/bin/tbl /usr/share/man/man1/tcsh.1 yas 12141 0.0 -0.0 27320 388 p0 S 8:38AM 0:00.01 /usr/bin/groff -Wall -mtty-char -Tascii -mandoc -c yas 12142 0.0 -0.0 27344 612 p0 S 8:38AM 0:00.10 /usr/bin/less -is yas 12143 0.0 -0.1 27800 1388 p0 S 8:38AM 0:00.38 troff -Wall -mtty-char -mandoc -c -Tascii yas 10895 0.0 -0.0 30716 600 ?? S 10:42PM 0:01.79 /usr/sbin/sshd -i % ps uxw | cut -c 1-10,65-
USER PID COMMAND yas 108976 -tcsh yas 108959 /usr/sbin/sshd -i yas 121374 man tcsh yas 121381 sh -c (cd /usr/share/man && /usr/bin/tbl /usr/share/man/man1/tcsh. yas 121391 sh -c (cd /usr/share/man && /usr/bin/tbl /usr/share/man/man1/tcsh. yas 121402 /usr/bin/tbl /usr/share/man/man1/tcsh.1 yas 121411 /usr/bin/groff -Wall -mtty-char -Tascii -mandoc -c yas 121420 /usr/bin/less -is yas 121438 troff -Wall -mtty-char -mandoc -c -Tascii yas 121447 grotty yas 122116 cut -c 1-10 yas 117590 -csh %
![]()
% ls -l ~/Library/*/Firefox/Profiles/*/.parentlock% cd ~/Library/*/Firefox/Profiles/*
% pwd
% ls -l .parentlock
![]()
% open /Applications/Calculator.appこれを、次のような方法を利用して、長い文字列をキーボードから打たないで 実行しなさい。![]()
% echo "$prompt"この時、ダブルクォート![]()
""
を付けないとうまく表示できないことがある。この理由を考えなさい。
% echo $promptシェル変数 prompt は、シェルのプロンプトを保持している。 これを変更してみなさい。![]()
マニュアル man tcsh で、prompt 変数の中で次のような表現がどのような意味 を持っているかを調べなさい。
%m
%c
%n
以下の問題について、問題、および、回答をテキスト・ファイルに記述し、 レポート提出ページから提出しなさい。 端末の表示(コマンドの実行結果)は、コピー&ペースト等の機能を使って提出 するファイルに含めなさい。
(1) シェルのファイル名置換機能を使って /usr/bin にある次のようなファイ ルをすべて表示しなさい。結果は、ls または echo コマンドで表示し、コマン ドラインを含めてその結果をレポートに含めなさい。
(2) echo コマンドを使って、次の文字列を画面に表示しなさい。
(3) 次のファイル名(ディレクトリ名も含めたすべて)を、シェル変数やファイ ル名置換を使って短く表現しなさい。最後に ls -ld で表示しなさい。
(4) tcsh の補完機能を使って、次のファイル名を打ち込み、ls -ld で表示し なさい。この時、どのようなキー操作を行ったかを書きなさい。
最終的に次のような結果が得られるはずである。
% <回答>30703212 emacs-21.2.1 30703212 emacs 3976096 php 2455700 jikes 1850988 emacs-undumped %
![]()
(7) The Unix Supr Text の 28.6 節、および、28.8節を読みなさい。そして、 HTML で記述できる文書の構造について3-10行でまとめなさい。
(8) The Unix Supr Text の 28.6 節、および、28.8節を読みなさい。そして、 アンカーについて3-10行でまとめなさい。