Slide 1

Slide 1 text

ShellCheckで ShellCheckで ShellScriptを少し ShellScriptを少し まともに まともに Kenichiro Matohara(matoken) 1 / 21

Slide 2

Slide 2 text

南隅から参加(鹿児島の右下) 好きなLinuxディストリビューションはDebian お仕事募集 mailto:work@matohara.org Kenichiro Matohara(matoken) Kenichiro Matohara(matoken) https://matoken.org https://matoken.org 2 / 21

Slide 3

Slide 3 text

最近の発表 最近の発表 で発表した「Linux Chromium でビデオのハードウ ェアエンコード・デコードしたい」→間違いがあったので修正してか らUp予定 https://speakerdeck.com/matoken/ 昨日の東海道らぐ 3 / 21

Slide 4

Slide 4 text

鹿児島らぐ 鹿児島らぐ 次回04/25( ) https://kagolug.org/ https://kagolug.org/ Cross Distro Developers Campと同じ日 鹿児島Linux勉強会 2021.04(オンライン開催) - connpass 4 / 21

Slide 5

Slide 5 text

ShellCheckでshell scriptをまともに ShellCheckでshell scriptをまともに する する via ,. /-| (--; (,-'; _/_.-'; _/-.__._< .-'`-.__ '\ .'`---=___`===': /_..---' ___.--.' |` ___.--' __ .i| |-' ___.--'_.8:E| \,-' __.-/88::E! `-.,' _.'|88::E| `;'../88:: E; |.'!88::E"/ /.''!"iiE / `--'`._.-' hjw https://ascii.co.uk/art/shell 5 / 21

Slide 6

Slide 6 text

Shell Script Shell Script Shell Script はコマンドラインと近いのでお手軽に書ける うろ覚えの文法でも動いてしまう shellの互換性の問題 basism! 調べるのは面倒 manや書籍? 6 / 21

Slide 7

Slide 7 text

ShellCheck ShellCheck ShellScriptの静的解析ツール 各ディストリビューションパッケージがある ShellCheck – shell script analysis tool 7 / 21

Slide 8

Slide 8 text

Webでお試し Webでお試し 8 / 21

Slide 9

Slide 9 text

コマンドライン コマンドライン $ shellcheck ./sample.sh In ./sample.sh line 3: for f in $(ls *.m3u) ^---------^ SC2045: Iterating over ls output is fragile. Use globs. ^-- SC2035: Use ./*glob* or -- *glob* so names with dashes won't become In ./sample.sh line 5: grep -qi hq.*mp3 $f \ ^-----^ SC2062: Quote the grep pattern so the shell won't interpret it. ^-- SC2086: Double quote to prevent globbing and word splitting. Did you mean: grep -qi hq.*mp3 "$f" \ In ./sample.sh line 6: && echo -e 'Playlist $f contains a HQ file in mp3 format' ^-- SC2039: In POSIX sh, echo flags are undefined. ^ SC2016 E i d 't d i i l t d bl t 9 / 21

Slide 10

Slide 10 text

10 / 21

Slide 11

Slide 11 text

ShellCheckを参考に書き換え ShellCheckを参考に書き換え ↓ 警告と修正例が便利 Wikiで詳細が確認できる #!/bin/sh ## Example: a typical script with several problems for f in $(ls *.m3u) do grep -qi hq.*mp3 $f \ && echo -e 'Playlist $f contains a HQ file in mp3 format' done #!/bin/sh ## Example: a typical script with several problems for f in *.m3u do [ -e "$f" ] || break grep -qi "hq.*mp3" "$f" \ && echo "Playlist $f contains a HQ file in mp3 format" done 11 / 21

Slide 12

Slide 12 text

脱basism 脱basism 12 / 21

Slide 13

Slide 13 text

basism? basism? bashが入っているとは限らない bash依存なのに #!/bin/sh (/bin/sh だけど POSIX非互換も) 可搬性を考えると脱basidmしておきたい とはいえ移植予定のない近年のGNU/Linux環境用なら bash でも?(独 自拡張便利だし) 「Bash」+「-ism」 → 「bashism」 13 / 21

Slide 14

Slide 14 text

ShellCheckのチェックシェル変更 ShellCheckのチェックシェル変更 man 1 shellcheck -s でチェックする shell を指定できる 既定値は bash -s shell, –shell=shell Specify Bourne shell dialect. Valid values are sh, bash, dash and ksh. The default is to use the file's shebang, or bash if the target shell can't be determined. 14 / 21

Slide 15

Slide 15 text

その他 その他 Debian devscripts パッケージに checkbashisms というscriptも (未確認) $ sudo apt install devscripts $ checkbashisms -f slide/resources/sample.sh possible bashism in slide/resources/sample.sh line 6 (echo -e): grep -qi hq.*mp3 $f \ && echo -e 'Playlist $f contains a HQ file in mp3 format' 15 / 21

Slide 16

Slide 16 text

ShellCheckをテキストエディタ上 ShellCheckをテキストエディタ上 で で 様々なテキストエディタに対応 :) https://github.com/koalaman/shellcheck#in-your-editor 16 / 21

Slide 17

Slide 17 text

vim vim itspriddle/vim-shellcheck: Vim wrapper for ShellCheck, a static analysis tool for shell scripts. $ mkdir -p ~/.vim/pack/shellcheck/start $ cd ~/.vim/pack/shellcheck/start $ git clone https://github.com/itspriddle/vim-shellcheck.git $ vim -u NONE -c "helptags vim-shellcheck/doc" -c q 17 / 21

Slide 18

Slide 18 text

18 / 21

Slide 19

Slide 19 text

その他 その他 除外オプション 出力フォーマットの変更 .shellcheckrc 19 / 21

Slide 20

Slide 20 text

まとめ まとめ うろ覚えでなんとなく書いていたShellScriptを少しまともに 警告だけでなく編集例,詳細のWikiへのポインタも表示してくれ て便利 ちょっとしたことなら例を真似るだけでOK Shellの種別でのチェックも出来るので脱basismにも 20 / 21

Slide 21

Slide 21 text

奥付 奥付 発表 2021-04-25(Sun) 発表者 公開URL , , 利用ソフトウェア ライセンス CC BY-NC-SA 4.0 鹿児島Linux勉強会 2021.04(オンライン開催) Kenichiro Matohara(matoken) SpeakerDeck pdf pdf-sig source Asciidoctor Reveal.js 21 / 21