金沢大学総合メディア基盤センター�大野浩之�ユニバーサルシェルプログラミング研究所�松浦智之
このスライドのPDF⇒ https://richlab.org/j/2d62
1
Information Media Center, Kanazawa Univ.�Hiroyuki Ohno, Ph.D.�Universal Shell Programming Lab.�Tomoyuki Matsuura
You can get this slide from https://richlab.org/j/2d62
2
第6回概要(2019-05-23)
3
Overview of #6(2019-05-23)
4
1.ソフトウェアにおける
「標準語」とは
5
on software
6
シェル・プログラミングへの不満
7
このあいだ、僕の書いたシェルスクリプトを、友達のパソコンで動かそうとしたら、動かなくてさあ。
シェルスクリプトは、環境依存っていうの?入っているOSによって動かないことが多いんだよね。
そうそう。Linuxで書いた
シェルスクリプトを
FreeBSDに持っていったら動かないし……。
Typical complaints against ShellSctipt
8
A while ago, I brought my ShellScript to my friend’s computer. But it didn’t work.
Is that “platform-dependency?”�Another OS doesn’t often allow to work ShellScripts my OS allows.
Yes.
The ShellScript I made for Linux didn’t work on FreeBSD.
シェル・プログラミングへの不満
9
いやいや、
そもそも呼び出すコマンドが
あったりなかったりするから。
それに同じコマンドあっても、
OSによって
通用しないオプションあるし…
bashならbash、というふうに
使うシェルを決めて、
全ての環境にインストールすれば?
Typical complaints against ShellSctipt
10
But really, not only shell but also various commands aren’t available on each platforms.
The available options are also different even if the same command exists as well.
Hey, why don’t you install the same shell, bash or zsh, on all the computers which you are going to use.
シェル・プログラミングへの不満
11
表示(フォーマット)が
違うこともあるよね。
psコマンドとか。
正規表現で使える文字
(メタ文字)もOSごとに
なんかバラバラだしさ〜。
Typical complaints against ShellSctipt
12
In “ps” command case, the display format are also different among each OSs.
You know, Regular Expression rule is slightly different, too.
シェル・プログラミングへの不満
13
PerlとかRuby、Pythonとかの方が絶対いいよ。
だよね。
言語が決まれば、使える関数とか文法、書式が
きっちり決まるからね。
大抵どこのOSにもあるから
標準語みたいなもの�でしょ!
Typical complaints against ShellSctipt
14
PHP, Ruby, Python are more useful definitly.
Yeah!
It’s promissed that the same language gives the same behaviours.
Those are almost like standard languages because they could be installed on almost all of OSs.
「標準語」の誤解
15
Misunderstanding of “standard language”
16
「標準語」の誤解
違いは何か?
17
標準語 | そうでない |
HTML | Flash |
FTP | Windowsファイル共有、AppleTalk |
C言語、 C++、 C# | Java、 アセンブリ |
Misunderstanding of “standard language”
What’s the different point?
18
Standard Language | not std. |
HTML | Flash |
FTP | Windowsファイル共有、 AppleTalk |
C言語、 C++、 C# | Java、 アセンブリ |
「標準語」の誤解
19
イラスト: 素材Good
ほら、困るでしょ!!
Misunderstanding of “standard language”
20
Illustrated by “sozai-good”
Be careful!
「標準語」の誤解
一製品に依存し、替えられない状態を「ロックイン」と呼ぶ。(⇒検索)�
21
標準語でないもの | その理由 |
Linux |
|
FreeBSDなどのOS |
|
Perlなどの言語 | 〃 |
bashなどのシェル | 〃 |
GNU版AWK,sed�など(GNU版コマンド群) | 〃 |
Misunderstanding of “standard language”
This feature is call “vendor lock-in” or “customer lock-in.”�
22
Non-std-lang. | Reason |
Linux |
|
OSs (FreeBSD, …) |
|
Perl, Ruby, …, etc | ” |
Shells (bash, …) | ” |
GNU commands | ” |
でもこれはセーフ
23
標準語として 認められるもの | 理由 |
Linux, FreeBSDなどの�POSIX準拠部分 |
|
bash, ksh, zshなどの�POSIX準拠部分 | 〃 |
GNU版AWK,sedなど�GNUコマンドの�POSIX準拠部分 | 〃 |
Misunderstanding of “standard language” (safe case)
24
The safe as a std-lang. | Reason |
The part of following POSIX on UNIX-liks OSs |
|
The part of following POSIX on Bourne’s extended shells (bash, ksh, zsh, ...) | ” |
The part of following POSIX on GNU commands | ” |
「標準語」の誤解
25
Misunderstanding of “standard language” (safe case)
26
2.標準語を話そう
27
2. Be a standard language speaker
28
2.1. 方言を知る
29
2.1. Know which is a dialect
30
方言を知る
31
Know which is a dialect
32
方言を知る
33
Know which is a dialect
34
2.2. シェルスクリプトの文法にある方言
35
2.2. Dialects of ShellScripts
36
bashというシェルについて
37
A kind of shell “bash”
38
bashにはいろいろ便利な独自機能がある
39
$ bash
$ echo {1..9}
1 2 3 4 5 6 7 8 9
$ echo {1..9}x{1..9}
1x1 1x2 1x3 1x4 1x5 1x6 1x7 1x8 1x9 2x1 2x2 ... 9x9
$ exit
$ dash
$ echo {1..9}
{1..9}
$ echo {1..9}x{1..9}
{1..9}x{1..9}
$
Bash has many original functions
40
$ bash
$ echo {1..9}
1 2 3 4 5 6 7 8 9
$ echo {1..9}x{1..9}
1x1 1x2 1x3 1x4 1x5 1x6 1x7 1x8 1x9 2x1 2x2 ... 9x9
$ exit
$ dash
$ echo {1..9}
{1..9}
$ echo {1..9}x{1..9}
{1..9}x{1..9}
$
bashにはいろいろ便利な独自機能がある
41
$ bash
$ diff /etc/passwd <(tail -n +2 /etc/passwd)
1d0
< root:x:0:0:root:/root:/bin/bash
$ exit
$ dash
$ diff /etc/passwd <(tail -n +2 /etc/passwd)
dash: Syntax error: “(“ unexpected
$
Bash has many original functions
42
$ bash
$ diff /etc/passwd <(tail -n +2 /etc/passwd)
1d0
< root:x:0:0:root:/root:/bin/bash
$ exit
$ dash
$ diff /etc/passwd <(tail -n +2 /etc/passwd)
dash: Syntax error: “(“ unexpected
$
bashにはいろいろ便利な独自機能がある
43
$ bash
$ exit 10 | exit 20 | exit
$ echo retval:$? retvals:${PIPESTATUS[@]}
retval:0 retvals:10 20 0
$ exit
$ dash
$ exit 10 | exit 20 | exit
$ echo retval:$? retvals:${PIPESTATUS[@]}
dash: Syntax error: Bad substitution
$
Bash has many original functions
44
$ bash
$ exit 10 | exit 20 | exit
$ echo retval:$? retvals:${PIPESTATUS[@]}
retval:0 retvals:10 20 0
$ exit
$ dash
$ exit 10 | exit 20 | exit
$ echo retval:$? retvals:${PIPESTATUS[@]}
dash: Syntax error: Bad substitution
$
bashにはいろいろ便利な独自機能がある
45
Bash has many original functions
46
【演習】逆に、bashの方が不便なこともある
47
#!/bin/zsh
if [ "_$1" = '_' ]; then
# 何もしない
else
echo "引数に$1が設定されました"
fi
[exercise] Know worse case of bash than the other shells
48
#!/bin/zsh
if [ "_$1" = '_' ]; then
# Do nothing
else
echo "$1 was set as the first argument."
fi
【演習】逆に、bashの方が不便なこともある
49
#!/bin/bash
if [ "_$1" = '_' ]; then
# 何もしない
else
echo "引数に$1が設定されました"
fi
[exercise] Know worse case of bash than the other shells
50
#!/bin/bash
if [ "_$1" = '_' ]; then
# Do nothing
else
echo "$1 was set as the first argument."
fi
【演習】逆に、bashの方が不便なこともある
51
#!/bin/bash
if [ "_$1" = '_' ]; then
# 何もしない
:
else
echo "引数に$1が設定されました"
fi
これが必要
[exercise] Know worse case of bash than the other shells
52
#!/bin/bash
if [ "_$1" = '_' ]; then
# Do nothing
:
else
echo "$1 was set as the first argument."
fi
Required!
POSIX中心主義における注意点
53
Importance for POSIX Centric programming
54
2.3. コマンドにある方言
55
2.3. Dialect on commands
56
そもそも、OSによって存在しないコマンドがある
などなど……
57
There are original cmds which isn’t supported by the other OSs
etc…...
58
そもそも、OSによって存在しないコマンドがある
59
POSIX文書が閲覧できるページ
There are original cmds which isn’t supported by the other OSs
60
POSIX document page
方言が強いコマンド その1 ― date
せっかくできるのに書式が違う!
※ 無いと不便な日時計算を行う方法は、今後の講義で解説。
61
A tight dialect command (1) - date
Both implement are possible but they have incompatible usages.
※ A compatible command we made will be introduced #8 of this lecture.
62
方言が強いコマンド その2 ― tr
63
$ zcat /usr/share/man/man1/tr.1.gz | head | tr -d '0-9A-Za-z'
$ zcat /usr/share/man/man1/tr.1.gz | head | tr -d '[0-9][A-Z][a-z]'
A tight dialect command (2) ― tr
64
$ zcat /usr/share/man/man1/tr.1.gz | head | tr -d '0-9A-Za-z'
$ zcat /usr/share/man/man1/tr.1.gz | head | tr -d '[0-9][A-Z][a-z]'
方言が強いコマンド その2 ― tr
65
A tight dialect command (2) ― tr
66
方言が強いコマンド その3 ― GNU版コマンド群
67
A tight dialect command (3) ― GNU commands
68
方言が強いコマンド その3 ― GNU製コマンド群
見比べるとPOSIXに無いオプションや機能が多数
69
A tight dialect command (3) ― GNU commands
You can know how many original functions GNU commands have.
70
【演習】挙動が異なるコマンド
71
$ date +%Y%m%d | sed 's/./&_/g' | tr _ '\n' | sort | uni�q -c | sort -r
[exercise] Experience differences of command behaviours
72
$ date +%Y%m%d | sed 's/./&_/g' | tr _ '\n' | sort | uni�q -c | sort -r
挙動が異なるコマンド
73
$ date +%Y%m%d …
2 6
2 2
2 0
1 5
1 1
1
$
$ date +%Y%m%d …
2 6
2 2
2 0
1 5
1 1
1
$
FreeBSD
CentOS
[exercise] Experience differences of command behaviours
74
$ date +%Y%m%d …
2 6
2 2
2 0
1 5
1 1
1
$
$ date +%Y%m%d …
2 6
2 2
2 0
1 5
1 1
1
$
on FreeBSD
on CentOS
挙動が異なるコマンド
75
$ date +%Y%m%d …
2 6
2 2
2 0
1 5
1 1
1
$
$ date +%Y%m%d …
2 6
2 2
2 0
1 5
1 1
1
$
[exercise] Experience differences of command behaviours
76
$ date +%Y%m%d …
2 6
2 2
2 0
1 5
1 1
1
$
$ date +%Y%m%d …
2 6
2 2
2 0
1 5
1 1
1
$
挙動が異なるコマンド
77
[exercise] Experience differences of command behaviours
78
参考ページ
79
See also
80
2.4. 正規表現にある方言
81
2.4. Dialects of Regular Expressions
82
正規表現とは?
83
What is Regular Expression (RE) ?
84
正規表現(Regular Expression)にも方言がある
※ 「その他」はどれもEREの拡張になっている
※ EREはBREの完全な上位互換ではない
85
There are some types on Regular Expressions
※ All Non-POSIXs is based on ERE
※ ERE is not completely an upward compatible with BRE!
86
覚えるべき正規表現の種類は2(+1)種類
「その他」はどれもEREの拡張なので後でもよいし、POSIXにはない
87
You have to learn the only 3 types!
“Others” are unnecessary as long as we program with only POSIX commands.
88
後は、どのコマンドがどれに対応しているか覚えるだけ
89
コマンド名 | 対応しているもの | コマンド名 | 対応しているもの |
AWK | AWKの方言 | grep (“-E”あり) | ERE |
ed | BRE | more | BRE |
egrep | ERE | sed | BRE |
ex | BRE | vi | BRE |
grep (“-E”なし) | BRE | | |
Next, you have to learn which command supports which RE
90
Command | Supporting type | Command | Supporting type |
AWK | AWK’s dialect | grep (with “-E” opt.) | ERE |
ed | BRE | more | BRE |
egrep | ERE | sed | BRE |
ex | BRE | vi | BRE |
grep (w/o “-E” opt.) | BRE | | |
BRE、ERE、AWKの方言の詳細
91
See also
92
方言を検証するためのソフト
93
The software to experience various dialects
94
3.本日の宿題
95
3. Assignment
96
正規表現の使い方を覚える
97
$ echo ’文字列’ | grep ’解答となる正規表現’
$ echo ’文字列’ | grep -E ’解答となる正規表現’
$ echo ’文字列’ | sed ’s/解答となる正規表現/~/g’
Exercise of Regular Expression
98
$ echo ’string’ | grep ’your RE’
$ echo ’string’ | grep -E ’your RE’
$ echo ’string’ | sed ’s/your RE/~/g’
講義スライド案内 ― Lecture Slide Index
99