ShellCheckをMacに導入してシェルスクリプトの文法チェックをしてみた

## やりたいこと
* シェルの文法チェックを走らせたあとでレビューとかできるといいなと考えていたら
 * これだ：[`ShellCheck`](http://www.shellcheck.net/)

### 環境
* Mac
* ShellCheck
 * homebrewで導入する

### インストール
* [公式](http://www.shellcheck.net/)より：https://github.com/koalaman/shellcheck

```
brew install shellcheck
```

#### 動作テスト
```
% shellcheck -V
ShellCheck - shell script analysis tool
version: 0.3.5
license: GNU Affero General Public License, version 3
website: http://www.shellcheck.net

% shellcheck
No files specified.

Usage: shellcheck [OPTIONS...] FILES...
  -e CODE1,CODE2..  --exclude=CODE1,CODE2..  exclude types of warnings
  -f FORMAT         --format=FORMAT          output format
  -s SHELLNAME      --shell=SHELLNAME        Specify dialect (bash,sh,ksh,zsh)
  -V                --version                Print version information
```

### 文法チェック
* 元となるのはこちら： [About ShellCheck](http://www.shellcheck.net/about.html)
* [wiki](https://github.com/koalaman/shellcheck/wiki)にて、チェック内容の理由が。
 * 適当に手元のスクリプトにあてると以下のチェックが走る。便利！
 * __なお、vimで作業中にもチェックすることもできる。__

```bash
% shellcheck ./bin/setup.sh

In ./bin/setup.sh line 4:
WORK_DIR="~/dotfiles"
          ^-- SC2088: Note that ~ does not expand in quotes.


In ./bin/setup.sh line 31:
  ln -s ${WORK_DIR}/${FILE} ~/${FILE}
                     ^-- SC2086: Double quote to prevent globbing and word splitting.
                               ^-- SC2086: Double quote to prevent globbing and word splitting.


In ./bin/setup.sh line 46:
if [ `uname` = "Darwin" ]; then
     ^-- SC2046: Quote this to prevent word splitting.
     ^-- SC2006: Use $(..) instead of deprecated `..`


In ./bin/setup.sh line 53:
  open -a Safari "https://developer.apple.com/downloads/index.action" && read key
                                                                              ^-- SC2034: key appears unused. Verify it or export it.
```

### 文法チェックに従って修正してみた

#### [SC2088](https://github.com/koalaman/shellcheck/wiki/Sc2088): Note that ~ does not expand in quotes
```diff
-WORK_DIR="~/dotfiles"
+WORK_DIR=~/"dotfiles" 
```

#### [SC2086](https://github.com/koalaman/shellcheck/wiki/Sc2086): Double quote to prevent globbing and word splitting.
```diff
-  ln -s ${WORK_DIR}/${FILE} ~/${FILE}
+  ln -s ${WORK_DIR}/"${FILE}" ~/"${FILE}"
``` 

#### [SC2046](https://github.com/koalaman/shellcheck/wiki/Sc2046): Quote this to prevent word splitting.
* 警告の対処としてはこうだが、SC2006で対処が本来の形

```diff
-if [ `uname` = "Darwin" ]; then
+if [ "`uname`"  = "Darwin" ]; then
```

#### [SC2006](https://github.com/koalaman/shellcheck/wiki/Sc2006): Use $(..) instead of deprecated \`..\`
* 上記とかぶりつつだが、以下にしろということ

```diff
-if [ "`uname`"  = "Darwin" ]; then
+if [ "$(uname)"  = "Darwin" ]; then
```

#### [SC2034](https://github.com/koalaman/shellcheck/wiki/Sc2034): key appears unused. Verify it or export it.
* 今回は、ダブルクォートで囲むだけなので省略

### まとめ
シェルスクリプトを書くときに丁寧にしたいときには役にたちそうと感じたので使うことにする

---

## おまけ
* Macでも、そうでない環境でも`Haskell`の`Cabal`でも

### `Cabal`
* Haskellのパッケージ管理で導入も可能だったようだ
* これもhomebrewで入る ... `homebrew install cabal`
* 他のLinux等の環境なら `cabal` 入れての手順でよさそう

```shell-session
% cabal update
Config file path source is default config file.
Config file /Users/usr0600109/.cabal/config not found.
Writing default configuration to /Users/usr0600109/.cabal/config
Downloading the latest package list from hackage.haskell.org
Note: there is a new version of cabal-install available.
To upgrade, run: cabal install cabal-install
% cabal install cabal-install
Resolving dependencies...
Downloading Cabal-1.20.0.3...
Downloading network-2.6.0.2...
Downloading random-1.1...
Configuring network-2.6.0.2...
Downloading stm-2.4.4...
Configuring Cabal-1.20.0.3...
Configuring random-1.1...
Downloading transformers-0.4.2.0...
Downloading zlib-0.5.4.2...
Configuring stm-2.4.4...
Downloading text-1.2.0.3...
Building stm-2.4.4...
Building random-1.1...
Building network-2.6.0.2...
Installed stm-2.4.4
Configuring zlib-0.5.4.2...
Building zlib-0.5.4.2...
Configuring transformers-0.4.2.0...
Installed random-1.1
Building transformers-0.4.2.0...
Configuring text-1.2.0.3...
Installed zlib-0.5.4.2
Building text-1.2.0.3...
Installed network-2.6.0.2
Installed transformers-0.4.2.0
Downloading mtl-2.2.1...
Configuring mtl-2.2.1...
Building mtl-2.2.1...
Installed mtl-2.2.1
Building Cabal-1.20.0.3...
Installed text-1.2.0.3
Downloading parsec-3.1.7...
Configuring parsec-3.1.7...
Building parsec-3.1.7...
Installed parsec-3.1.7
Downloading network-uri-2.6.0.1...
Configuring network-uri-2.6.0.1...
Building network-uri-2.6.0.1...
Installed network-uri-2.6.0.1
Downloading HTTP-4000.2.19...
Configuring HTTP-4000.2.19...
Building HTTP-4000.2.19...
Installed HTTP-4000.2.19
Installed Cabal-1.20.0.3
Downloading cabal-install-1.20.0.6...
Configuring cabal-install-1.20.0.6...
Building cabal-install-1.20.0.6...
Installed cabal-install-1.20.0.6
```

### `cabal install shellcheck`
* インストール

```
% cabal install shellcheck
Resolving dependencies...
Downloading primitive-0.5.4.0...
Downloading regex-base-0.93.2...
Configuring primitive-0.5.4.0...
Downloading syb-0.4.4...
Configuring regex-base-0.93.2...
Configuring syb-0.4.4...
Building syb-0.4.4...
Building regex-base-0.93.2...
Building primitive-0.5.4.0...
Installed regex-base-0.93.2
Downloading regex-posix-0.95.2...
Configuring regex-posix-0.95.2...
Installed syb-0.4.4
Downloading json-0.9...
Building regex-posix-0.95.2...
Configuring json-0.9...
Installed primitive-0.5.4.0
Downloading tf-random-0.5...
Configuring tf-random-0.5...
Building json-0.9...
Building tf-random-0.5...
Installed tf-random-0.5
Downloading QuickCheck-2.7.6...
Configuring QuickCheck-2.7.6...
Installed regex-posix-0.95.2
Downloading regex-compat-0.95.1...
Building QuickCheck-2.7.6...
Configuring regex-compat-0.95.1...
Building regex-compat-0.95.1...
Installed json-0.9
Installed regex-compat-0.95.1
Installed QuickCheck-2.7.6
Downloading ShellCheck-0.3.5...
Configuring ShellCheck-0.3.5...
Building ShellCheck-0.3.5...
Installed ShellCheck-0.3.5
```

* 入った
 * ちゃんと使うならパス設定しつつだろう

```
% cabal list shellcheck
* ShellCheck
    Synopsis: Shell script analysis tool
    Default available version: 0.3.5
    Installed versions: 0.3.5
    Homepage: http://www.shellcheck.net/
    License:  AGPL-3
```