2017年06月16日 情報科学類 コンピュータリテラシ 筑波大学 システム情報系 情報工学域 新城 靖 <yas@cs.tsukuba.ac.jp>
このページは、次の URL にあります。
http://www.coins.tsukuba.ac.jp/~yas/coins/literacy-2017/2017-06-16
あるいは、次のページから手繰っていくこともできます。
http://www.coins.tsukuba.ac.jp/~yas/
http://www.cs.tsukuba.ac.jp/~yas/
bash (Bourne-Again SHell) は、sh (Bourne Shell)の機能を強化したもの。 シェル・スクリプトを書く時には、多くのシステムで備わっている /bin/sh を 使うことも多い。(/bin/sh はあるが /bin/bash がないシステムもある。)
例:サイズが大きいファイルの上位 10 個を表示したい。 ls コマンドと sortコマンド と headコマンドを組み合わせる。
$ ls -l | sort -k 5 -nr | head
-rw-r--r-- 1 yas prof 28202 6 9 10:24 exam-result.xlsx
-rw-r--r-- 1 yas prof 10465 5 26 11:02 files
-rw-r--r-- 1 yas prof 8574 5 15 18:24 kana.aiff
drwx------ 75 yas prof 8192 5 20 2014 _OLD_HOME_
drwxr-xr-x 4 yas prof 6144 4 10 11:16 dot
drwx------ 4 yas prof 5120 4 21 14:35 Desktop
-rw-r--r-- 1 yas prof 4320 4 25 18:08 literacy-a4.txt~
-rw-r--r-- 1 yas prof 4307 5 2 17:07 literacy-a4.txt
drwxr-xr-x 71 yas prof 4096 4 10 17:10 coins
drwxr-xr-x 7 yas prof 4096 5 19 2016 public_html
$
echo コマンド、
historyコマンド
あるいは、コピー&ペースト機能を用いて
端末から打ち込んだものを結果をファイルに保存する。以下の例では、echo コ
マンドを使ってファイルを作成している。
$
(^p で1行もどす。)
$ ls -l | sort -k 5 -nr | head
(^a で、行頭に移動して 「echo '」と打ち、^e して
「' > ls-size」 と打つ)
$ echo 'ls -l | sort -k 5 -nr | head' > ls-size10
$ cat ls-size10
ls -l | sort -k 5 -nr | head
$ ls -l ls-size10
-rw-r--r-- 1 yas prof 29 6 15 14:38 ls-size10
$ bash ls-size10
-rw-r--r-- 1 yas prof 28202 6 9 10:24 exam-result.xlsx
-rw-r--r-- 1 yas prof 10465 5 26 11:02 files
-rw-r--r-- 1 yas prof 8574 5 15 18:24 kana.aiff
drwx------ 75 yas prof 8192 5 20 2014 _OLD_HOME_
drwxr-xr-x 4 yas prof 6144 4 10 11:16 dot
drwx------ 4 yas prof 5120 4 21 14:35 Desktop
-rw-r--r-- 1 yas prof 4320 4 25 18:08 literacy-a4.txt~
-rw-r--r-- 1 yas prof 4307 5 2 17:07 literacy-a4.txt
drwxr-xr-x 71 yas prof 4096 4 10 17:10 coins
drwxr-xr-x 7 yas prof 4096 5 19 2016 public_html
$
bash
」と打つのは煩わ
しいことがある。その場合は、次のようにする。
1行目にテキスト・エディタで「#!/bin/bash
」を書き加える。
$ emacs ls-size10
(1行目に「#!/bin/bash
」と書き加える。)
$ cat ls-size10
#!/bin/bash
ls -l | sort -k 5 -nr | head
$
chmod +x
で実行可能属性を付ける
$ ls -l ls-size10
-rw-r--r-- 1 yas prof 41 6 15 14:41 ls-size10
$ chmod +x ls-size10
$ ls -l ls-size10
-rwxr-xr-x 1 yas prof 41 6 15 14:41 ls-size10
$
ファイル名を指定すると、実行することができる。ファイル名としては、明示
的に相対パス名であることを示すために先頭に「./
」を付けるか、ホー
ムディレクトリにあるこを示すために「~/
」を付ける(ホーム・ディレ
クトリにあることを仮定している)。
$ ./ls-size10
-rw-r--r-- 1 yas prof 28202 6 9 10:24 exam-result.xlsx
-rw-r--r-- 1 yas prof 10465 5 26 11:02 files
-rw-r--r-- 1 yas prof 8574 5 15 18:24 kana.aiff
drwx------ 75 yas prof 8192 5 20 2014 _OLD_HOME_
drwxr-xr-x 4 yas prof 6144 4 10 11:16 dot
drwx------ 4 yas prof 5120 4 21 14:35 Desktop
-rw-r--r-- 1 yas prof 4320 4 25 18:08 literacy-a4.txt~
-rw-r--r-- 1 yas prof 4307 5 2 17:07 literacy-a4.txt
drwxr-xr-x 71 yas prof 4096 4 10 17:10 coins
drwxr-xr-x 7 yas prof 4096 5 19 2016 public_html
$ ~/ls-size10
-rw-r--r-- 1 yas prof 28202 6 9 10:24 exam-result.xlsx
-rw-r--r-- 1 yas prof 10465 5 26 11:02 files
-rw-r--r-- 1 yas prof 8574 5 15 18:24 kana.aiff
drwx------ 75 yas prof 8192 5 20 2014 _OLD_HOME_
drwxr-xr-x 4 yas prof 6144 4 10 11:16 dot
drwx------ 4 yas prof 5120 4 21 14:35 Desktop
-rw-r--r-- 1 yas prof 4320 4 25 18:08 literacy-a4.txt~
-rw-r--r-- 1 yas prof 4307 5 2 17:07 literacy-a4.txt
drwxr-xr-x 71 yas prof 4096 4 10 17:10 coins
drwxr-xr-x 7 yas prof 4096 5 19 2016 public_html
$
~/bin
に置くと、他のコマンド(ls, cp,
emacs) 等と同じように実行することができる。
$ mkdir ~/bin
(注意: mkdir は、1度だけ実行すればよい。)
$ ls-size10
-bash: ls-size10: command not found
$ mv ls-size10 ~/bin
$ ls -l ~/bin/ls-size10
-rwxr-xr-x 1 yas prof 41 6 15 14:41 /home/prof/yas/bin/ls-size10
$ ls-size10
-rw-r--r-- 1 yas prof 28202 6 9 10:24 exam-result.xlsx
-rw-r--r-- 1 yas prof 10465 5 26 11:02 files
-rw-r--r-- 1 yas prof 8574 5 15 18:24 kana.aiff
drwx------ 75 yas prof 8192 5 20 2014 _OLD_HOME_
drwxr-xr-x 4 yas prof 6144 4 10 11:16 dot
drwx------ 4 yas prof 5120 4 21 14:35 Desktop
-rw-r--r-- 1 yas prof 4320 4 25 18:08 literacy-a4.txt~
-rw-r--r-- 1 yas prof 4307 5 2 17:07 literacy-a4.txt
drwxr-xr-x 71 yas prof 4096 4 10 17:10 coins
drwxr-xr-x 7 yas prof 4096 5 19 2016 public_html
$
ホームディレクトリにある「ls-size10
」は、そのままファイル名を打
ち込んでも実行できない。mv コマンドで「~/bin/」に移動した後は、
「ls-size10
」で実行可能になる。
シェル・スクリプトを実行する時に、シェル・スクリプトに対して引数を与え ることができる。 The Unix Super Text 40.13.2 参照。
ls-size10 では、カレント・ワーキング・ディレクトリ(.)だけ表示できる。他 のディレクトリを表示したい。また、上位 10 個ではなく、5 個、20 個と数を 変えたい。 シェル・スクリプトの名前を 「lss」 とする。
$ cd ~/bin
$ ls -l ls-size10
-rwxr-xr-x 1 yas prof 41 6 15 14:41 ls-size10
$ ls -l lss
ls: lss: No such file or directory
$ cp ls-size10 lss
$ ls -l lss
-rwxr-xr-x 1 yas prof 41 6 15 14:46 lss
$ emacs lss
(修正)
$ cat lss
#!/bin/bash
ls -l $2 | sort -k 5 -nr | head -$1
$
(cp の時点で x ビットは立っているので chmod +x は不要。)
ls-size10 で ls には引数はなかったが、lss では「$2」を与えている。「$2」
は、コマンドに与えられた引数のうち、2番目のものを意味している。また、
head には -$1 を与えている。
作成したシェルスクリプト「lss
」を実行してみる。
$ cd
$ pwd
/home/prof/yas
$ lss 5 /bin
-r-xr-xr-x 1 root wheel 1315248 2 6 2014 ksh
-r-xr-xr-x 1 root wheel 1228416 7 21 2016 sh
-r-xr-xr-x 1 root wheel 1228336 7 21 2016 bash
-rwxr-xr-x 1 root wheel 530320 2 6 2014 zsh
-rwxr-xr-x 2 root wheel 357984 2 6 2014 tcsh
$ lss 3 /usr/bin
-r-xr-xr-x 1 root wheel 11738080 7 21 2016 emacs
-rwxr-xr-x 1 root wheel 10121104 7 21 2016 php
-r-xr-xr-x 1 root wheel 5308832 2 6 2014 parl5.16
$ lss 10 .
-rw-r--r-- 1 yas prof 28202 6 9 10:24 exam-result.xlsx
-rw-r--r-- 1 yas prof 10465 5 26 11:02 files
-rw-r--r-- 1 yas prof 8574 5 15 18:24 kana.aiff
drwx------ 75 yas prof 8192 5 20 2014 _OLD_HOME_
drwxr-xr-x 4 yas prof 6144 4 10 11:16 dot
drwx------ 4 yas prof 5120 4 21 14:35 Desktop
-rw-r--r-- 1 yas prof 4320 4 25 18:08 literacy-a4.txt~
-rw-r--r-- 1 yas prof 4307 5 2 17:07 literacy-a4.txt
drwxr-xr-x 71 yas prof 4096 4 10 17:10 coins
drwxr-xr-x 7 yas prof 4096 5 19 2016 public_html
$
$1,$2,...
で参照できる。
$*
では、全ての引数を参照できる。
$#
で引数の数がわかる。
shift
コマンドで引数をずらすことができる。
a b c
が与えられた時の実行例。
$ echo $#
3
$ echo $1 $2 $3
a b c
$ shift
$ echo $#
2
$ echo $1 $2 $3
b c
$
#
」を使ってコメントを書くことができる。bash は、
文字「#
」移行、行末まで無視する。
$ echo a b # c d
a b
$ # echo a b c
$
$ date +%A
金曜日
$ echo Today is `date +%A` .
Today is 金曜日 .
$
この例では、バッククォート(` `)で括った部分
が実行され、その結果を echo コマンドの引数に使っている。バッククォート
(` `)の代わりに、「$(コマンド)」という形式も
使える。
$ echo Today is $(date +%A) .
Today is 金曜日 .
$
コンピュータでは、クォート「'xxx'」とバッククォー
ト「`xxx`」は違う。
バッククォートは、シェルスクリプトでもよくつかう。
$ tex=file1.tex
$ basename file1.tex .tex
file1
$ basename $tex .tex
file1
$ base=`basename $tex .tex`
$ echo $base
file1
$
The Unix Super Text 40.22.1 参照。
~/.bashrc
という名前のファイルにあるプログラムを自動的
に実行する。~/.bashrc
では、次のようなことを行う。
~/.bashrc を編集したら、一度、iTerm のウィンドウを問題なく開けるか、確 認すると良い。
~/.bashrc に問題があり、iTerm のウィンドウが開かなくなった時には、次の ような方法で修正する。
$ cat /usr/local/lib/standard/bashrc-home
#
# coins standard ~/.bashrc
#
if [ -f /usr/local/lib/standard/bashrc ]; then
. /usr/local/lib/standard/bashrc
fi
# add your own code below
$
編集内容を有効にするには、次の方法がある。
$ . ~/.bashrc
$ source ~/.bashrc
~/.bashrc
に保存する。
以下の例は、ssh でよくアクセスするホストをシェル変数に登録している。
$ cat ~/.bashrc
...
coins=coins.tsukuba.ac.jp
www=www.$coins
icho=icho.u.tsukuba.ac.jp
...
$
これを使うと、ssh が簡単になる。
$ ssh $www
$ ssh $icho
$ /usr/local3/coins/macosx/bin/emacs
$ /bin/ls
環境変数 PATH にディレクトリ名を登録しておくと、ディレクトリ名を省略す
ることができる。
例:
$ echo $PATH
(省略):/usr/local3/coins/macosx/bin:(省略):/bin:(省略)
$ emacs
$ ls
~/.bashrc
などを設定して、~/bin
を PATH環境変数に含まれ
るようにすることを奨める。自分で作成したプログラムやシェル・スクリプトを
~/bin
に置くと、ファイル名を指定しなくても
(「~/ファイル名
」や
「./ファイル
」打たなくて)、
~/bin
以下の短いファイル名で実行できるようになる。
coins では、標準で ~/bin
が PATH
に含まるように設定されて
いるので、各自設定する必要はない。
$ quota -v
Disk quotas for user yas (uid 1013):
Filesystem 1K blocks quota limit grace files quota limit grace
/home 959318 2764800 3072000 23138 0 0
coins の標準は、上限 3 GB (3*1024 KB)。これを超えると、ファイルを保存で
きなくなる。電子メールも受信できなくなる。
90% の 2.7 GB (2764800 KB) を超える、すなわち、残り 10% を切ると、警告
のメールが飛ぶ仕組みがあるが、メールが飛ばないこともあるので、各自自分
で気をつける。
手引き 10.8 参照。
上のユーザは、959,318 KB 使っている。 ファイル数は、23,138 個。ファイル数の上限は、設定されていない。
quota の limit を超えた時には、 不要なファイルを消す。
注意: MacOSX の「ゴミ箱」に入っているファイルもディスク容量を使用してい る。「ゴミ箱を空にする」という操作を行って初めて使用中のバイト数が減る。
^C
(Control+C)
で強制終了する。
$ du ~
(ホーム・ディレクトリ以下のファイルの容量を表示)
$ du .
(カレントワーキング・ディレクトリ以下のファイルの容量を表示)
$ du -s .
(-s で合計だけ表示)
$ du -s -k .
(-k で KB 単位で表示)
$ du -s -k *
(指定されたディレクトリ(*なのでそこにあるもの全部)単位で合計の表示)
$ du -s -k * | sort -nr | head
(合計を大きい順にソートし、その先頭 10 行を表示)
man du、
手引き 2.8.3 参照、
手引き 10.8 参照。
~/Downloads/
)にファイルを保存することがある。
ここにある不用なファイルは削除すべきである。
~/Library/Caches/
以下に
キャッシュとなるファイルを作成する。quota を圧迫している場合や、アプリ
ケーションが(古いキャッシュを参照して)うまく動作しない場合には、
rm -r等で削除する。
$ du -s ~/Library/Caches/*
1 /home/prof/yas/Library/Caches/Adobe
10516 /home/prof/yas/Library/Caches/Firefox
15642 /home/prof/yas/Library/Caches/Google
40 /home/prof/yas/Library/Caches/Metadata
4 /home/prof/yas/Library/Caches/Mozilla
13 /home/prof/yas/Library/Caches/com.apple.DictionaryManager
11 /home/prof/yas/Library/Caches/com.apple.DictionaryServices
5 /home/prof/yas/Library/Caches/com.apple.Safari
66 /home/prof/yas/Library/Caches/com.apple.nsservicescache.plist
25 /home/prof/yas/Library/Caches/com.apple.preferencepanes.cache
320 /home/prof/yas/Library/Caches/com.apple.preferencepanes.searchindexcache
0 /home/prof/yas/Library/Caches/com.apple.tiswitcher.cache
2120 /home/prof/yas/Library/Caches/com.google.SoftwareUpdate
$
ホーム・ディレクトリ ~
、デスクトップ ~/Desktop
や 書類
のディレクトリ ~/Documents
に、多くのファイルを放置することはよ
くない。ディレクトリを作成し、整理すべきである。
1つの目安は、ls コマンドで 1画面に入らない時には分割する。
ls
ls -l
ls -ld
ls -a
ls file1 file2
ls file*
mkdir dir
rmdir dir
cp file1 file2
cp file1 file2 dir1
cp dir/file1 .
mv file1 file2
mv file1 file2 dir1
mv dir/file1 .
rm file1 file2
$ ls -l
total 1
-rw-r--r-- 1 yas prof 2 6 15 21:48 file1
$ rm -i file1
remove file1? y
$ ls -l
$
シェルスクリプトやbash aliasで、rm を自動的に rm
-i に変えることは、一般的には勧められない。「rm -i y」という操
作が、一連の操作として身に付いてしまうので。
$ ls -l
total 1
-r--r--r-- 1 yas prof 2 6 15 21:44 file1
$ rm file1
override r--r--r-- yas/prof for file1? y
$ ls -l
$
書き込みできないファイルでも、-f オプション(force) をつけると、rm コマ
ンドは、利用者に問い合わせることなく黙って削除する。
$ ls -l
total 1
-r--r--r-- 1 yas prof 2 6 15 21:53 file1
$ rm -f file1
$ ls -l
$
例:
何もしないと、自動的に再帰的な処理を行うプログラムもある。例: tar, tree, find 等。
ディレクトリに対しても mv コマンドは有効である。ディレクトリの名前を mv で変更すると、木構造で考えると、それ以下のファイルの名前を全て変更し たことと同じ効果がある。
ファイルのコピーでは、以下で述べるシンボリック・リンク等の問題があるため、 cp -r では不十分なことが多い。
rm
コマンドに -r
オプションをつけると、指定されたディレク
トリ以下の全てのディレクトリを削除する。
$ rm -r dir
rm -r
でも、書き込みできないファイルが含まれている時には、利用者
に問い合わせる。そのような問い合わせを行うことなく強制的に削除したい時
には、
-f オプション
をつけることもできる。
$ rm -rf dir
rm -r
は非常に危険なので、細心の注意にはらって実行すること。
大量の情報を保存するには、木構造を使うしかない。 しかし、木構造だけではうまくいかない。
図13 こうもりの分類(1)
図14 こうもりの分類(2)
木構造は、ファイルを整理するのに非常に強力な構造である。しかし、それだ けでは、ファイルを整理するには不都合が起きる。それを解消するために、次 のような名前で呼ばれる仕組みが用意されている。
注意:bash, csh の alias とファイル名の alias (Macintosh) は、まったくの別物。
図15 こうもりの分類(別名つき)
木構造を補う方法として、 ハイパーテキスト を使うことがある。
例:java コマンド
$ ls -l /usr/bin/java
lrwxr-xr-x 1 root wheel 74 10 7 2015 /usr/bin/java -> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java
$
シンボリック・リンクは、ls -l で見ると、右端に l (小文字の L) と表示さ
れる。
ファイル /usr/bin/java
を参照すると、
/System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands
にある
java
が使われる。
$ ls -l /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java
-rwxr-xr-x 1 root wheel 54624 2 7 2014 /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java
$
シンボリック・リンクを作成するには、ln -s コマンドを使う。
$ ln -s oldname newname
この結果、newname
でファイルを参照すると、
oldname
というファイルが使われる。(このファイルは、
newname
と oldname
の2つの名前を持つ。)
$ cat file1
cat: file1: No such file or directory
$ echo file1 > file1
$ cat file1
file1
$ ln -s file1 slink1
$ cat slink1
file1
$ ls -l file1 slink1
-rw-r--r-- 1 yas prof 6 6 17 14:20 file1
lrwxr-xr-x 1 yas prof 5 6 17 14:20 slink1 -> file1
$ cp slink1 file2
$ ls -l file2
-rw-r--r-- 1 yas prof 6 6 17 2015 file2
$ cat file2
file1
$
cp -r や cp -R でファイルをコピーする時に、シンボリック・リンクが含まれ
ていると、問題が生じることがある。システム(MacOS, Linux, BSD)によって
cp コマンドの挙動が異なる。シンボリック・リンクをシンボリック・リンクと
してコピーするには、tar コマンド等を使い、cp を使わない方が良い。
$ du ~/.Trash/
1 /home/prof/yas/.Trash//名称未設定フォルダ
26 /home/prof/yas/.Trash/
$
図? コンパイラ方式でのプログラムの実行
図? インタプリタ方式でのプログラムの実行
コンパイラ方式とインタプリタ方式の中間的な方法もある。
図? コンパイラとインタプリタの併用によるプログラムの実行
$ cat ~/.bashrc
#
# coins standard ~/.bashrc
#
if [ -f /usr/local/lib/standard/bashrc ]; then
. /usr/local/lib/standard/bashrc
fi
# add your own code below
$
まず、端末で何かコマンドを実行しなさい。以下の例では、 /Applications/Calculator.app を実行している。
$ open /Applications/Calculator.app
(^p で1行もどす。^a で、行頭に移動して 「echo '」と打ち、^e して
「' > run-calc」 と打つ)
$ echo 'open /Applications/Calculator.app' > run-calc
$ cat run-calc
open /Applications/Calculator.app
$
bash の機能で、^p (Control+P) で1行戻して、echo でファイルに落とす。
echo には、実行したコマンドを
「'シングルクォート'」で括る(
'|'
) などして、エスケープして与えると安全である。
作成したファイルは、そのまま実行できる。
$ bash run-calc
いちいち bash と打たないでもいいようにするために、chmod
コマンド
で実行可能属性をつける。
$ ls -l run-calc
-rw-r--r-- 1 yas prof 34 6 16 15:08 run-calc
$ chmod +x run-calc
$ ls -l run-calc
-rwxr-xr-x 1 yas prof 34 6 16 15:08 run-calc
$ ./run-calc
$ ~/run-calc
$
エディタ(Emacs)で1行目に「#!/bin/bash」か「#!/bin/sh」を入れる。
#!/bin/bash
open /Applications/Calculator.app
完成したコマンドを ~/bin
に移動する。
$ mkdir ~/bin
(注意: mkdir は、1度だけ実行すればよい。)
$ mv run-calc ~/bin
$ run-calc
$
echo
コマンドではなく、history
コ
マンドを使う。
$ open /Applications/Firefox.app
$ open /Applications/Thunderbird.app
$ history
(ヒストリの表示して、何行必要か数える。history コマンド自身の分、1行多く保存する。)
$ history |tail -4 > run-ft
$ cat run-ft
512 open /Applications/Firefox.app
513 open /Applications/MacPorts/Emacs.app
514 history
$ emacs run-ft
(不要な部分を削除する。1行目に #!/bin/bash と入れる。)
$ cat run-ft
#!/bin/bash
open /Applications/Firefox.app
open /Applications/Thunderbird.app
$ ls -l run-ft
-rw-r--r-- 1 yas prof 78 6 22 18:36 run-ft
$ cat run-ft
#!/bin/bash
open /Applications/Firefox.app
open /Applications/Thunderbird.app
$ ls -l run-ft
-rw-r--r-- 1 yas prof 78 6 22 18:36 run-ft
$ chmod +x run-ft
$ ls -l run-ft
-rwxr-xr-x 1 yas prof 78 6 22 18:36 run-ft
$ ./run-ft
$ ~/run-ft
$ mkdir ~/bin
(注意: mkdir は、1度だけ実行すればよい。)
$ mv run-ft ~/bin
$ run-ft
$
$ file .bashrc .emacs /bin/ls public_html/htdocs/index.html.en
.bashrc: ASCII text
.emacs: Lisp/Scheme program text
/bin/ls: Mach-O 64-bit executable x86_64
public_html/htdocs/index.html.en: HTML document text
$
$ cd /usr/bin
$ file * | grep shell
$
そのプログラムが、一般のプログラム(機械語)かシェル・スクリプトかは、 file コマンドを使うと調べることができる。
$ cd /usr/bin
$ ls -l apropos
-r-xr-xr-x 1 root wheel 1808 5 19 2009 apropos
$ file apropos
apropos: POSIX shell script text executable
$ head apropos
#!/bin/sh
#
# apropos -- search the whatis database for keywords.
# whatis -- idem, but match only commands (as whole words).
#
# Copyright (c) 1990, 1991, John W. Eaton.
# Copyright (c) 1994-1999, Andries E. Brouwer.
#
# You may distribute under the terms of the GNU General Public
# License as specified in the README file that comes with the man
$
1行に1つ、なにかの名前(人名、果物の名前)を含むファイルを作成しなさい。 sort コマンドを使って、並べ替えなさい。
$ emacs fruits.txt
$ sort fruits.txt
$ sort < fruits.txt
sort コマンドでよく使うオプション
$ date
2017年 6月15日 木曜日 14時17分31秒 JST
$ date-num
2017-06-15
$
ヒント: man date と man strftime 参照。
date コマンドの + で、strftime の説明にある format string を指定する。
The UNIX Super Text 上巻 38.1 節の表 38-1 に間違いがある。
$ date-cities
Asia/Tokyo date
2017年 6月15日 木曜日 14時20分25秒 JST
Europe/London date
2017年 6月15日 木曜日 06時20分25秒 JST
America/New_York date
2017年 6月15日 木曜日 01時20分25秒 JST
$
ヒント: date コマンドは、 環境変数の利用方法(TZ) で、表示を変化させる。
ヒント: 都市の情報は、/usr/share/zoneinfo/ にある。 夏時間(daylight saving time/summer time)により、 経度の標準からずれていることもある。
ヒント: 画面に都市名を表示するには、echo コマンドが使える。
$ lldir ~
drwxr-xr-x 59 yas prof 12288 6 16 15:37 /home/prof/yas
$ lldir /
drwxrwxr-t 27 root admin 1326 4 22 15:43 /
$
$ l2 file
ヒント: 引数には、拡張子 .tex 無しのファイル名を受け付けた方が、シェル
スクリプトが簡単になる。
$ lpshow file
(画面には、file.pdf が Preview.app で表示される)
ヒント: 引数には、拡張子 .tex 無しのファイル名を受け付けた方が、シェル
スクリプトが簡単になる。
ヒント: 拡張子 .pdf を持つファイルを open コマンドで開くと、標準では Preview.app が動作するが、他のプログラム(例えば Adobe Reader)にも変更で きる。この課題でも他のプログラムを使ってもよい。
$ ls-dot ~
.
..
.bashrc
.emacs
.login
$
ヒント
方法1:シェルの ファイル名置換でよく使われるパタン を利用し、「.*」というパタンで探す。
方法2: ls に -a オプションを付けると、全てのファイルを表示し、 そのうち、先頭が「.」のものを抜き出す。 先頭が「.」のものを抜き出すには、 grep コマンドで次のパタンを検索する。
$ grep '^\.'
abc
.abc
.abc
aaa
.aaa
.aaa
^D
$
余裕があれば、-l などのオプションが付けられるようにしなさい。
$ lsd ~
Desktop/
Documents/
Downloads/
Library/
Mail/
Maildir/
Movies/
Music/
Pictures/
WinFiles/
bin/
public_html/
$
方法1: ls -F の結果から grep で「/」がついているものだけを抜き出す。
余裕があれば、-l などのオプションが付けられるようにしなさい。 余裕があれば、ls と同じように、ファイル名の順に並べ替えなさい。
$ wc-lines *.c
85 228 1836 proc-uid-print.c
75 187 1156 pipe-rw-dup.c
50 152 1141 vaddr-print.c
46 140 1071 proc-create.c
67 161 1014 pipe-rw-nodup.c
38 93 819 signal-int.c
50 98 802 setjmp-longjmp.c
32 90 561 run-n.c
27 66 535 home-print.c
20 50 424 cont-1.c
20 50 424 cont-0.c
25 60 419 t-system.c
20 40 384 exec-date.c
14 49 370 arg-print.c
15 44 355 env-print.c
12 32 305 cont-2.c
13 16 174 fork-hello.c
4 10 60 main-return.c
$
この課題では、合計(Total)は表示されなくてもよい。
ヒント:wc の出力を sort コマンドでソートする。for で1つずつ wc コマン ドを実行して、全体の結果を sort するか、引数 $* で wc した後、sort する。
$ ps aux
USER PID %CPU %MEM VSZ RSS TT STAT STARTED TIME COMMAND
root 1 0.0 0.0 2456708 1356 ?? Ss 火08AM 1:26.40 /sbin/la
yas 3549 0.0 0.0 2435468 980 s000 Ss 3:40PM 0:00.01 -bash
yas 3548 0.0 0.0 2450900 748 ?? S 3:40PM 0:00.00 /usr/sbi
yas 3546 0.0 0.0 2456124 672 ?? Ss 3:40PM 0:00.01 /sbin/la
root 3544 0.0 0.1 2450900 4700 ?? S 3:40PM 0:00.27 /usr/sbi
root 3543 0.0 0.0 2446148 820 ?? Ss 3:40PM 0:00.00 /usr/lib
...
root 54 0.0 0.6 2468840 25220 ?? Ss 火08AM 0:43.01 /usr/lib
root 3556 0.0 0.0 2434788 428 s000 R+ 3:40PM 0:00.00 ps aux
$
そのうち、メモリのサイズ(RSS)が大きいプロセスを 10 個だけ表示するシェル・
スクリプトを作りなさい。
$ ps-rss-top10
USER PID %CPU %MEM VSZ RSS TT STAT STARTED TIME COMMAND
root 318 0.0 3.0 215076 127388 ?? Ss 火08AM 1:12.05 /Library/
root 104 0.0 0.9 2567852 37332 ?? Ss 火08AM 0:41.42 /System/L
_windowserver 1007 0.0 0.6 2775984 25560 ?? Ss 火05PM 0:31.43 /Sys
root 54 0.0 0.6 2468840 25220 ?? Ss 火08AM 0:43.01 /usr/libe
root 1599 0.0 0.6 11156056 24040 ?? Ss 水02PM 0:09.13 /System/L
_mysql 218 0.0 0.5 2510832 18940 ?? S 火08AM 0:57.74 /opt/loca
_securityagent 1019 0.3 0.4 2757796 15396 ?? S 火05PM 7:51.89 /Sy
root 305 0.0 0.3 152008 14292 ?? S 火08AM 0:31.75 /Library/
root 1010 0.0 0.2 2727284 7956 ?? Ss 火05PM 0:03.56 /System/L
root 1050 0.0 0.2 2777952 7676 ?? Ss 火05PM 0:00.66 /System/L
$
ヒント:1行目は、そのまま表示する。RSS の順(第6フィールド)に sort して
head する。
余裕があれば、VSZ の順、CPU 時間の順に表示するスクリプトを作りなさい。
類似のことを実行するプログラムとして top がある。
$ diff-backup kadai10.txt
余裕があれば、比較しているファイルの名前を表示したり、ファイルごとに停
止する、引数を取る、diff に対するオプションを取る、などの工夫をしなさ
い。
^C
(Control+C)
で強制終了する。
$ du ~/Desktop
$ du ~/Documents
$ du ~/Downloads
$ ls -d D*
Desktop Documents Downloads
$ du -s D*
155115 Desktop
43227 Documents
144962 Downloads
$ du -s -h D*
76M Desktop
21M Documents
71M Downloads
$ du -s -k D*
77558 Desktop
21614 Documents
72481 Downloads
$
~/Library/Caches/Firefox/Profiles/*/cache2/entries
$ cd ~/Library/Caches/Firefox/Profiles/*/cache2/entries
$ ls -l
$ file *
$ du -h
file コマンド
は、ファイルの種類を推定して表示するコマンドである。ファイルの拡張子が
なくても、画像や HTML 等を推定できる。
du コマンド
は、ディスクの使用量(disk usage)を表示するコマンドである。
手引き 2.7.3 参照、
手引き 10.8.1 参照。
実行例
$ cd ~/Library/Caches/Firefox/Profiles/*/cache2/entries
$ file *
006309EAFFD4653F45B69F09BF6F930B6C5B394C: HTML document text
010373204138EBE73E75A5935139FBE5159574DA: PNG image data, 471 x 170, 8-bit colormap, non-interlaced
01411C7B7D29B77239ADD6E1444AF735CF58EDAA: data
01B0B8F5A4CF26E30ABE4F537A04FDD2A769E1E3: data
02071B585FF428DA83F1E4771B34B0448CE22DB9: data
024B2D783D20A9528AFF7608FBBC242F09A56F7C: data
0298A6896A40F67AB5A8A2BC7CF8A2A686AF7797: gzip compressed data, from Unix
02A6CF51FEFDBB859AF2B3ABBCFF533F4B56FF75: gzip compressed data, from Unix, max speed
03868AAF0B3AF200ED91951DD663C59643712DF3: GIF image data, version 89a, 1 x 1
041D89FF5E95B5856742F2D33817E0F50850DED0: gzip compressed data, from Unix
....
$
その他の Web ブラウザについては、次のディレクトリ、その子供、親、兄弟の ディレクトリを観察してみなさい。
Firefox では、次のようして設定できる。
自分が使っている Web ブラウザで、次のことを調べなさい。
$ ls ~/Library/Caches
$ ls -l ~/Library/Caches
$ ls -lR ~/Library/Caches
$ du ~/Library/Caches
$ du -s ~/Library/Caches/*
$ du -sh ~/Library/Caches/*
不用ならば、削除しなさい。アプリケーションが動作していない時に削除する
ことが望ましい。
$ ls ~/Library/Caches
$ rm -r ~/Library/Caches
$ quota
$ quota -v
この結果と、 du コマンド の結果を比較しなさい。
$ du -s -k ~
(表示までに時間がかかる。強制終了したければ、^C)
.DS_Store
)」や「._.DS_Store
」というファイ
ルが作られることがある。これは、Finder がアイコンの位置等を保持するため
のファイルである。このファイルは、ls コマンドでは表示されないが、次のよ
うに -a オプションを使ったり、「*」を使うと表示される。
$ ls -a
.
..
(中略)
.DS_Store
(中略)
._.DS_Store
(中略)
$ ls .*DS*
.DS_Store ._.DS_Store
$ ls -l .*DS*
-rw-r--r--@ 1 yas prof 24580 6 17 11:44 .DS_Store
-rw-r--r-- 1 yas prof 4096 3 14 2014 ._.DS_Store
$ ls -l ~/Desktop/.*DS*
-rw-r--r--@ 1 yas prof 12292 4 25 17:07 /home/prof/yas/Desktop/.DS_Store
-rw-r--r-- 1 yas prof 4096 4 7 2014 /home/prof/yas/Desktop/._.DS_Store
$
これを観察してみなさい。rmdir でディレクトリを削除する時に、このファイ
ルが邪魔をして削除できないことがある。この時には、このファイルを削除し
なさい。
$ ls .*DS*
.DS_Store ._.DS_Store
$ rm .*DS*
$
._
」が始まるファイル名のファイルが作られることが
ある。これは、他の Unix にはない、MacOSX 独自の
ファイルの属性(
ファイルの属性、
ファイルの属性(2)、
)
(メタデータ)を表すものである。独自の属性としては、ファイルを作成した時
刻や作成したプログラムがある。
$ ls ._*
._.DS_Store ._audio-sampling.png
._Desktop ._fj-meetings-coins.eml
._Library ._shinjo-picture-2017-05-10.jpeg
._audio-sampling-2.png
$
これを観察してみなさい。rmdir でディレクトリを削除する時に、このファイ ルが邪魔をして削除できないことがある。この時には、このファイルを削除し なさい。
$ ls ._*
(表示して確認)
$ rm ls ._*
$
$ mkdir ~/dir1
$ ls ~/dir1
(最初は空)
$ cd ~/dir1
$ ls
(最初は空)
$ emacs file1.txt
$ ls
file1.txt
$ ls
(削除するファイルの確認)
$ rm file1.txt
(削除。成功すると何も表示されない。)
$ rm file1.txt~
(もし、「~」付きのファイルがあれば、削除。成功すると何も表示されない。)
$ ls
(ディレクトリが空であることの確認)
$ cd ..
(1個上のディレクトリに移動。)
$ ls ~/dir1
(ディレクトリが空であることの確認)
$ ls -d ~/dir1
(ディレクトリが存在すること確認)
$ rmdir ~/dir1
(ディレクトリの削除。成功すると何も表示されない。)
$ ls -d ~/dir1
(エラーが表示されることにより、ディレクトリが削除されたことを確認)
$
ls コマンドは、「.」から始まるファイル名を表示しない。ls コマンドに -a オプションを付けると、表示する。
ディレクトリを削除しようとした時に、ls コマンドで表示されないが、「.」 で始まるファイルがあるために、削除できないことがある。このことを確認し なさい。 たとえば、 .DS_Storeや 「._」から始まるファイル名 のファイルが存在することがある。
$ mkdir ~/from_dir
$ mkdir ~/to_dir
$ cd ~/from_dir
$ echo 1 > file1.txt
$ echo 2 > file2.txt
$ echo 3 > file3.txt
$ ls *.txt
$ ls *.txt
(移動するファイルの確認)
$ ls ~/to_dir
(移動する先のディレクトリの確認)
$ mv *.txt ~/to_dir
(移動。成功すると何も表示されない。)
$ ls ~/to_dir
(移動した先のディレクトリの確認)
$ mkdir ~/dir1
$ cd ~/from_dir
$ echo 1 > file1.txt
$ ls
(ファイル名の確認)
$ mv file1.txt file2.txt
(改名。成功すると何も表示されない。)
$ ls
(ファイル名の確認)
$ open ~/from_dir
$ open ~/to_dir
option キーを押しながらドラッグすると、移動ではなく、コピーになる。 このことを確認しなさい。
なお、コピーを行った後、元のファイルを消せば実質的に移動(改名)になる。
名称未設定フォルダ
」という名前のフォルダが作成される。
$ open ~/.Trash/
$ cd ~/.Trash/
$ ls
$ ls -l
$ du .
$ du -s .
$ lv ~/.bashrc
$ lv /usr/local/lib/standard/bashrc
環境変数 PATHの値を観察しなさい。
$ echo $PATH
環境変数 PATH に含まれているディレクトリには、どのようなコマンドがある
かを調べなさい。例えば、/usr/local/bin を調べるには、次のようにする。
$ ls /usr/local/bin
PATH 以外にどのような
シェル変数と環境変数
が設定されているかを調べなさい。
$ set
(全てのシェル変数と環境変数の表示)
$ set | lv
(全てのシェル変数と環境変数の表示。lv によるページ単位の表示。)
$ printenv
(環境変数の表示)
$ printenv | lv
(環境変数の表示。lv によるページ単位の表示。)
$
~/.bashrc に alias の定義を追加しても、既に実行しているシェルには反映さ れない。実行しているシェルに反映させるには、「.」コマンドを用いる。 ~/.bashrcの編集結果の反映参照。
ls -l
の代わりに
ll
というコマンドを使えるようにしなさい。
$ ls -l
total 80
-rw-r--r-- 1 yas prof 36224 6 16 16:24 index.html
-rw-r--r-- 1 yas prof 4459 6 16 16:24 screen-tab.png
$ ll
-bash: ll: command not found
# この時点ではコマンドは見つからない
$ alias ll='ls -l'
# alias による ll コマンドの定義
$ ll
total 80
-rw-r--r-- 1 yas prof 36224 6 16 16:24 index.html
-rw-r--r-- 1 yas prof 4459 6 16 16:24 screen-tab.png
$
$ alias
$ alias ll
名前='定義内容'
」
を引数として与える。例:
$ alias ll='ls -l'
$ unalias ll
~/.bashrc に alias の定義を追加しても、既に実行しているシェルには反映さ れない。実行しているシェルに反映させるには、「.」コマンドを用いる。 ~/.bashrcの編集結果の反映参照。
$ cat ~/.bashrc
#
# coins standard ~/.bashrc
#
if [ -f /usr/local/lib/standard/bashrc ]; then
. /usr/local/lib/standard/bashrc
fi
# add your own code below
alias ll='ls -l'
# ~/.bashrc に ll を追加した
$ ll
bash: ll: command not found
# 追加しただけでは、コマンドは見つからない
$ . ~/.bashrc
# 「.」コマンドで、~/.bashrc を読み込む
$ ll
(ls -l の表示)
# ll コマンドが有効になっている。
$
echo
でファイルに落すのではなく、alias
コマンドでエ
イリアスを定義する。
~/.bashrc
に追加する。
$ cd ~
$ ls -R . | lv
$ ls -lR . | lv
$ ls -laR . | lv
-R
オプションは、このようにしばしば-l
オプションや
-a
オプションと一緒に使われる。また、-R
オプションは、大
量の結果を表示することが多いので、パイプに出力してページャで表示するこ
とが多い。
(これらのオプションを忘れた人は、man コマンドを見なさい。)
$ ls -l /Applications | grep '^l'
ここで、'^l'
は、
行の先頭がl
で始
まるという意味である。
The Unix Super Text 第32章 正規表現 参照。
$ lynx .
$ lynx ~
$ w3m .
$ w3m ~
(1) 練習問題(1808) から
練習問題(1817) まで
から2つ選んでシェル・スクリプトを作成しなさい。
作成したシェル・スクリプトを、ディレクトリ ~/bin
に起きなさい。
作成したシェル・スクリプトについて、次のことを書きなさい。
練習問題 1行からなるシェル・スクリプトの作成、 練習問題 historyからのシェル・スクリプトの作成 参照。
(2) quotaコマンドを実行しなさい。大きなファイ ルを cp コマンド等でコピーしなさい。その後、再び、quota コマンドを実行 しなさい。コピーの前後で数字が変化した部分と変化しなかった部分がある。 変化した部分は、何を意味するのか説明しなさい。変化しなかった部分で、重 要な数字を1つ選び、何を意味するのか説明しなさい。
(3) coins の個人のホームディレクトリで利用可能な領域を増やすために、不 用なファイルを削除すると良い。不用なファイルがしばしば存在する場所を2つ 上げなさい。 そこにある不用なファイルを削除する方法を簡単に説明しなさい。(実際には削 除しなくても良い。レポートには、その方法だけを説明すれば良い。)
(4) The Unix Super Text の次の部分を読みなさい。
(5) 次の文書を読みなさい。
(6) 次の文書を読みなさい。
ガイドラインの中で、意味が分からない用語があれば、用語集を調べなさい。 ガイドラインの中から疑問に思ったこと、または、改善すべきと思ったことを 2項目以上示しなさい。この時、次のような情報を含めなさい。(7) [加点] mkdir, rmdir, cp, mv, rm 等のコマンドや Finder を用いて、ファ イルを整理しなさい。レポートには次の項目を含めなさい。
ディレクトリ構成の説明には、 treeコマンド の結果を利用してもよい。ただし、今回整理した部分だけを含めるこ と。~/Library/ など、自分では整理しなかったファイルについては、含めない こと。
課題が出される前に既にディレクトリを作成して既に既に整った状況であった 場合には、問題点の代わりに整理の考え方について説明しなさい。またディレ クトリを新たに作成する代わりに既存のディレクトリについて報告しなさい。
(8) [加点] The Unix Super Text の次の部分を読みなさい。