LoginSignup
81
69

More than 5 years have passed since last update.

シェルスクリプトのlint

Last updated at Posted at 2015-01-02

シェルスクリプトのLint。メモ書き。

-n オプション

構文チェックします。 -nつけて実行すればよい。冗長的になる-nvのほうがわかりやすいかも。

 $ sh -n  hoge.sh
 $ sh -nv hoge.sh

bashでPosixに準拠したコード、いわるゆbashに依存しないボーンシェルスクリプトの場合は、set -o してあげる。

 $ set -o posix
 $ bash -nv hoge.sh

checkbaskisms

Perl製のシェルスクリプトのlint。Posixに準拠しているかどうかチェックします。bashやksh固有のコードをチェックするってことですね。
http://sourceforge.net/projects/checkbaskisms/

こんなコード、配列は素のshでは...。このように叱ってくれます。

$ cat foo.sh 
#!/bin/sh
elems[0]=0
elems[1]=1
for x in ${elems[*]}; do
  echo $x
done

$ ./checkbashisms foo.sh
possible bashism in foo.sh line 3 (bash arrays, H[0]):
elems[0]=0
possible bashism in foo.sh line 4 (bash arrays, H[0]):
elems[1]=1
possible bashism in foo.sh line 5 (bash arrays, ${name[0|*|@]}):
for x in ${elems[*]}; do

posix準拠なコードのチェックだけで、構文まではチェックしてくれない。少し残念。

$ cat hoge.sh
#!/bin/sh
for x in 1 2 3 do
  echo $x
done

$  ./checkbashisms hoge.sh
$ echo $?
0

shlint

ruby製のシェルスクリプトのlint。中身はcheckbaskismsを呼んで + 各シェルで -n しているだけみたい。
https://github.com/duggan/shlint

$ gem install shlint
$ cat foo.sh
#!/bin/sh
elems[0]=0
elems[1]=1
for x in ${elems[*]}; do
  echo $x
done

$ shlint --debug ./foo.sh
possible bashism in ./foo.sh line 3 (bash arrays, H[0]):
elems[0]=0
possible bashism in ./foo.sh line 4 (bash arrays, H[0]):
elems[1]=1
possible bashism in ./foo.sh line 5 (bash arrays, ${name[0|*|@]}):
for x in ${elems[*]}; do
Using zsh...
Testing ./foo.sh
Using ksh...
Testing ./foo.sh
Using bash...
Testing ./foo.sh
Using dash...
Testing ./foo.sh
Using sh...
Testing ./foo.sh
Checking for bashisms...

shellcheck

最強なシェルスクリプトのlint。チェック厳しすぎる。
http://www.shellcheck.net/

shellcheck.png

WEB版だけでなくて、コマンドライン版もあるので、テスト自動化しやすい。
無視したい箇所には、そのコードの上に所定のコメントを書けばOKなようです。すこぶる便利ですね。

# shellcheck disable=code[,code...]

というので、shellcheck最強。

81
69
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
81
69