筑波大学 システム情報系 情報工学域 新城 靖 <yas@cs.tsukuba.ac.jp>
このページは、次の URL にあります。
https://www.coins.tsukuba.ac.jp/~syspro/2025/2025-05-21
あるいは、次のページから手繰っていくこともできます。
https://www.coins.tsukuba.ac.jp/~syspro/2025/
http://www.coins.tsukuba.ac.jp/~yas/
「端末」を複数開くと、「シェル」のプロセスが2つ実行される。 プログラムとしては、同じシェルだが、プロセスとしては別物になる。
その昔は、1度に1個のプログラムだけしかメモリに入れられなかった。 (プログラムとプロセスの違いが希薄。)
プログラムとプロセス
$ /bin/ls
シェルが外部コマンドを実行する時は、プロセスが作られる。組み込みコマン ドを実行する時は、プロセスは作られず、シェル自身が実行する(シェルの内 部のプログラムが動く)。
内部コマンドには、次のようなものがある。
いくつかのコマンドは、組み込みコマンドと外部コマンドの両方がある(紛ら わしい)
プロセスには、ID (identifier)と属性がある。 psコマンド は、プロセスの一覧と属性を表示するコマンドである。
$ ps
PID TTY TIME CMD
4026784 pts/0 00:00:00 bash
4028828 pts/0 00:00:00 emacs
4028900 pts/0 00:00:00 ps
$
1行が1プロセスに対応。左から次のような意味を持つ。
/dev/pts/0
は
pts/0
になる。
TIME
CMD
(または COMMAND
)
$ tty
/dev/pts/0
$
$ ps x
PID TTY STAT TIME COMMAND
4026641 ? Ss 0:00 /lib/systemd/systemd --user
4026642 ? S 0:00 (sd-pam)
4026648 ? Ssl 0:00 /usr/bin/pipewire
4026649 ? Ssl 0:00 /usr/bin/pipewire-media-session
4026650 ? Ssl 0:00 /usr/bin/pulseaudio --daemonize=no --log-target=jou
4026677 ? Ss 0:00 /usr/bin/dbus-daemon --session --address=systemd: -
4026698 ? Ssl 0:00 /usr/libexec/gvfsd
4026719 ? Sl 0:00 /usr/libexec/gvfsd-fuse /run/user/1013/gvfs -f
4026752 ? Ssl 0:00 /usr/libexec/xdg-document-portal
4026753 ? R 0:00 sshd: yas@pts/0
4026756 ? Ssl 0:00 /usr/libexec/xdg-permission-store
4026779 ? SNsl 0:00 /usr/libexec/tracker-miner-fs-3
4026784 pts/0 Ss 0:00 -bash
4026823 ? Ssl 0:00 /usr/libexec/gvfs-udisks2-volume-monitor
4026828 ? Ssl 0:00 /usr/libexec/gvfs-afc-volume-monitor
4026833 ? Ssl 0:00 /usr/libexec/gvfs-gphoto2-volume-monitor
4026837 ? Ssl 0:00 /usr/libexec/gvfs-mtp-volume-monitor
4026841 ? Ssl 0:00 /usr/libexec/gvfs-goa-volume-monitor
4026845 ? Sl 0:00 /usr/libexec/goa-daemon
4026852 ? Sl 0:00 /usr/libexec/goa-identity-service
4028414 ? S 0:00 sshd: yas@pts/1
4028415 pts/1 Ss+ 0:00 -bash
4028504 pts/1 Tl 0:00 emacs -nw
4028828 pts/0 Tl 0:00 emacs -nw
4029698 pts/0 R+ 0:00 ps x
$
ps に x, l, u, a 等のオプションを付けると、
ps はプロセスの
状態
(STATe)
を表示する。よく表示される状態(大文字部分)には、次のようなものがある。
R
(runnable)
U
(uninterruptible), D
(Disk),
S
(Sleep)、I
(Idle)
$ ps a
端末と結びついていないプロセスも含めるには、x オプションもつける。
$ ps ax
練習問題 ps lと親プロセス、 pstreeコマンド参照。 。
プロセスを殺すとは、プロセスが自主的に終了しない時に、強制的に終了させ ること。
Unix では、プロセスを殺す方法は、主に次の2つ。
注意: 端末プログラムでウィンドウを閉じたくらいでは、 問題のあるプロセスは死んでいないこともある。
$ sleep 5
$
以下の例では、500秒 sleep したかったが、途中でやめたくなった。
$ sleep 500
^C
$
^C を押すと、端末の働きで、プロセスに対して
「シグナル」が飛ぶようになっている。
cat コマンドは、引数のファイルを画面に表示する。
$ cat /etc/shells
/bin/sh
/bin/bash
/usr/bin/sh
/usr/bin/bash
/usr/bin/tmux
/bin/tmux
/usr/bin/zsh
/bin/zsh
/bin/ksh
/bin/rksh
/usr/bin/ksh
/usr/bin/rksh
/bin/csh
/bin/tcsh
/usr/bin/csh
/usr/bin/tcsh
/usr/bin/fish
/bin/fish
$
cat コマンドは、引数を与えないと、キーボードから(行単位で)データを読み、
それを画面に表示する。
途中で cat コマンドのプロセスを^Cで強制終了している。
$ cat
Line 1
Line 1
Line 2
Line 2
^C
$
^C
と同様に ^\
(「^\」または「^¥」)
もプロセスを強制終了させるキーで
ある。^\ は、^Cが利かないときでも利くことがある。
^\
の場合、デバッグ用にcore
という名前のファイル
ができる。普段は、core ファイルは不要なので、^C
を使うべ
きである。
coins の環境では、core ファイルが作られないように設定されている。 core ファイルの利用方法は、デバッガ gdb のマニュアル、 man core (core ファイルの man ページ(man コマンドで表示されるページ))、
stty コマンドを使うと、^C
や ^\
を変更でき
る。
$ stty -a
speed 38400 baud; rows 40; columns 80; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = ;
eol2 = ; swtch = ; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R;
werase = ^W; lnext = ^V; discard = ^O; min = 1; time = 0;
-parenb -parodd -cmspar cs8 -hupcl -cstopb cread -clocal -crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff
-iuclc ixany imaxbel iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt
echoctl echoke -flusho -extproc
$
「intr」のところに表示されるものが「^C」,
「quit」のところに表示されるものが「^\」に相当するキー。
「kill」は、1行削除の意味。
^C
や ^\
で死なないプロセス
を殺すには、kill コマンドを使う。
これには、普通、別の端末を使う。 ssh で遠隔ログインしている時は、端末プログラムで、もう1枚ウィンドウを開き、 同じホストに遠隔ログインする。 ps コマンドで PID を調べ、kill コマンドに与える。
$ ps
(pidを調べる)
$ kill pid
$
kill コマンドでは、引数で与えれたプロセス識別子pidのプロセスを殺す。
他人のプロセスは、権限がないので殺せない。
注意:pid
には、プロセス識別子となる番号を入れる。「p」、
「i」「d」と3文字打つのではない。たとえば、PID が 1023 のプロセスを殺す
時には、次のように打つ。
$ kill 1023
-KILL
をつけてkillコマンドを実行する。
$ kill -KILL pid
-KILL
以外にも、様々な種類がある。詳しくは、man kill を参照。
次の例は、bash と macs のプロセスを探している。
$ ps
PID TTY TIME CMD
19795 pts/0 00:00:00 bash
27126 pts/0 00:00:00 ps
$ ps x
PID TTY STAT TIME COMMAND
19648 ? Ss 0:00 /lib/systemd/systemd --user
19649 ? S 0:00 (sd-pam)
<中略。大量の表示>
26321 pts/1 Ss 0:00 -bash
26374 pts/1 Sl+ 0:00 emacs -nw
27164 pts/0 R+ 0:00 ps x
$ ps x | grep bash
19795 pts/0 Ss 0:00 -bash
26321 pts/1 Ss 0:00 -bash
27393 pts/0 S+ 0:00 grep bash
$ ps x | grep emacs
26374 pts/1 Sl+ 0:00 emacs -nw
$
練習問題 ps コマンドと grep コマンド 参照。
ps, grep の変わりに pgrep コマンドを使う
方法もある。
ps, grep, kill の代わりに
pkill コマンドを使う方法もある。
ps プロセスの一覧の表示 ps x プロセスの一覧の表示 ps ax プロセスの一覧の表示 stty -a 端末の状態(キーの割り当て)の表示 ^C (キーボード) プロセスを殺す kill PID プロセスを殺す kill -KILL PID プロセスを殺す(必殺)PID はプロセス識別子。
PID
と打つのではない。
図? プログラムの標準入出力の普通の接続先
$ cat
Line 1
Line 1
Line 2
Line 2
Line 3
Line 3
^D
$
cat コマンドは、入力しているファイルが終わりになる(EOF, end of file)と、
自動的に終了する。キーボードからの入力は、無限に継続可能だが、
^D
(Control + D) と打つと、普通のファイルと同様に、ファイルの終
わりという意味になる。
cat コマンドは、「-n」オプションを付けると行番号を付加する。
$ cat -n
Line 1
1 Line 1
Line 2
2 Line 2
Line 3
3 Line 3
^D
$
$ ls -l
合計 50
drwx------ 20 yas 510 31 4月 6 16:29 Maildir
drwxr-xr-x 13 yas prof 13 3月 31 10:20 WinFiles
drwxr-xr-x 2 yas prof 10 4月 10 14:42 bin
drwxr-xr-x 88 yas 510 97 4月 17 16:38 coins
drwxr-xr-x 15 yas 510 17 4月 12 16:50 cs
drwxr-xr-x 4 yas 510 103 4月 10 21:47 dot
drwxr-xr-x 3 yas prof 3 4月 7 10:02 etc
drwxr-xr-x 4 yas 510 4 3月 7 2014 lib
lrwxrwxrwx 1 yas 510 14 7月 18 2024 os2 -> coins/os2-2016
drwxr-xr-x 19 yas 510 76 3月 24 10:07 public_html
drwx------ 3 yas prof 3 3月 24 17:24 snap
lrwxrwxrwx 1 yas 510 12 4月 11 2014 syspro -> coins/syspro
drwxr-xr-t 2 yas prof 2 3月 29 23:08 thinclient_drives
drwxr-xr-x 3 yas prof 15 4月 13 21:33 tmp
<省略>
$
シェルは、「記号>」 があると、標準出力を画面から指定されたファ
イルに切り替えて、指定されたプログラムを実行する。
プログラムの結果は、ファイルに保存され、画面には何も表示されない。
$ ls -l > file1
$
図? プログラムの標準出力のファイルへの切替え
-n
(nは数)というオプションが与
えられると、先頭の n 行だけ表示する。
$ head -5 file1
合計 50
drwx------ 20 yas 510 31 4月 6 16:29 Maildir
drwxr-xr-x 13 yas prof 13 3月 31 10:20 WinFiles
drwxr-xr-x 2 yas prof 10 4月 10 14:42 bin
drwxr-xr-x 88 yas 510 97 4月 17 16:38 coins
$
$ head -5
Line 1
Line 1
Line 2
Line 2
Line 3
Line 3
Line 4
Line 4
Line 5
Line 5
$
シェルは、「記号<」 があると、標準入力をキーボードから指定され たファイルに切り替え、指定されたプログラムを実行する。
$ head -5 < file1
合計 50
drwx------ 20 yas 510 31 4月 6 16:29 Maildir
drwxr-xr-x 13 yas prof 13 3月 31 10:20 WinFiles
drwxr-xr-x 2 yas prof 10 4月 10 14:42 bin
drwxr-xr-x 88 yas 510 97 4月 17 16:38 coins
$
図? プログラムの標準入力のファイルへの切替え
以下の例では、画面の表示は同じだが、動作が異なる。 以下の例では、head コマンドが自分でファイルを開いている。
$ head -5 file1
合計 54
drwx------ 20 yas 510 31 4月 6 16:29 Maildir
drwxr-xr-x 13 yas prof 13 3月 31 10:20 WinFiles
drwxr-xr-x 2 yas prof 10 4月 10 14:42 bin
drwxr-xr-x 88 yas 510 97 4月 17 16:38 coins
$
|
」があると、次の動作を行う (パイプ(pipe)機能)。
|
」の左のプログラム(プロセス)の標準出力を、右のプログ
ラム(プロセス)の標準入力に(ファイルを介することなく)接続する。
$ ls -l | head -5
合計 54
drwx------ 20 yas 510 31 4月 6 16:29 Maildir
drwxr-xr-x 13 yas prof 13 3月 31 10:20 WinFiles
drwxr-xr-x 2 yas prof 10 4月 10 14:42 bin
drwxr-xr-x 88 yas 510 97 4月 17 16:38 coins
$
図? プログラムの標準出力のパイプへの切替え
$ ls -l > tempfile
$ head -5 < tempfile
合計 55
drwx------ 20 yas 510 31 4月 6 16:29 Maildir
drwxr-xr-x 13 yas prof 13 3月 31 10:20 WinFiles
drwxr-xr-x 2 yas prof 10 4月 10 14:42 bin
drwxr-xr-x 88 yas 510 97 4月 17 16:38 coins
$ rm tempfile
$
パイプは、3つ以上のプログラム(プロセス)を結びつけることもできる。
$ ls -l | cat -n | head -5
1 合計 54
2 drwx------ 20 yas 510 31 4月 6 16:29 Maildir
3 drwxr-xr-x 13 yas prof 13 3月 31 10:20 WinFiles
4 drwxr-xr-x 2 yas prof 10 4月 10 14:42 bin
5 drwxr-xr-x 88 yas 510 97 4月 17 16:38 coins
$
$ ls -l Maildi > file1
ls: 'Maildi' にアクセスできません: そのようなファイルやディレクトリはありません
$ cat file1
$
「Maildi
」 は、「public_html
」の打ち間違い(rがない)である。
ls コマンドは、エラー・メッセージを標準出力ではなく標準エラー出力に書き
出す。
「2>file
」をつかうと、標準エラー出力をファイルに保存
することもできる。
$ ls -l Maildi > file1 2> file2
$ cat file1
$ cat file2
ls: 'Maildi' にアクセスできません: そのようなファイルやディレクトリはありません
$
「2>&1
」をつかうと、標準エラー出力(2)を、標準出力
(1)に混ぜて(&
)、ファイルに保存することもできる。
$ ls -l Maildi > file1 2>&1
$ cat file1
ls: 'Maildi' にアクセスできません: そのようなファイルやディレクトリはありません
$
「2>&1
」は、順番が大事。ファイルに混ぜて保存する場合
は、「>file
」の後にやること。先にやると、その時点で
の標準出力(画面)が指定されてしまう。
$ ls -l Maildi 2>&1 > file1
ls: 'Maildi' にアクセスできません: そのようなファイルやディレクトリはありません
$ cat file1
$
2>&1
を使うと、エラー・メッセージも含めてパイプに出力
することもできる。この場合は、パイプ|
より左に書く。
(理由は、「プログラム言語処理」の授業を受けると分かる。)
$ ls -ld Maildi 2>&1 | cat -n
1 ls: 'Maildi' にアクセスできません: そのようなファイルやディレクトリはありません
$
$ echo hello
hello
$ echo hi
hi
$ echo hello > hello.txt
$ cat hello.txt
hello
$ echo hi > hello.txt
$ cat hello.txt
hi
$ echo hello > hello.txt
$ cat hello.txt
hello
$ echo hi >> hello.txt
$ cat hello.txt
hello
hi
$
図? アクセス制御における主体、オブジェクト、および、操作
アクセス制御のオブジェクトは、ファイル、または、プロセスである。 操作には、次のようなものがある。
図? 実世界のユーザとUNIX中のプロセス・ファイル
Unixでは、全てのファイルやプロセスは、あるユーザの所有物である。 ファイルとプロセスには、UID が付加されている。
1人のユーザが複数のグループに属することができる。
whoami
コマンドを用いる。
$ whoami
yas
$
この例では、この人(のプロセス)のユーザ名は、yas
。
プロセスが属しているグループのグループ名を調べるには、
groups
コマンドを用いる。
$ groups
prof c-admin c-spec tebiki c-gakusei c-comp coins-2019 coins-2025
$
この例では、この人(のプロセス)は、
prof
, c-admin
, c-spec
,
tebiki
, c-gakusei
, c-comp
,
coins-2019
, coins-2025
という8つのグループに属している。
idコマンドも使える。
$ id
uid=1013(yas) gid=5510(prof) groups=5510(prof),5020(c-admin),5065(c-spec),5150(t
ebiki),5180(c-gakusei),6000(c-comp),6019(coins-2019),6023(coins-2025)
$
$ ls -l .bashrc
-rw-r--r-- 1 yas prof 180 3月 8 2019 .bashrc
$ ls -ld ~
drwxr-xr-x 35 yas prof 91 4月 17 17:11 /home/prof/yas
$
この例では、UID 属性が yas、GID 属性が prof である。
ファイルの「内容」のアクセス3段階であるが、ファイルの「属性」次の2段 階である。
(ホームディレクトリ以下の)ディレクトリの x を使ったファイルへの読み込み アクセスの許可と禁止。
図? ディレクトリのxの働き
スーパー・ユーザ(super user)
は、UID が 0 。
特権ユーザ(privileged user)、
ルート(root)、
su
(su コマンド、あるいは super user の略)ともよばれる。
スーパーユーザのユーザ名は、慣例として
root
という文字列が使われる。
ファイルのバックアップなど、システム管理の作業には スーパー・ユーザの仕組みが必要になる。
chown
コマ
ンドを用いる。ただし、スーパーユーザの権限が必要である。
ファイルをコピーして、元ファイルを消すことはできる。
ファイルの GID 属性を変更するには、chgrp
コマンドを使う。
一般ユーザは、自分が属しているグループにのみ変更できる。
任意のグループに変更するには、スーパー・ユーザの権限が必要である。
$ ls -l file1.txt
-rw-r--r-- 1 yas prof 1282 4月 17 16:50 file1.txt
$ chmod go-r file1.txt
$ ls -l file1.txt
-rw------- 1 yas prof 1282 4月 17 16:50 file1.txt
$
file1.txt に対して、グループ(g)とそれ以外の人(o)に対して「読込み可」の許可を
取り除いている。
chmodは一般的には次の形式で実行する。
$ chmod set file1 file2 ...
set の形式
[ugoa][=+-][rwx]
ここの[ugoa]は、誰に対するアクセスの設定を変更するかを対象となる人を以
下の表から選んで並べる(複数可)。
文字 | 単語 | 意味 | |
u | user | ユーザ(利用者,所有者) | |
g | group | グループ | |
o | others | その他 | |
a | all | 全員、ugo と同じ | |
(空) | umaskを除く全員 |
文字 | 意味 |
+ | 許可を追加する |
- | 許可を除く |
= | 指定した許可だけとする |
文字 | 単語 | 意味 |
r | read | 読込み可 |
w | write | 書込み可 |
x | execute | 実行可 |
X | execute | 実行可(所有者に対して「実行可」がある時だけ) |
chmod には、-R (recursive)オプションがある。 これは、ディレクトリをたぐり全部変更することを意味する。
rw-r--r--
rwxr-xr-x
rwxr-xr-x
rwx------
rw-r--r--
~/public_html
以下のファイル)
rwxr-xr-x
public_html
以下のディレクトリ)
rwxr-xr-x
または rwx--x--x
~
。~/public_html
の親ディレクトリ)。
x ビットを出す。
rw-rw-r--
または rwxrwxr-x
r-xr-xr-x
, r-x------
$ echo hello
hello
$ echo hi
hi
$ echo /usr/bin/*z
/usr/bin/7z /usr/bin/unxz /usr/bin/xz
$ echo /usr/bin/7z /usr/bin/unxz /usr/bin/xz
/usr/bin/7z /usr/bin/unxz /usr/bin/xz
$
変数名=値
」を使う。
$ X=100
$
参照するには、「$変数名
」とする。
$ X=100
$ echo $X
100
$
他の変数と紛らわしい時には、
「"$変数名"
」のようにダブルクォートで括るとよい。
$ echo x"$X"x
x100x
$
(注意: $Xx と $X は別の変数なので区別できるようにする。)
あるいは、${変数名}
のように、
括弧で括る方法もある。
$ echo x${X}x
x100x
$
シェル変数を削除するには、unset コマンドを使う。
$ unset X
$ echo $X
$ echo x"$X"x
xx
$
設定されていないシェル変数を使おうとすると、空として扱われる。
注意:シェルのプロンプト($
) と間違えないこと。シェルのプロンプ
ト($
) は、実際のコンピュータでは、
yas@azalea16:~$
のようになっている。
azalea16:~ yas$ echo $PS1
\h:\W \u\$
azalea16:~ yas$ PS1='hi$ '
hi$ ls
Maildir dot lily-netstat syspro デスクトップ
WinFiles etc literacy-a4.txt syspro-url ドキュメント
bin expressions literacy-a5.txt thinclient_drives ビデオ
cal-2025-05.txt file1 os2 tmp ピクチャ
cal-2025-567.txt file2 passwd vmware ミュージック
coins hello.txt public_html ダウンロード 公開
cs lib snap テンプレート
hi$
くわしくは man bash を参照。
$ coins=coins.tsukuba.ac.jp
$ ssh www.$coins
$ ssh azalea10.$coins
$ X=100
...
$ export X
$
あるいは、最初から export を付けて代入の操作をすることもできる。
$ export X=100
$
bash では、環境変数の参照方法と削除方法はシェル変数と同じ($
とunset)である。
$ date
2025年 5月 9日 火曜日 18:48:46 JST
$ echo $TZ
$ ls -l file1
-rw-r--r-- 1 yas prof 0 5月 9 17:05 file1
$ TZ=EST
$ echo $TZ
EST
$ export TZ
$ date
2025年 5月 9日 火曜日 04:49:32 EST
$ ls -l file1
-rw-r--r-- 1 yas prof 0 5月 9 03:05 file1
$
date
コマンドは、TZ
が設定されていないと、そのコン
ピュータの標準(coins では、JST
、Japan Standard Time の意味)を利
用して日時を表示する。
$ man date
DATE(1) User Commands DATE(1)
NAME
date - print or set the system date and time
SYNOPSIS
date [OPTION]... [+FORMAT]
date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
DESCRIPTION
Display the current time in the given FORMAT, or set the system date.
Mandatory arguments to long options are mandatory for short options
too.
...
-u, --utc, --universal
print or set Coordinated Universal Time (UTC)
...
EXAMPLES
...
Show the time on the west coast of the US (use tzselect(1) to find TZ)
$ TZ='America/Los_Angeles' date
この 環境変数名=値 コマンド名
は、環境変数を設定してコマンドを
実行する方法の1つ。この方法では、元のシェルでは環境変数を変更しない。
環境変数 TZ の話は、man environ に出ている。この man ページには、他に も HOME, LANG, PATH 等、知っておくべき環境変数が多数記載されている。
$ man environ
ENVIRON(7) Linux Programmer's Manual ENVIRON(7)
NAME
environ - user environment
...
HOME A user's login directory, set by login(1) from the password file
passwd(5).
LANG The name of a locale to use for locale categories when not over‐
ridden by LC_ALL or more specific environment variables such as
LC_COLLATE, LC_CTYPE, LC_MESSAGES, LC_MONETARY, LC_NUMERIC, and
LC_TIME (see locale(7) for further details of the LC_* environ‐
ment variables).
PATH The sequence of directory prefixes that sh(1) and many other
programs apply in searching for a file known by an incomplete
pathname. The prefixes are separated by ':'. (Similarly one
has CDPATH used by some shells to find the target of a change
directory command, MANPATH used by man(1) to find manual pages,
and so on)
...
* TZ and TZDIR give timezone information used by tzset(3) and through
that by functions like ctime(3), localtime(3), mktime(3), strf‐
time(3). See also tzselect(8).
...
< > | $ { } ( ) [ ] & ; ^ " * ? ~ ' ` 空白 タブ
など。
コマンドの引数として渡したい時には、解釈を避ける(エスケープ(escape))た めの特殊な形式を使う。
形式 | エスケープ対象 | エスケープしないメタキャラクタ |
\x | 「\ 」の直後の一文字 | なし (行末の「\」は継続行の意味) |
'str' | 「'」でくくられた文字列 | 「'」自身は入れられない。「\'」も効かない。 |
"str" | 「"」でくくられた文字列 | 「$」, 「`」(バッククォート),「"」(ダブルクォート), 「\」,「!」 |
\
は、ASCII 5c(16進)の文字。表示は、「\」か「¥」。
ダブルクォート ""
の内部では、$
は解釈され、シェル変数や
環境変数の置き換えられる。シングルクォート
「''
」では、$
は解釈さない。
$ echo $HOME
/home/prof/yas
$ echo "$HOME"
/home/prof/yas
$ echo '$HOME'
$HOME
$ echo \$HOME
$HOME
$
$ echo $PATH
/home/prof/yas/bin:/usr/local3/coins/linux/bin:/usr/local3/coins/common/bin:/opt
/intel/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/gam
es:/usr/local/games:/snap/bin:/home/prof/yas/bin
$ printenv PATH
/home/prof/yas/bin:/usr/local3/coins/linux/bin:/usr/local3/coins/common/bin:/opt
/intel/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/gam
es:/usr/local/games:/snap/bin:/home/prof/yas/bin
$
シェルは、打ち込まれたコマンドを環境変数 PATH の中から探して実行する。
たとえば、ls
と打ち込まれると、/usr/bin
にある
/usr/bin/ls
を実行する。
複数のコマンドがみつかれば、PATHの先に見つかったものを実行する。 どのコマンドが実行されるかを確認するには、which コマンドを使う。
$ ls -l /usr/bin/ls /bin/ls
-rwxr-xr-x 1 root root 138208 2月 8 2024 /bin/ls
-rwxr-xr-x 1 root root 138208 2月 8 2024 /usr/bin/ls
$ which ls
/usr/bin/ls
$
$ history
1 saykotoeri2
2 saykotoeri2
3 fg
4 dup
<中略>
500 history
$
シェル変数 HISTFILESIZE の個数だけ記憶している。
$ echo $HISTFILESIZE
500
$
$ ls file.txt
file.txt
ここで -l を付けわすれたのに気がつく
$ ls -l !*
ls -l file.txt
-rw-r--r-- 1 yas prof 9 4 29 21:23 file.txt
$
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 19568 5月 11 16:53 kana.aiff
-rw-r--r-- 1 yas prof 17406 6月 15 2018 exam-result.xlsx
-rwxr--r-- 1 yas prof 9418 4月 18 11:07 lily-netstat
-rw-r--r-- 1 yas prof 579 4月 19 17:57 cal-2025-567.txt
-rw-r--r-- 1 yas prof 193 4月 19 17:53 cal-2025-05.txt
drwxr-xr-x 4 yas prof 103 5月 3 11:58 dot
drwxr-xr-x 88 yas prof 97 4月 17 17:11 coins
drwxr-xr-x 19 yas prof 76 3月 24 10:07 public_html
-rw-r--r-- 1 yas prof 46 4月 19 18:44 syspro-url
-rw------- 1 yas prof 36 4月 19 18:11 literacy-a5.txt
$
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 19568 5月 11 16:53 kana.aiff
-rw-r--r-- 1 yas prof 17406 6月 15 2018 exam-result.xlsx
-rwxr--r-- 1 yas prof 9418 4月 18 11:07 lily-netstat
-rw-r--r-- 1 yas prof 579 4月 19 17:57 cal-2025-567.txt
-rw-r--r-- 1 yas prof 193 4月 19 17:53 cal-2025-05.txt
drwxr-xr-x 4 yas prof 103 5月 3 11:58 dot
drwxr-xr-x 88 yas prof 97 4月 17 17:11 coins
drwxr-xr-x 19 yas prof 76 3月 24 10:07 public_html
-rw-r--r-- 1 yas prof 46 4月 19 18:44 syspro-url
-rw------- 1 yas prof 36 4月 19 18:11 literacy-a5.txt
$
シェル・スクリプトを実行する度にいちいち「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 19568 5月 11 16:53 kana.aiff
-rw-r--r-- 1 yas prof 17406 6月 15 2018 exam-result.xlsx
-rwxr--r-- 1 yas prof 9418 4月 18 11:07 lily-netstat
-rw-r--r-- 1 yas prof 579 4月 19 17:57 cal-2025-567.txt
-rw-r--r-- 1 yas prof 193 4月 19 17:53 cal-2025-05.txt
drwxr-xr-x 4 yas prof 103 5月 3 11:58 dot
drwxr-xr-x 88 yas prof 97 4月 17 17:11 coins
drwxr-xr-x 19 yas prof 76 3月 24 10:07 public_html
-rw-r--r-- 1 yas prof 46 4月 19 18:44 syspro-url
-rw------- 1 yas prof 36 4月 19 18:11 literacy-a5.txt
$ ~/ls-size10
-rw-r--r-- 1 yas prof 19568 5月 11 16:53 kana.aiff
-rw-r--r-- 1 yas prof 17406 6月 15 2018 exam-result.xlsx
-rwxr--r-- 1 yas prof 9418 4月 18 11:07 lily-netstat
-rw-r--r-- 1 yas prof 579 4月 19 17:57 cal-2025-567.txt
-rw-r--r-- 1 yas prof 193 4月 19 17:53 cal-2025-05.txt
drwxr-xr-x 4 yas prof 103 5月 3 11:58 dot
drwxr-xr-x 88 yas prof 97 4月 17 17:11 coins
drwxr-xr-x 19 yas prof 76 3月 24 10:07 public_html
-rw-r--r-- 1 yas prof 46 4月 19 18:44 syspro-url
-rw------- 1 yas prof 36 4月 19 18:11 literacy-a5.txt
$
~/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 19568 5月 11 16:53 kana.aiff
-rw-r--r-- 1 yas prof 17406 6月 15 2018 exam-result.xlsx
-rwxr--r-- 1 yas prof 9418 4月 18 11:07 lily-netstat
-rw-r--r-- 1 yas prof 579 4月 19 17:57 cal-2025-567.txt
-rw-r--r-- 1 yas prof 193 4月 19 17:53 cal-2025-05.txt
drwxr-xr-x 4 yas prof 103 5月 3 11:58 dot
drwxr-xr-x 88 yas prof 97 4月 17 17:11 coins
drwxr-xr-x 19 yas prof 76 3月 24 10:07 public_html
-rw-r--r-- 1 yas prof 46 4月 19 18:44 syspro-url
-rw------- 1 yas prof 36 4月 19 18:11 literacy-a5.txt
$
ホームディレクトリにある「ls-size10
」は、そのままファイル名を打
ち込んでも実行できない。mv コマンドで「~/bin/」に移動した後は、
「ls-size10
」で実行可能になる。
(Coins では、標準で ~/bin が PATH に含まれている。)
シェル・スクリプトを実行する時に、シェル・スクリプトに対して引数を与え ることができる。
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 /usr/bin
-rwxr-xr-x 1 root root 24879104 5月 1 2024 x86_64-linux-gnu-lto-dump-11
-rwxr-xr-x 1 root root 16130168 12月 1 17:52 snap
-rwxr-xr-x 1 root root 10280680 5月 4 2024 gdb
-rwxr-xr-x 1 root root 8379976 7月 6 2024 gtk4-encode-symbolic-svg
-rwxr-xr-x 1 root root 7004032 2月 9 2024 luahbtex
$ lss 3 /usr/sbin
-rwxr-xr-x 1 root root 5412752 4月 27 16:07 ipp-usb
-rwxr-xr-x 1 root root 3397048 6月 4 2024 wpa_supplicant
-rwxr-xr-x 1 root root 3255560 6月 9 2024 NetworkManager
$ lss 10 .
-rw-r--r-- 1 yas prof 19568 5月 11 16:53 kana.aiff
-rw-r--r-- 1 yas prof 17406 6月 15 2018 exam-result.xlsx
-rwxr--r-- 1 yas prof 9418 4月 18 11:07 lily-netstat
-rw-r--r-- 1 yas prof 579 4月 19 17:57 cal-2025-567.txt
-rw-r--r-- 1 yas prof 193 4月 19 17:53 cal-2025-05.txt
drwxr-xr-x 4 yas prof 103 5月 3 11:58 dot
drwxr-xr-x 88 yas prof 97 4月 17 17:11 coins
drwxr-xr-x 19 yas prof 76 3月 24 10:07 public_html
-rw-r--r-- 1 yas prof 46 4月 19 18:44 syspro-url
-rw------- 1 yas prof 36 4月 19 18:11 literacy-a5.txt
$
$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
$
~/.bashrc
という名前のファイルにあるプログラムを自動的
に実行する。~/.bashrc
では、次のようなことを行う。
~/.bashrc を編集したら、一度、端末プログラムがウィンドウを問題なく開けるか、確 認すると良い。
~/.bashrc に問題があり、ssh でログインできない(ログインしてもすぐにロ グアウトしてしまう)時や、端末プログラムがウィンドウを開かない(一瞬開い てすぐに閉じる)時には、次のような方法で修正する。
$ 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=icho01.u.tsukuba.ac.jp
...
$
これを使うと、ssh が簡単になる。
$ ssh $www
$ ssh $icho
~/.bashrc
などを設定して、~/bin
を
環境変数PATH
に含まれ
るようにすることを奨める。自分で作成したプログラムやシェル・スクリプトを
~/bin
に置くと、ファイル名を指定しなくても
(「~/ファイル名
」や
「./ファイル
」打たなくて)、
~/bin
以下の短いファイル名で実行できるようになる。
coins では、標準で ~/bin
が PATH
に含まるように設定されて
いるので、各自設定する必要はない。
例:
何もしないと、自動的に再帰的な処理を行うプログラムもある。例: tar, tree, find 等。
ディレクトリに対しても mv コマンドは有効である。ディレクトリの名前を mv で変更すると、木構造で考えると、それ以下のファイルの名前を全て変更し たことと同じ効果がある。
ファイルのコピーでは、 シンボリック・リンク等の問題があるため、 cp -r では不十分なことが多い。
$ quota -v
Disk quotas for user tcoins1 (uid 19001):
Filesystem 1K blocks quota limit grace files quota limit grace
/home 1763828 10240000 11264000 12583 0 0
/usr/local/local3 0 0 0 0 0 0
$
上のユーザは、/home の領域で 1,763,828 KB 使っている。 上限は、10,240,000 KB。 ファイル数は、12583 個。ファイル数の上限は、設定されていない。
coins の標準は、上限 10 GB (10*1024 KB)。これを超えると、ファイルを保存で きなくなる。 coins.tsukuba.ac.jpの電子メールも受信できなくなる。 10 GB (10*1024 KB) を超えると、警告のメールが飛ぶ仕組みがあるが、メール が飛ばないこともあるので、各自自分で気をつける。 手引き 5.8 ディスククォータに関する警告メールが来てしまった 参照
Coins では 2025年3月のシステムの置き換えで quota が使えなくなった。 残りどのくらい使えるかは、Windows に(リモートデスクトップで)ログインして調べる方法がある。
Windows にログインしなくても、同じことが smbclient コマンドの du サブ コマンド (disk usage) を使うことでもわかる。$ smbclient -U coins-ad\\$USER //130.158.231.169/home -c du
Password for [COINS-AD\ユーザ名]:(パスワードを打つ。画面には表示されない。)
10485760 blocks of size 1024. 9395656 blocks available
Total number of bytes: 0
$
この例では、
10,485,760 KB (10 GB) のうち
9,395,656 KB (8.96 GB) 使えることがわかる。
130.158.231.169 は、ファイルサーバ pansy5.coins.tsukuba.ac.jp の IP アドレス。このコマンドを簡単に実行するための coins-quota という シェル・スクリプトも用意されている。
$ which coins-quota
/usr/local3/coins/linux/bin/coins-quota
$ cat `which coins-quota`
#!/bin/sh -x
smbclient -U coins-ad\\$USER //130.158.231.169/home -c du
$
quota limit を超えた時には、不要なファイルを消す。 まずは、キャッシュを削除する。
^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、
手引き付録 1.9.2 duコマンド 参照、
手引き 5.8.1 必要のないファイルを調査,削除する 参照。
http://www.coins.tsukuba.ac.jp/~ユーザ名/
http://www.coins.tsukuba.ac.jp/~ユーザ名/index.html
https://www.coins.tsukuba.ac.jp/~ユーザ名/
https://www.coins.tsukuba.ac.jp/~ユーザ名/index.html
ユーザ名
」を、ログインの時に打つ自分のユーザ名
で置き換える。たとえば、学籍番号が 202194321 の人は、次のような URL で
参照される。
http://www.coins.tsukuba.ac.jp/~s2054321/
http://www.coins.tsukuba.ac.jp/~s2054321/index.html
https://www.coins.tsukuba.ac.jp/~s2054321/
https://www.coins.tsukuba.ac.jp/~s2054321/index.html
~/public_html/
$ mkdir ~/public_html
2025年注意。2025年3月より http: 用と https: 用のディレクトリが統合され た。以前は、別のディレクトリであった。
mkdir の後、 ディレクトリのxの働き を理解し、 ホームディレクトと作成したディレクトリに対して chmod コマンド で others に対して x ビットを立てる必要がある。 練習問題 Webページ用のディレクトリ作成 参照。
このディレクトリに index.html というファイルを作成すれば、 それが情報科学類での個人のホーム・ページの内容となる。
/var/log/httpd/access*
/var/log/httpd/ssl_access*
$ cd /var/log/httpd/
$ ls access* | tail -1
$ ls -t access* | head -1
以下の例では、現在増加しているログは
access_log_20240609
である。$ cd /var/log/httpd/
$ ls access* | tail -1
access_log_20240609
$ ls -t access* | head -1
access_log_20240609
$
以下は、アクセス・ログの内容の例である。見やすいように改行を入れているが
実際には1行が1つのページの転送を意味する。
2001:2f8:3a:1711::230:30 - - [14/Jun/2024:10:04:32 +0900] "GET /~syspro/2024/202 1-06-16/ HTTP/1.1" 200 77088 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:89.0) Gecko/20100101 Firefox/89.0" 2001:2f8:3a:1711::230:30 - - [14/Jun/2024:10:04:32 +0900] "GET /~syspro/2024/202 1-06-16/images/www-cgi.png HTTP/1.1" 200 15499 "http://www.coins.tsukuba.ac.jp/~ syspro/2024/2024-06-16/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:89.0) Gecko/20100101 Firefox/89.0" 2001:2f8:3a:1711::230:30 - - [14/Jun/2024:10:05:32 +0900] "GET /~syspro/2024/202 1-06-16/cgi-printarg.cgi?lastname=name1&firstname=name2&lang=C&email=who%40dom H TTP/1.1" 200 443 "http://www.coins.tsukuba.ac.jp/~syspro/2024/2024-06-16/" "Mozi lla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:89.0) Gecko/20100101 Firefox/89.0" 2001:2f8:3a:1711::230:30 - - [14/Jun/2024:10:06:23 +0900] "POST /~syspro/2024/20 21-06-16/cgi-printarg.cgi HTTP/1.1" 200 375 "http://www.coins.tsukuba.ac.jp/~sys pro/2024/2024-06-16/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:89.0) Ge cko/20100101 Firefox/89.0"このログは、設定ファイル /etc/httpd/conf/httpd.conf にある CustomLog 等の設定による。
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined CustomLog "|/usr/sbin/rotatelogs -l /etc/httpd/logs_http/access_log_%Y%m%d 20M" combinedcombined という名前が付けられたログのフォーマットの意味は、以下の通りである。
アクセスログの他に、エラーが起きた時にもログが作られる。これを エラーログ という。エラーログは、情報科学類のサーバ www (www.coins.tsukuba.ac.jp) では、次の場所にある。
/var/log/httpd/error_log_*
/var/log/httpd/ssl_error_log_*
その他に、次のようなログを取ることもある。
ssl_request_log
(coins では
/var/log/httpd/ssl_request_log_*)
/var/log/httpd/suexec.log
等 (coins では /var/log/secure)
練習問題 学類 Web サーバのアクセス・ログの観察 参照。
$ ps
$ ps l
ps コマンドに、次のオプションを与えて、動作を確認しなさい。
$ ps l
$ ps -l
「-」の有無で、使い方が違うことがある。
$ ps u
$ ps -u ログイン名
他のオプションについて、man ps を見て見なさい。
/etc/services
にある http
が含まれる行
/etc/services
にある pop
が含まれる行
/etc/services
にある imap
が含まれる行
ls
が含まれる行
grep コマンドには次のようなオプションを指定することができる。
端末と結びついていないプロセスを調べるには、ps コマンドに x オプション を与える。次のようにして、bash、emacs、ssh 関連、その他 /bin 関連プロセスの PID を調べなさい。
$ ps x | grep bash
$ ps x | grep emacs
$ ps x | grep ssh
$ ps x | grep /bin
grep コマンドに -i オプションを付けると大文字と小文字を関係なく調べるこ
とができる。このことを確認しなさい。
$ ps x | grep S
$ ps x | grep -i s
$ ps aux | lv
この方法を、確かめなさい。lv コマンドについては、
lv コマンド
を参照しなさい。q(終了)、次のページへの移動(スペースキー)、
ページを戻る(b)は、必ず覚えなさい。
手引き付録 1.8.2 lvコマンド 参照
$ ps u
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
yas 72189 0.0 0.0 21288 5580 pts/0 Ss 15:59 0:00 -bash
yas 93931 4.0 0.1 200456 36652 pts/0 Tl 16:59 0:00 emacs -nw
yas 93937 0.0 0.0 22584 1556 pts/0 R+ 16:59 0:00 ps u
$
%CPU
%MEM
VSZ
(Virtual Size)
RSS
(resident set size)
START
$ ps l
F UID PID PPID PRI NI VSZ RSS WCHAN STAT TTY TIME COMMAND
0 1013 72189 72175 20 0 21288 5580 do_wai Ss pts/0 0:00 -bash
0 1013 93931 72189 20 0 200456 36652 do_sig Tl pts/0 0:00 emacs -
0 1013 94534 72189 20 0 22372 1564 - R+ pts/0 0:00 ps l
$
PID 93931 の emacs の親プロセスは、PID 72189 の bash。
PID 94534 の ps の親プロセスは、PID 72189 の bash。
$ ps ux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
yas 72046 0.0 0.0 17788 10532 ? Ss 15:59 0:00 /lib/systemd/
yas 72047 0.0 0.0 171900 5712 ? S 15:59 0:00 (sd-pam)
yas 72053 0.0 0.0 49488 6768 ? Ssl 15:59 0:00 /usr/bin/pipe
yas 72054 0.0 0.0 33512 7036 ? Ssl 15:59 0:00 /usr/bin/pipe
<省略>
$ ps uxw
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
yas 72046 0.0 0.0 17788 10532 ? Ss 15:59 0:00 /lib/systemd/
systemd --user
yas 72047 0.0 0.0 171900 5712 ? S 15:59 0:00 (sd-pam)
yas 72053 0.0 0.0 49488 6768 ? Ssl 15:59 0:00 /usr/bin/pipe
wire
yas 72054 0.0 0.0 33512 7036 ? Ssl 15:59 0:00 /usr/bin/pipe
wire-media-session
yas 72055 0.0 0.0 307056 22776 ? Ssl 15:59 0:00 /usr/bin/puls
eaudio --daemonize=no --log-target=journal
<省略>
$
また、端末の画面を横幅を広げて ps コマンドを打ちなさい。
$ top
よく使われるオプションに -o, -d -u がある。
詳しくは man top を見なさい。
$ top -o %CPU
(CPU を消費している順)
$ top -o RES
(Resident Memory Size, メモリの消費している順)
$ top -d 5
(5秒置きに更新)
$ top -u $USER
(自分のプロセスだけ)
$ top
$ ps x | grep top
(結果省略。PID を調べる。)
$ kill PID
$
$ cat > file1.txt
Line 1
Line 2
^D
$ cat file1.txt
Line 1
Line 2
$
$ cat
aaa
aaa
bbb
bbb
^C
$
なお、^C と 入力の終わりを意味する ^D は働きが異な
る。どちらでもプロセスが終了することがあるが、強制終了したい場合には、
^C を使いなさい。
$ ls -R ~
(大量の表示、省略)
^C
$
$ yes | head
y
y
y
y
y
y
y
y
y
y
$
yes コマンドは普通、終了しないので、最後の 10 行を表示しようと tail コ
マンドを使っても表示されない。
$ yes | tail
この時、別の端末で yes コマンドのプロセスを kill してみなさい。
$ ps
(yes のプロセスの PID を調べる)
$ kill PID
$
yes のプロセスが終了すると、tail コマンドが最後の 10 行を表示する。
このことを確認しなさい。
yes と tail を ^C で強制終了するとどうなるか。調べなさい。
$ yes | tail
^C
$
yes コマンドと lv コマンドを実行するとどうなるか調べなさい。
$ yes | lv
yes コマンドを単独で実行し、^C で停止しなさい。
$ yes
...
^C
$
cat -n と合わせて実行し、必要なら強制終了しなさい。
$ yes | cat -n
$ yes | cat -n | lv
$ yes | cat -n | head
$ yes | cat -n | tail
C-c
) は、C-x C-c
のように、その
キーに割り当てられた関数を実行する意味。)
C-c
は、C-x
と同じように、2ストローク以上で1つの意
味をなすことが多い。何もない状態でC-c
を打った場合には、
C-g
によりキャンセルしなさい。
kill コマンドを使って、プロセスを殺してみなさい。
$ lv ~/.bashrc
$ ps
$ kill PID
lv コマンドの代わりに、bash からなるプロセスを殺すとどうなるか調べなさい。
ps, grep, kill の変わりに pgrep や pkill コマンドを使う方法もある。
$ ps 引数 | grep オプション パタン
$ kill 数
たとえば、次の例のように、
pulseaudio
のプロセスを探して、殺してみなさい。
$ ps ux | grep pul
yas 72055 0.0 0.0 307056 22776 ? Ssl 15:59 0:00 /usr/bin/pulseaudio --daemonize=no --log-target=journal
(端末がないので、ps に x オプションをつける。)
$ kill 72055
(これで死んでいる。)
$ kill 72055
-bash: kill: (72055) - そのようなプロセスはありません
(2度目は確認のため。)
$ ps ux | grep pul
yas 105073 0.0 0.0 18792 2432 pts/0 S+ 17:30 0:00 grep pul
(ps コマンドでも表示されない。)
$
ps l
コマンドを使うと、親プロセスの PID が表示される。
この仕組みを使って、親プロセスの親プロセスを調べなさい。
さらに、その親プロセスを調べなさい。
次の例は、bash の親プロセス、その親プロセス、さらにその親プロセスを調べ て、sshd であることを突き止めている。
$ ps alx | grep bash
0 0 1393 1 20 0 20016 3756 - S ? 0:05 /bin/bash /usr/bin/vboot-agent-service
0 1013 72189 72175 20 0 21424 5736 do_wai Ss pts/0 0:00 -bash
0 1013 105712 72189 20 0 18792 2428 pipe_r S+ pts/0 0:00 grep bash
$ ps alx | grep 72175
5 1013 72175 72043 20 0 19212 8728 - S ? 0:00 sshd: yas@pts/0
0 1013 72189 72175 20 0 21424 5736 do_wai Ss pts/0 0:00 -bash
0 1013 105802 72189 20 0 18792 2436 pipe_r S+ pts/0 0:00 grep 72175
$ ps alx | grep 72043
4 0 72043 1153 20 0 17992 10676 - Ss ? 0:00 sshd: yas [priv]
5 1013 72175 72043 20 0 19212 8728 - S ? 0:00 sshd: yas@pts/0
0 1013 105868 72189 20 0 18792 2404 pipe_r S+ pts/0 0:00 grep 72043
$ ps alx | grep 1153
4 0 1153 1 20 0 15720 8984 - Ss ? 0:00 sshd: /usr/sbin/sshd -D [listener] 1 of 10-100 startups
4 0 72043 1153 20 0 17992 10676 - Ss ? 0:00 sshd: yas [priv]
4 0 105775 1153 20 0 15720 8676 - Ss ? 0:00 sshd: [accepted]
$
どんなプロセスでも、PID が 1 番のプロセスにたどりつく。このことを確認しなさい。
$ pstree
$ pstree | lv
$ pstree -p | lv
端末の画面の横幅を広げると見やすい。
macOS で、Command+Option+Esc キーを押すと 「アプリケーションの強制終了」のウインドウを表示できる。
(まず、Commandキー() と Option キーを左手で押し、
押しっぱなしの状態で、右手で Esc キーを一度押して離す。)
左上のアップル・メニュー(リンゴのアイコン)を押し、
「強制終了」を選んでも良い。
「アプリケーションの強制終了」のウインドウ
この機能を確認しなさい。なお、この機能では、ウインドウを開くプロセスし か強制終了できない。iTerm の中で実行されるプロセスや端末とは結びついて いないプロセスを強制終了するには、kill コマンドを使うしかない。
$ open "/Applications/Utilities/Activity Monitor.app"
または、Finder で、「移動」メニューから「ユーティリティ」を
選択。「アクティビティモニタ.app」のアイコンをダブルクリック。
(クリックで拡大)
アクティビティモニタの表示
表示されたら、「CPU」タブを表示しなさい。そして、 表示されるプロセスの順序を変えてみなさい。
topコマンド の表示と比較しなさい。
「プロセス」のタブでは、CPU 時間の順番やメモリ使用量の順番に並べ替えな さい。
強制終了しても問題がなさそうな「アプリケーション」や「プロセス」を、強 制終了してみなさい。
$ cal 5 2025
5月 2025
日 月 火 水 木 金 土
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 5 2025 > cal-2025-05.txt
$ ls -l cal-2025-05.txt
-rw-r--r-- 1 yas prof 193 4月 19 17:53 cal-2025-05.txt
$ cat cal-2025-05.txt
5月 2025
日 月 火 水 木 金 土
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
$
動作例:
$ bc
bc 1.07.1
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006, 2008, 2012-2017 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
10+20
30
^D
$
bc コマンドは、プロンプトを表示しないが、ユーザが打ち込んだ数式を行単位
で計算して表示する。終了するには、入力の終わり(end of file) を意味する
^D
(Control+D) を押す。コントロール・キーを押しながら、
D キーを押す。
次のような機能が利用できる。
bc に -l
オプションを付けると小数が扱える。
詳しくは、man bc を見なさい。
$ cat > expressions
10+20
2^40
^D
$ cat expressions
10+20
2^40
$
次に作成した数式を、
キーボードの代わりにファイルからデータを読み込ませる。
$ bc < expressions
30
1099511627776
$
次に、echo コマンドとパイプを使ってみなさい。数式は、ダブルクォーテーショ
ンマーク(「"」)で括るとよい。
$ echo "10+20"
10+20
$ echo "10+20" | bc
30
$ echo "2^40" | bc
1099511627776
$
$ ls -l cal-2025-567.txt
ls: 'cal-2025-567.txt' にアクセスできません: そのようなファイルやディレクトリはありません
$ cal 5 2025 > cal-2025-567.txt
$ cal 6 2025 >> cal-2025-567.txt
$ cal 7 2025 >> cal-2025-567.txt
$ cat cal-2025-567.txt
5月 2025
日 月 火 水 木 金 土
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
6月 2025
日 月 火 水 木 金 土
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
7月 2025
日 月 火 水 木 金 土
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
$
$ lv /usr/share/dict/words
$ lv /etc/services
$ ls
file1.txt file2.txt
$ ls | cat
file1.txt
file2.txt
$
このことを確認しなさい。
$ ls
$ ls | cat
$ ls | lv
ls
の -1
オプションは、(端末でパイプでも)縦に表示するオプ
ションである。-1
は、数字の 1
であり、アルファベットの
L
の小文字の l
ではない。ls
の -C
オプショ
ンは、(端末でパイプでも)横に表示するオプション。
$ ls
file1.txt file2.txt
$ ls -1
file1.txt
file2.txt
$ ls -C
file1.txt file2.txt
$ ls -C | cat
file1.txt file2.txt
$
このことを確認しなさい。
$ ls -l /usr/bin
$ ls -l /usr/bin | lv
$ ls -l /usr/bin
$ ls -l /usr/bin | head
$ ls -l /usr/bin | head -20
$ ls -l /usr/bin
$ ls -l /usr/bin | head
$ ls -l /usr/bin | head -20
$ ps aux | lv
$ ps aux | head
treeコマンド
の長い結果をページャで表示してゆっくり見る。
$ tree
$ tree | lv
$ grep yes /usr/share/dict/words | lv
$ tail /usr/share/dict/words | sort -r
まず、pbpaste コマンドを使って、Web ページの一部や端末の表示の一部を ファイルに保存しなさい。
$ pbpaste > file1.txt
$ pbcopy < ファイル名
cat コマンドとパイプを使う方法もある。
$ cat ファイル名 | pbcopy
cat コマンドではなく、他のコマンドでも良い。
$ ps | pbcopy
$ ls -l | pbcopy
$ ls
(タブ入りの表示、横)
$ ls | expand
(タブを含まない表示、縦)
$ ls -C | expand
(タブを含まない表示、横。コピーして、テキストエディタにペーストしても乱れない。)
$
この動きを確認しなさい。
。
。
M-x shell-command
この機能を、C-u (Control+u) を付けて実行すると、コマンドを実行
してその結果を現在編集中のファイル(バッファ)に挿入できる。
C-u M-x shell-command
Shell command: 実行したいコマンド
この機能を確認しなさい。
dir > files.txt
type files.txt
type files.txt | more
dir, type は、「コマンドプロンプト」のコマンド。
powershell では, ls と cat コマンドを使う。
$ id
$ whoami
$ groups
$ echo ~
$ ls -ld /
$ ls -ld /home
$ ls -ld /home/ugrad/
・・・中略・・・
$ ls -ld ~
Coins の Web サーバで個人の Web ページを開設した人は、
ディレクトリ ~/, ~/public_html や ~/public_html/index.html のモードを調べな
さい。
$ ls -ld ~/
$ ls -ld ~/public_html
$ ls -l ~/public_html/index.html
他の人(WWWページとして公開するために、WWWサーバ・プロセスからのアクセ
スも含む)がアクセスしようとしたらどうなるかを考察しなさい。
その他に、次のファイルやディレクトリのモードを調べなさい。
$ cd ~yas
$ ls -l
$ ls -l literacy-a*
$ lv literacy-a4.txt
$
$ cd ~yas
$ ls -l
$ ls -l literacy-a*
$ lv literacy-a5.txt
$
$ ls -l file1.txt
-rw-r--r-- 1 yas prof 2 5 10 18:59 file1.txt
$ chmod go-r file1.txt
$ ls -l file1.txt
-rw------- 1 yas prof 2 5 10 18:59 file1.txt
$
go-r
を含めて、様々なモードの設定方法がある。
次の設定方法の意味を確認しなさい。
WWW ページとして公開するファイルのモードはその他(others)に対するアクセ ス許可で r ビットが立っている必要がある。もし、このビットを chmod o-r で落した場合、Web ブラウザでアクセスできなくなる。このことを確かめなさ い。
以下の例では、sample1.html というファイルが存在し、 http://www.coins.tsukuba.ac.jp/~ユーザ名/sample1.html 等で アクセス可能な状態になっているものとする。
$ cd ~/public_html
$ cp sample1.html sample2.html
<アクセス制御のテスト用のファイルを cp コマンドで作成する。>
$ ls -l sample2.html
-rw-r--r-- 1 yas prof 408 5 18 11:11 sample2.html
<Firefoxでアクセス、再読み込み等>
$ chmod o-r sample2.html
$ ls -l sample2.html
-rw-r----- 1 yas prof 408 5 18 11:11 sample2.html
<Firefoxでアクセス、再読み込み等>
$ chmod o+r sample2.html
$ ls -l sample2.html
-rw-r--r-- 1 yas prof 408 5 18 11:11 sample2.html
<Firefoxでアクセス、再読み込み等>
$
同様に、ディレクトリを作成し、その他(others)に対する x ビットを立てたり 落したりしながら、その効果を確認しなさい。
$ cd ~/public_html
$ mkdir dir1
$ emacs dir1/file1.html
$ ls -ld dir1
$ ls -l dir1/file1.html
<Firefoxでアクセス、再読み込み等>
$ chmod o-x dir1
$ ls -ld dir1
<Firefoxでアクセス、再読み込み等>
chmod コマンドで ugoa を指定しない場合、 a と似ているが、umask の部分は落とされる。 このことを確認しなさい。
$ ls -l file1.txt
(ファイルのグループの表示)
$ id
(自分(プロセス)が属しているグループの表示)
$ chgrp グループ file1.txt
$ ls -l file1.txt
(ファイルのグループの表示)
$
まず、適当な内容を持つテキスト・ファイルを作成しなさい。
$ echo hello > file1
$ ls -l file1
-rw-r--r-- 1 yas prof 6 5 23 22:28 file1
$ cat file1
hello
$
そして、次のような要求を満たすように chmod コマンドを使ってモードを変更
しなさい。提出するファイルには、chmod コマンドをどのように利用したか、
また、その結果 ls -l の表示がどのようになったかを含めなさい。
$ ls -ld dropbox
drwx-wx-wx 2 yas prof 80 5 22 15:28 dropbox
$
送信側は、受信者が読めるモードで、ファイルを作成し、受信者の drop box
ディレクトリの下にコピーする。
$ emacs file-2025-05-21.text
$ ls -l file-2025-05-21.text
-rw-r--r-- 1 yas prof 56353 5 22 15:38 file-2025-05-21.text
$ cp file-2025-05-21.text ~user/dropbox
$
送信者は、このディレクトリから目的の取り出す。cp コマンド等でファイルを
コピーし、元のファイルを rm コマンド等で削除する。
このような drop box を用いて、隣の人、あるいは、その他の友人にファイルを渡しなさい。 同様に、ファイルを受け取りなさい。
$ ls -l file-2025-05-21.text
-rw-r--r-- 1 yas prof 56353 5 22 15:38 file-2025-05-21.text
$ cp file-2025-05-21.text ~
$ rm file-2025-05-21.text
注意: この練習問題では、
インターネット上のファイル・サービス
https://www.dropbox.com/
を用いない。
他の人へのアクセスを防ぐ方法として、ファイル名に乱数(予測されない文字 列)を含める方法がある。この方法を練習してみなさい。
なお、cp コマンド等でコピーした場合には、ps コマンドで引数が表示される ことがある。このことを確認しなさい。ファイル名を秘匿してdrop box に書き 込む時には、ps で表示されない方法を使う必要がある。その方法を考えなさい。
/tmp
など、誰でも書き込みできる場所にファイルを作成すると、その
ファイルの UID 属性はどうなるか調べなさい。新たに実験用のディレクトリを
mkdir コマンドで作成しなさい。そのディレクトリのGID属性を chgrp で変更
し、その中に新たなファイルやディレクトリを作成したら、その GID がどう設
定されるかを確かめなさい。また、既存のファイルをmv コマンドで移動したら
どうなるかを調べなさい。
$ set
$ set | lv
どのようなシェル変数が設定されているか観察しなさい。
$ printenv
$ printenv | lv
$ echo $HOME
/home/prof/yas
$ printenv HOME
/home/prof/yas
$
注意: echo
の引数には、$
を付けるが、
printenv
の引数には、$
を付けない。
次のような環境変数の値を調べなさい。
$ date
$ echo $TZ
$ export TZ=EST
$ date
$ unset TZ
$ date
$
環境変数 TZ
に /usr/share/zoneinfo/
の下にあるファイル名
を設定して、その動作を確認しなさい。
$ echo $HOME
cd コマンドの引数に与えて、ホーム・ディレクトリや
ホーム・ディレクトリ以下のディレクトリに移動してみなさい。
$ cd /
$ pwd
$ cd $HOME
$ pwd
$ cd /
$ pwd
$ cd $HOME/Maildir
$ pwd
$
ファイル名の置き換え機能「~/」を使う方法と環境変数 HOME を使う方法を比
較しなさい。
$ echo $PATH
(表示を確認する)
$ aaaa
-bash: aaaa: command not found (エラーの表示)
$ ls
(普通に実行される)
$ which ls
/usr/bin/ls
$ PATH=/
$ ls
-bash: ls: command not found (エラーの表示)
$ exit
この例ではPATH の内容がかなり限定され、普段は実行できるコマンドが実行で
きなくなっている。それで、最後に exit コマンドでシェルを終了している。
シェルが必要ならば、もう一度端末を実行するか ssh でログインしてシェルを実行する。
$ u=/usr/local3/coins/linux/bin
$ echo $u
/usr/local3/coins/linux/bin
$ ls $u/ruby
/usr/local3/coins/linux/bin/ruby
$ ls $u/jupyter
/usr/local3/coins/linux/bin/jupyter
$
$ echo *
(ファイル名の並び)
$ ls -ld *
(ls -ld の結果)
$
次のようにして、「*」をエスケープして、ファイル名置き換えの動作を無効化
できることを確認しなさい。
$ echo \*
*
$ ls -ld \*
ls: *: No such file or directory
(「*」というファイルが存在しないというエラー・メッセージ。エラーが出ることが正しい動作。)
$ echo '*'
*
$ ls -ld '*'
ls: *: No such file or directory
$ echo "*"
*
$ ls -ld "*"
ls: *: No such file or directory
$
$ echo '< > | { } ( ) [ ] & ; ^ " * ? ~'
< > | { } ( ) [ ] & ; ^ " * ? ~
$ echo "< > | { } ( ) [ ] & ; ^ ' * ? ~"
< > | { } ( ) [ ] & ; ^ ' * ? ~
$ echo \< \> \| \{ \} \( \) \[ \] \& \; \^ \" \* \? \~
< > | { } ( ) [ ] & ; ^ " * ? ~
$
" "
」 や シングルクォート
「' '
」の働きを調べなさい。
$ echo $HOME
(表示の確認)
$ echo "$HOME"
(表示の確認)
$ echo '$HOME'
(表示の確認)
$ history
$ history | tail -5
(過去の5行だけ表示)
$ history | grep cd
(cd を含む行だけ表示)
$ history | lv
(ページャ lv でページ単位で表示。)
!
の後に番号
!!
^P
/^N
で選択して $ echo $HISTFILESIZE
history コマンドで表示されるコマンドの数を調べなさい。
$ history
<表示>
$ history | wc
$
wc は、word count の略で、行数、単語数、バイト数を表示する。一番左が行
数である。
シェル変数 HISTFILESIZE の値を変化させると、何が起きるか調べなさい。
azalea15:~ yas$ echo $PS1
\h:\W \u\$
azalea15:~ yas$
この時、"$PS1" のようにダブルクォートでエスケープしないとうまく表示できないことがある。この理由を考えなさい。
シェル変数 PS1 は、シェルのプロンプトを保持している。これを変更してみな
さい。
azalea15:~ yas$ PS1='hi$ '
hi$
hi$
マニュアル man bash で、PS1 変数(PROMPTING)の中で次のような表現がどのような意味を 持っているかを調べなさい。
\h
\W
\u
$ set | wc
88 128 2643
$ set | lv
(表示省略。q で終了)
$ printenv | wc
41 47 1435
$ printenv | lv
(表示省略。q で終了)
$
" "
」 や シングルクォート 「'
'
」は、中に改行を含めることができる。逆に言うと、必ず閉じる
必要がある。閉じられていない場合には、シェルのプロンプトが$
から
>
に変化する。このことを確認しなさい。
$ echo "a
> b
> c"
(表示の確認)
$ echo 'a
> b
> c'
(表示の確認)
$
$ cat ~/.bashrc
#
# coins standard ~/.bashrc
#
if [ -f /usr/local/lib/standard/bashrc ]; then
. /usr/local/lib/standard/bashrc
fi
# add your own code below
$
まず、端末で何かコマンドを実行しなさい。以下の例では、直接 Linux にロ グインしている状態か、または、リモートデスクトップでログインしている状 態で、「システムプログラム」の授業のページを標準の Web ブラウザで開い ている。
以下の open コマンド (xdg-open コマンド) は、ファイル名、または、URL を引数により、それを開くようなプログラムを実行する。以下の例では、URL を引数により、Web ブラウザを実行して、そのページを開いている。
$ ls -l /usr/bin/open
lrwxrwxrwx 1 root root 22 2月 6 14:50 /usr/bin/open -> /etc/alternatives/open
$ ls -l /etc/alternatives/open
lrwxrwxrwx 1 root root 17 2月 6 14:50 /etc/alternatives/open -> /usr/bin/xdg-open
$ open https://www.coins.tsukuba.ac.jp/~syspro/2025/
(^p で1行もどす。^a で、行頭に移動して 「echo '」と打ち、^e して
「' > open-syspro」 と打つ)
$ echo 'open https://www.coins.tsukuba.ac.jp/~syspro/2025/' > open-syspro
$ cat open-syspro
open https://www.coins.tsukuba.ac.jp/~syspro/2025/
$
bash の機能で、^p (Control+P) で1行戻して、echo でファイルに落とす。
echo には、実行したコマンドを
「'シングルクォート'」で括る(
'|'
) などして、エスケープして与えると安全である。
作成したファイルは、そのまま実行できる。
$ bash open-syspro
いちいち bash と打たないでもいいようにするために、chmod
コマンド
で実行可能属性をつける。
$ ls -l open-syspro
-rw-r--r-- 1 yas prof 34 6 16 15:08 open-syspro
$ chmod +x open-syspro
$ ls -l open-syspro
-rwxr-xr-x 1 yas prof 34 6 16 15:08 open-syspro
$ ./open-syspro
$ ~/open-syspro
$
テキストエディタで1行目に「#!/bin/bash」か「#!/bin/sh」を入れる。
#!/bin/bash
open https://www.coins.tsukuba.ac.jp/~syspro/2025/
完成したコマンドを ~/bin
に移動する。
$ mkdir ~/bin
(注意: mkdir は、1度だけ実行すればよい。)
$ mv open-syspro ~/bin
$ open-syspro
$
$ file .bashrc .emacs /bin/ls public_html/index.html.en
.bashrc: ASCII text
.emacs: Lisp/Scheme program, ASCII text
/bin/ls: ELF 64-bit LSB pie executable, x86-64, version 1 (SYS
V), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=8
97f49cafa98c11d63e619e7e40352f855249c13, for GNU/Linux 3.2.0, stripped
public_html/index.html.en: HTML document, ASCII text, with escape sequences
$
$ cd /usr/bin
$ file * | grep shell
$
そのプログラムが、一般のプログラム(機械語)かシェル・スクリプトかは、 file コマンドを使うと調べることができる。
$ cd /usr/bin
$ ls -l zless
-rwxr-xr-x 1 root root 2206 9月 5 2024 zless
$ file zless
zless: POSIX shell script, ASCII text executable
$ head zless
#!/bin/sh
# Copyright (C) 1998, 2002, 2006-2007, 2010-2018 Free Software Foundation, Inc.
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
$
1行に1つ、なにかの名前(人名、果物の名前)を含むファイルを作成しなさい。 sort コマンドを使って、並べ替えなさい。
$ テキストエディタで fruits.txt を作成する
$ sort fruits.txt
$ sort < fruits.txt
sort コマンドでよく使うオプション
$ date
2025年 4月15日 金曜日 12時12分46秒 JST
$ date-num
2025-04-15
$
ヒント: man date と man strftime 参照。
date コマンドの + で、strftime の説明にある format string を指定する。
$ date-cities
Asia/Tokyo
2025年 4月15日 金曜日 12時56分51秒 JST
Europe/London
2025年 4月15日 金曜日 04時56分51秒 BST
America/New_York
2025年 4月14日 木曜日 23時56分51秒 EDT
$
ヒント: 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 がある。
$ ./countdown 3
3
2
1
0
$
$ diff-backup kadai10.txt
余裕があれば、比較しているファイルの名前を表示したり、ファイルごとに停
止する、引数を取る、diff に対するオプションを取る、などの工夫をしなさ
い。
$ 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
に追加する。
$ man lv
$ lv -h
$ cd ~
$ ls -R . | lv
$ ls -lR . | lv
$ ls -laR . | lv
-R
オプションは、このようにしばしば-l
オプションや
-a
オプションと一緒に使われる。また、-R
オプションは、大
量の結果を表示することが多いので、パイプに出力してページャで表示するこ
とが多い。
(これらのオプションを忘れた人は、man コマンドを見なさい。)
$ ls -l /Applications | grep '^l'
ここで、'^l'
は、
正規表現(regular expression)
で行の先頭がl
で始
まるという意味である。
。
$ lynx .
$ lynx ~
$ w3m .
$ w3m ~
2025/04/20 注意。 smbclient は、azalea か jelly では動作する。 violet01, violet03 (www) ではうまく動作しないかもしれない。
この結果と、 du コマンド の結果を比較しなさい。
$ du -s -k ~
(表示までに時間がかかる。強制終了したければ、^C)
^C
(Control+C)
で強制終了する。
$ cd
$ du *
$ du -s *
$ du -s -h *
$ du -s -k *
|
」を用いれば 1 回で終わることもある。
Firefoxブラウザを操作して、 元の HTML (ソース)を表示 しなさい。
$ ls -l literacy-a2.txt
(1つのファイルの属性の表示)
$ ls -l literacy-a*.txt
(複数のファイルの属性の表示)
$ ls -ld ~
(ホーム・ディレクトリの属性の表示)
$ cd
(ホーム・ディレクトリへ移動)
$ ls -ld
(カレントワーキングディレクトリ==ホーム・ディレクトの属性の表示)
ファイルの属性 を参考にして、ls -l の結果を、解釈しなさい。今日の実習では特に 次の属性について着目しなさい。
r 読込み可 w 書込み可 x 実行可(ディレクトリの場合は探索可)
情報科学類での個人のホーム・ページ を作成しなさい。 次のディレクトリを作成し、それ以下にファイルを作成する。
~/public_html/
$ mkdir ~/public_html
作成したディレクトリのその他(others)に対するアクセス許可で x ビットが立っ
ていることを確認しなさい。
$ ls -ld ~/public_html
drwxr-xr-x 5 yas prof 4096 4 10 13:46 /home/prof/yas/public_html
^ここ
さらに、ホーム・ディレクトリにもその他(others)に対するアクセス許可で x
ビットが立っていることを確認しなさい。
$ ls -ld ~
drwxr-xr-x 53 yas prof 7168 5 15 15:25 /home/prof/yas
$
^ここ
x ビットがたっていない場合には、 ディレクトリのxの働き を理解し、 chmod コマンド で修正しなさい。 手引き付録 1.8.7 chmod コマンド 参照。
注意:この練習問題は、練習問題 (3110)WWW ページ用のディレクトリ作成の後に行いなさい。
ディレクトリ ~/public_html/
の下に、単純なテキスト・ファイル
(HTMLではない)を Emacs 等で作成して置きなさい。ファイル名の拡張子は、
「.text
」または「.txt
」としなさい。
作成したファイルのモードを調べなさい。file1.text
という
名前のテキスト・ファイルを作成した場合、次のようにその他(others)に対す
るアクセス許可で r ビットが立っていることを確認しなさい。
$ cd ~/public_html
$ emacs file1.text
(何か簡単な内容を打ち込み、保存して終了する。)
$ ls -l file1.text
-rw-r--r-- 1 yas prof 2 May 22 22:09 file1.text
$
^ここ
作成したファイルを Web ブラウザで表示しなさい。たとえば、
file1.text
という名前のテキスト・ファイルを作成した場合、
次のような URL で参照する。
http://www.coins.tsukuba.ac.jp/~ユーザ名/file1.text
ユーザ名
」を、ログインの時に打つ自分のユーザ名で置き
換えなさい。たとえば、学籍番号が 202194321 の人は、次のような URL を開
きなさい。
http://www.coins.tsukuba.ac.jp/~s2054321/file1.text
HTTP で始まる URL には、public_html
は含まれないことに注
意しなさい。
注意:この練習問題は、練習問題 (3110)WWW ページ用のディレクトリ作成の後に行いなさい。
次の HTML ファイルを、ディレクトリ ~/public_html
の下にコピー
し、それを Web ブラウザ表示しなさい。
~yas/public_html/coins/literacy-2018/samples/sample1.html
$ cd ~/public_html
$ ls
(sample1.htmlが存在しないことを確認する。)
$ cp ~yas/public_html/coins/literacy-2018/samples/sample1.html .
$ ls -l sample1.html
-rw-r--r-- 1 yas prof 525 5 14 16:19 sample1.html
$
最後のカレント・ワーキング・ディレクトリの意味の「.」を忘れないこと。
ls -l の結果、その他(others)に対するアクセス許可で r ビットが立っている
ことを確認しなさい。
コピーした後、Web ブラウザで次の URL を開きなさい。
http://www.coins.tsukuba.ac.jp/~ユーザ名/sample1.html
ユーザ名
」を、ログインの時に打つ自分のユーザ名で置き
換えなさい。たとえば、学籍番号が 202194321 の人は、次のような URL を開
きなさい。
http://www.coins.tsukuba.ac.jp/~s2054321/sample1.html
HTTP で始まる URL には、public_html
は含まれないことに注
意しなさい。
個人のホーム・ページを作成しなさい)。 各自のホーム・ページは、次の HTML ファイルに対応する。
~/public_html/index.html
http://www.coins.tsukuba.ac.jp/~ユーザ名/index.html
http://www.coins.tsukuba.ac.jp/~ユーザ名/
URL でディレクトリを指定すると、自動的にそのディレクトリにある 「index.html」という名前のファイルが使われる。この機能を使うと、 各自のホーム・ページは、次のように短く表示できる。
http://www.coins.tsukuba.ac.jp/~ユーザ名/
$ cd /var/www/html/
$ ls
<省略>
$ cat index.html
<省略>
$
/var/log/httpd/
以下のファイルにためられる。
これを、次のコマンドを利用しながら観察しなさい。
$ hostname
violet03
$ cd /var/log/httpd/
$ pwd
/var/log/httpd
$
access*
の「*
」は、
シェルの
ファイル名置換
の意味である。
$ ls
(多くのファイルが表示される)
$ ls access*
(まだ多くのファイルが表示される)
$ ls ssl_access*
(まだ多くのファイルが表示される)
$ ls access* | tail -1
access_log_20250418
$ ls ssl_access* | tail -1
ssl_access_log_20250418
$
この例では、access_log_20250418
(http://のもの) と
ssl_access_log_20250418
(https://のもの) が最新のものである。
$ lv access上のlsの結果で置き換える
$ lv ssl_access上のlsの結果で置き換える
たとえば、上の例では、次のように打つ。
$ lv access_log_20250418
$ lv ssl_access_log_20250418
$ tail 上のlsの結果で置き換える
(表示がなされる。)
(しばらく待つ。)
$ tail 上のlsの結果で置き換える
(表示がなされる。)
(しばらく待つ。)
$ tail 上のlsの結果で置き換える
(表示がなされる。)
$
(しばらく間隔を空けてから)数回 tail コマンドを実行すると、その度に行
が増えているはずである。
たとえば、上の例では、次のように打つ。
$ tail access_log_20250418
$ tail access_log_20250418
$ tail access_log_20250418
$
$ tail ssl_access_log_20250418
$ tail ssl_access_log_20250418
$ tail ssl_access_log_20250418
$
https://www.coins.tsukuba.ac.jp/~syspro/2025/report/present.cgi,出席ボタン
」
を押すと、そこに表示されるので、それを利用する方法がある。
(出席ボタンは、何度押しても問題がない。授業時間外に押しても問題はない。
最終的に、授業時間内のものだけ集計される。
授業を履修していなくても、出席ボタンを押しても良い。
)
$ grep IPアドレス アクセスログのファイル名
たとえば、IP アドレスが 2001:2f8:3a:1711::230:30 であれば、次のように打つ。
$ grep 2001:2f8:3a:1711::230:30 アクセスログのファイル名
たとえば、IP アドレスが 130.158.230.30 であれば、次のように打つ。
$ grep 130.158.230.30 アクセスログのファイル名
ここで「アクセスログのファイル名」は、
練習問題 アクセスログの観察
で調べなさい。
たとえば、その練習問題の例では、次のように打つ。
$ grep 2001:2f8:3a:1711::230:30 access_log_20250418
$ grep 2001:2f8:3a:1711::230:30 ssl_access_log_20250418
http://www.coins.tsukuba.ac.jp/~syspro/2025/
をアクセスした後で、アクセスログ access*
を検索してみなさい。
あるいは、https: この講義の Web ページ
https://www.coins.tsukuba.ac.jp/~syspro/2025/
をアクセスした後で、アクセスログ ssl_access*
を検索しなさい。
$ tail -f アクセスログのファイル名
ここで「アクセスログのファイル名」は、
練習問題 学類 Web サーバのアクセス・ログの観察
で調べなさい。
たとえば、その練習問題の例では、次のように打つ。
$ tail -f access_log_20250418
$ tail -f ssl_access_log_20250418
-f オプションを指定すると、tail コマンドは終了することなく新しく追加さ
れた行を表示する。この tail コマンドは、自動的には終了しないので、終わっ
たら、
^C (Control+C) で強制終了させる。
例:パスワード・ファイル
/home1/prof/yas/etc/passwd-doc1
に登録されているユーザだけが、
.htaccess
があるディレクトリ以下にあるファイルをアクセスできる。
.htaccess:
AuthType Basic AuthName "Staff Only" AuthUserFile /home1/prof/yas/etc/passwd-doc1 require valid-userこのパスワード・ファイルは、サーバ上でhtpasswd というプログラムで作成する。
$ htpasswd -c /home1/prof/yas/etc/passwd-doc1 user1
Adding password for user1.
New password:user1のパスワードを打ち込む
Re-type new password:user1のパスワードを打ち込む
$ htpasswd /home1/prof/yas/etc/passwd-doc1 user2
Adding user user2
New password:user2のパスワードを打ち込む
Re-type new password:user2のパスワードを打ち込む
$
一番最初は、-c
オプション付で実行する。passwd
コマ
ンドと同様に、打ち込んだパスワードは、画面には表示されず、また、確
認のために2回打つ必要がある。
htpasswd コマンドの結果、次のようなファイルが作られる。
user1:1fjr1tHIgoG7U
user2:qXaeA9Zge7Yqc
ユーザ名と暗号化されたパスワード(正確にはパスワードのハッシュ値)から
構成される。
パスワードによるアクセス制御の例:
設定ファイル .htaccess
AuthType Basic AuthName "Staff Only" AuthUserFile /home1/prof/yas/etc/passwd-doc1 require valid-user
~/Maildir/
の下に保存する。その様子を ls コマンドや cat コマンドで観察しなさい。
$ ls ~/Maildir/
$ ls -a ~/Maildir/
$ ls ~/Maildir/cur
$ ls ~/Maildir/new
$ ls ~/Maildir/tmp
$ cat ~/Maildir/cur/ファイル名
このようなディレクトリの形式を Maildir 形式や Maildir++ 形式という。
注意: ~/Maildir/
以下には、IMAP サーバが利用する重要なファイルも
ある。削除したり壊さないように、慎重に操作すること。
coins 以外に日常的に読み書きするメール・アカウントがある人は、毎日 coins に届いたメールを確認する方法として~/.forward を設定して、転送する 方法が考えられる。そのようにしたい人は、そうしなさい。
~/.forward は、emacs でテキスト・ファイルとして作成するとよい。
$ emacs ~/.forward
$ cat ~/.forward
\s2054321, myaddr@xxx.yyy.zzz
(これは一例なので真似しないように)
$
設定が完了したら、必ず自分自身の coins のアドレス
(例えば学生番号が 202194321 なら
s2054321@coins.tsukuba.ac.jp
)にメールを書き、次のことを
確認しなさい。
https://www.u.tsukuba.ac.jp/s-migration/,@s メールの今後の運用について
。
「@s から @u への転送設定は行わないでください。」と書いてあるが、理由は不明。
https://www.u.tsukuba.ac.jp/email-u-forward/,メール転送設定 (Microsoft 365)
レポートには、指定されていなければ、端末の全ての表示を含めなさい。全て の表示とは、シェルのプロンプト、自分が打ち込んだコマンド、コマンドの実 行結果である。端末が持つコピー機能でコピーし、テキストエディタ(のウィン ドウ)にペーストするなどして、テキストをコピーして、レポートに含めること。 レポートとして、画像を提出してはならない。
主専攻実験で K-3 を選択した人は、「[高度]」と印が付いているものも必ず 回答しなさい。それ以外の人も、回答することを奨める。
(1) 以下の方法で、ps コマンドと kill コマンドを実行し、その結果をレポー トに含めなさい。
$ lv /etc/passwd
$ ps | grep lv
$ ps x | grep lv
(表示)
$
$ kill pid
$ ps | grep lv
$ ps x | grep lv
(表示)
$
(2) シェルが持つ標準入出力の切り替え機能を使って、次のコマンドの結果を 全て含む1つのファイルを作りなさい。レポートには作成したファイルを含め なさい。
次のコマンドを実行しなさい。
sort /etc/services | tailこのコマンドと同じ結果を、中間ファイルと標準入出力の切り替えで行いなさい。
sort ... tail ... rm 中間ファイル端末の表示をレポートに含めなさい。
(3) 自分のホーム・ディレクトリとその下にあるファイルについて、次のこと を行いなさい。
$ ls -ld ~
$ cd ~
$ ls -l file522.txt
-rw-r--r-- 1 sXXXXXXX ugrad 4 5 20 10:43 file522.txt
$
このファイルを、coins の「他の利用者」が cat コマンドで読み出そうとした
時に何がおきると考えられるか。簡単に答えなさい。(実際に他の利用者に試
してもらって良い。考えるだけで実際には試さなくても良い。
他の人の操作
$ cd ~sXXXXXXX
$ ls -l ~sXXXXXXX/file522.txt
$ cat ~sXXXXXXX/file522.txt
調べるには、ls -l コマンドとlv コマンド、grep コマンドを使う方法もある。
$ ls -la | lv
$ ls -la | grep rw-r--r--
(4) 次のファイル名を次のファイル名やディレクトリを、シェル変数を設定し て短く表現しなさい。
/usr/bin/awk /usr/bin/gcc /usr/bin/openssl短く表現したものを、ls -ld コマンドの引数に与えて実行しなさい。
$ シェル変数の代入1
$ シェル変数の代入2
$ シェル変数の代入3
...
$ シェル変数の代入n
$ ls -l シェル変数の参照を利用短い表現
lrwxrwxrwx. 1 root root 4 2月 18 2019 /usr/bin/awk -> gawk
$ ls -l シェル変数の参照を利用短い表現
-rwxr-xr-x 2 root root 768592 3月 25 2020 /usr/bin/gcc
$ ls -l シェル変数の参照を利用短い表現
-rwxr-xr-x 1 root root 555304 9月 1 2024 /usr/bin/openssl
$
ヒント1: シェル変数への代入は、1回以上、何回でもよい。
ヒント2: シェル変数の値に空白が含まれている(可能性がある)ものを参照する
時には、ダブルクォートでくくる("$name"
)とよい。
ヒント3: 補完機能と組み合わせてもよい。その場合にはどのようなキー操作を したのかをレポートに書きなさい。
(5) echo コマンドを使って、次の文字列を画面に表示しなさい。 なお、3行目では、AとBの間に空白が3個ある。
M* ????dir A B <yes><no> Let's go #helpたとえば、次のように回答しなさい。
$ echo 回答内容
Lib*
$ echo 回答内容
????top
$ echo 回答内容
A B
$ echo 回答内容
<yes><no>
$ echo 回答内容
Let's go
$ echo 回答内容
#help
$
注意:これは、
メタキャラクタをエスケープ
する課題である。単に echo コマンドに対してこのような文字列を与えればよ
いということでない。
練習問題 「*」のエスケープ、
練習問題 様々なシェルのメタキャラクタのエスケープ
参照。
(6) 練習問題(3074) から
練習問題(3084) まで
から2つ選んでシェル・スクリプトを作成しなさい。
作成したシェル・スクリプトを、ディレクトリ ~/bin
に起きなさい。
作成したシェル・スクリプトについて、次のことを書きなさい。
(7) quotaコマンドの説明、coins-quota コマンド、 コンピューティング環境のquotaの説明を読みなさい。次の値を確認しなさい。
(8) 正規表現の検索機能を用いて、次のファイルやコマンドの出力から
目的のもの検索して画面に表示しなさい。選択ではなく、全て実行しなさい。
なお、grepコマンドで OR の機能が必要ならば、-e オプションを複数与える方
法がある。egrep コマンドの「|
」を用いれば 1 回で終わることもある。
(9) [高度] 情報科学類 Coins の Web サーバを用いて、次のような URL で HTML の Web ページを公開しなさい(不用なら、レポートを作成した後で削除す ること。また、chmod コマンド等で、モードを元に戻すこと。)
この例で、sXXXXXXX は、各自自分の Coins でのログイン名とする。また、 randomstring には、ランダムな文字列を指定する。
http://www.coins.tsukuba.ac.jp/~sXXXXXXX/randomstring.html
情報科学類のコンピュータ www.coins.tsukuba.ac.jp では、 (violet03.coins.tsukuba.ac.jp) では、Apache httpd というWeb サーバが UID 48 (ユーザ名 apache) で動作している。 このプロセスを、ps コマンドと grep コマンドを使って表示しなさい。これら のコマンドを打つ前に、ssh で www.coins.tsukuba.ac.jp にログインしてから 行うこと(violet01 や azalea では不可)。端末の表示をレポートに含めなさい。
$ ps オプション | grep 引数
Web ブラウザで http://www.coins.tsukuba.ac.jp/~sXXXXXXX/randomstring.html
という URL を開いた時、Apache Web サーバのプロセスは、ホームディレクト
以下のどのファイルを開いて読もうと試みるか。そのファイル名を「~」を使っ
て答えなさい。
Apache Web サーバのプロセスが、そのファイルを読み事ができ、Web ブラウザ にその内容が表示されたとする。そのことから、ホームディレクトリを含む、 いくつかのディレクトリのモードのうち、特定のビットが1になっていることが わかる。
ホームディレクトリ以下の各ディレクトリについて、どのビットが1になって いるかを答えなさい。たとえば、次の例のように、ls -ld の結果を表示し、 その下の行に、1になっている部分に「^」のマークをつけなさい。
$ ls -ld ~
drwxr-xr-x 41 yas prof 108 4月 21 09:41 /home/prof/yas
^
$
作成した Web ページを、Web ブラウザで表示しなさい。その後、Web サーバ が生成しているアクセスログから、自分自身がアクセスした記録を抜き出しな さい。 それには、次のような方法がある。