【Shell】ShellCheck ~ 入門編 ~

■ はじめに

静的コードチェックInspecode  / Rocro を掛ける際に

https://rocro.com/inspecode

どんなツールがサポートしているのかなっと調べていたら
「ShellCheck」ってのがあったので調べてみた

また、別記事で「shfmt」ってのみある。

shfmt ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2024/03/27/164109

目次

【1】ShellCheck
【2】Hello world
 1)出力結果
【3】インストール
【4】ローカル上での実行方法
 1)出力結果
 2)指摘部分の修正

【1】ShellCheck

* シェルスクリプトの lint ツール

https://www.shellcheck.net/
Github
https://github.com/koalaman/shellcheck.net
Wiki
https://www.shellcheck.net/wiki/Home

【2】Hello world

* とりあえず、ちょこっと試すなら、以下のサイトで行える

https://www.shellcheck.net/

* 以下の関連記事のシェルスクリプトを掛けてみる

https://dk521123.hatenablog.com/entry/2024/03/02/000000

#!/bin/bash

sqlfmt --version &> /dev/null
if [ $? -eq 0 ];
then
  echo "Found sqlfmt"
  which sqlfmt
  sqlfmt --version
else
  echo "sqlfmt is not supported in your env..."
fi

1)出力結果

* 上記のシェルを以下のサイトで実行すると、、、

https://www.shellcheck.net/

shellcheck myscript
 
Line 4:
if [ $? -eq 0 ];
     ^-- SC2181 (style): Check exit code directly with e.g. 'if mycmd;', not indirectly with $?.

https://www.shellcheck.net/wiki/SC2181

Check exit code directly with e.g. if mycmd;, not indirectly with $?.

Problematic code:

make mytarget

if [ $? -ne 0 ]
then
  echo "Build failed"
fi

Correct code:

if ! make mytarget
then
  echo "Build failed"
fi

【3】インストール

Debian

sudo apt install shellcheck

shellcheck --version

Redhut

yum -y install epel-release
yum -y install ShellCheck

shellcheck -V

VS Code
https://github.com/vscode-shellcheck

Pluginで「ShellCheck」を検索するとでてくるので
インストールする

【4】ローカル上での実行方法

# shellcheck <SHELL_NAME>
shellcheck test.sh

1)出力結果

出力結果

In test.sh line 4:
if [ $? -eq 0 ];
     ^-- SC2181 (style): Check exit code directly with e.g. 'if mycmd;', not indirectly with $?.

For more information:
  https://www.shellcheck.net/wiki/SC2181 -- Check exit code directly with e.g...

2)指摘部分の修正

修正後

#!/bin/bash                                       
                                                  
if command -v sqlfmt                              
then                                              
  echo "Found sqlfmt"                             
  which sqlfmt                                    
  sqlfmt --version                                
else                                              
  echo "sqlfmt is not supported in your env..."   
fi                                                

実行例

# 修正してもちゃんと動く
$ ./test.sh
/usr/local/bin/sqlfmt
Found sqlfmt
/usr/local/bin/sqlfmt
sqlfmt, version 0.21.2

参考文献

https://qiita.com/znz/items/63a3d581e8ed6ff11b8e
https://future-architect.github.io/articles/20210329/
https://christina04.hatenablog.com/entry/lint-shell-script-with-shellcheck
https://note.com/navitime_tech/n/n0675e103bafa

 関連記事

シェル ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2014/10/23/005406
shfmt ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2024/03/27/164109
シェル ~ 環境変数 IFS ~
https://dk521123.hatenablog.com/entry/2024/01/24/234634
ファイルに関する処理あれこれ
https://dk521123.hatenablog.com/entry/2020/09/28/000000
setコマンド
https://dk521123.hatenablog.com/entry/2021/09/14/000000
Linux ~ ユーザに関わるコマンド ~
https://dk521123.hatenablog.com/entry/2016/06/26/233349
シェル ~ インストール済みかどうか調べる ~
https://dk521123.hatenablog.com/entry/2024/03/02/000000