bash(set)コマンドのオプション3選

15

Click here to load reader

Upload: yuuki-namikawa

Post on 24-May-2015

4.362 views

Category:

Technology


3 download

TRANSCRIPT

Page 1: bash(set)コマンドのオプション3選

bash(set)コマンドオプション3選

並河 祐貴 (Yuuki NAMIKAWA)id:rx7 / @namikawa

nanapi勉強会 Vol.2, 2014/05/29

Page 2: bash(set)コマンドのオプション3選

自己紹介• 並河 祐貴 (a.k.a. id:rx7)

• (株)サイバーエージェント所属• エンジニア(Ops)• Blog: http://d.hatena.ne.jp/rx7/• Twitter: @namikawa

• 著書・寄稿多数

Page 3: bash(set)コマンドのオプション3選
Page 4: bash(set)コマンドのオプション3選

Historyに残っているコマンド実⾏数のランキング

$ history | awk '{print $2}' | sort | uniq -c | sort -nr

Page 5: bash(set)コマンドのオプション3選

私の結果・・・$ history | awk '{print $2}' | sort | uniq -c | sort -nr

390 ll322 cd217 vim

・・・省略・・・

1 e1 xit1 ihai1 ヴぁgらんt1 :q

Page 6: bash(set)コマンドのオプション3選

本題

Page 7: bash(set)コマンドのオプション3選

bashのシェルオプション• man bashで勉強

• SHELLOPTS変数で確認できる

• setコマンドとかでも指定できる

$ echo $SHELLOPTSbraceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor

Page 8: bash(set)コマンドのオプション3選

ignoreeof• シェルの⼊⼒終了を防ぐ

– Ctrl+R と Ctrl+D を押し間違えたりとか...

• set –o ignoreeof

• デフォルト: “IGNOREEOF=10”

Page 9: bash(set)コマンドのオプション3選

errexit• シェルスクリプト内でエラーが発生した

場合に、そこで終了

• set –e [or] set –o errexit

Page 10: bash(set)コマンドのオプション3選

errexit

$ cat test.sh #!/bin/bash

mkdir /tmp/hoge/fugatouch /tmp/hoge/fuga/test.txt

$ bash test.shmkdir: cannot create directory '/tmp/hoge/fuga': No such file or directorytouch: cannot touch '/tmp/hoge/fuga/test.txt': No such file or directory

$ bash -e test.shmkdir: cannot create directory '/tmp/hoge/fuga': No such file or directory

Page 11: bash(set)コマンドのオプション3選

xtrace• シェルスクリプト内で実⾏したコマンド

を細かく出⼒・展開してくれる

• set –x [or] set –o xtrace

Page 12: bash(set)コマンドのオプション3選

xtrace$ cat test.sh #!/bin/bash

mkdir /tmp/hoge/touch /tmp/hoge/test{1,2}.txtls -l /tmp/hoge/*

$ bash test.sh -rw-r--r-- 1 nami nami 0 5月 28 23:55 /tmp/hoge/test1.txt-rw-r--r-- 1 nami nami 0 5月 28 23:55 /tmp/hoge/test2.txt

$ bash -x test.sh + mkdir /tmp/hoge/+ touch /tmp/hoge/test1.txt /tmp/hoge/test2.txt+ ls -l /tmp/hoge/test1.txt /tmp/hoge/test2.txt-rw-r--r-- 1 nami nami 0 5月 28 23:56 /tmp/hoge/test1.txt-rw-r--r-- 1 nami nami 0 5月 28 23:56 /tmp/hoge/test2.txt

Page 13: bash(set)コマンドのオプション3選

norc• ~/.bashrc の読み込み・実⾏を⾏わない

• bash --norc

Page 14: bash(set)コマンドのオプション3選

norc$ cat .bashrcalias ll='ls -l‘

$ bashbash-4.3$ lltotal 32drwx------ 3 nami nami 102 5 26 11:08 Applicationsdrwxr-xr-x+ 4 nami nami 136 5 28 23:06 Desktop

・・・省略・・・

$ bash --norcbash-4.3$ llbash: ll: コマンドが⾒つかりません

Page 15: bash(set)コマンドのオプション3選

さいごに• シェルオプションは色々ある

– man bash に全部書いてあります・・・