はじめに
Bash+Visual Studio Codeを使用した現時点での環境構築になります。
環境構築関連は時間の経過によって変化激しいためできるだけ最新のものを見たほうが良いです。
Linter -shellcheck-
インストール
# Ubuntu 22.04
sudo apt install shellcheck
# CentOS Stream 9
dnf config-manager --set-enabled crb
dnf install epel-release epel-next-release
dnf install ShellCheck
※CentOS系は"ShellCheck"という名前になっている
使い方
$ shellcheck xxx.sh
In xxx.sh line 51:
echo $comma_str
^--------^ SC2086 (info): Double quote to prevent globbing and word splitting.
Did you mean:
echo "$comma_str"
により標準出力で問題点や文法の間違いを指摘してくれる。
Formatter -shfmt-
go言語で作成
インストール
# Ubuntu 22.04
sudo apt install shfmt
※ CentOS Steam 9はないので公式サイトかバイナリをダウンロード後、shfmtにリネームして、/usr/local/bin/shfmtか/usr/bin/shfmtに格納。
使い方
shfmt xxx.sh
により標準出力で成形されたShellScriptが出力される。
Formatter -EditorConfig-
フォーマットのうち
- 文字コード(UTF-8等)
- 改行コード
- インデント(tab,space)
- インデントの数(2,4等)
- 改行コード前の空白を削除
を統一的に行うもの。
Visual Studio Code等のテキストエディタと組み合わせて使う。全体的に設定するといった感じではなくプロジェクトごとに使用する感じ。
詳細はこちら
作成例
root = true
[*.sh]
indent_style = tab
indent_size = 4
IDE -Bash Language Server-
テキストエディタをIDEとして実行できるようにしたもの。
Visual Studio Code等のテキストエディタと組み合わせて使う。
Visual Studio CodeではBash IDEで組み込まれている。
Debugger -bashdb-
使い方はこちら
解読 -explainshell.com-
難解なコマンドラインを解説してくれるサイト
Visual Studio Code拡張機能
- Bash IDE
- ShellCheck
- shell-format
- Bash Debug
- Shebang Snippets
- shellman
- EditorConfg for VS Code
Bash IDE
Bash Language Serverとexplainshellとshellcheckが組み合わさったもの。Bash Language Serverについては拡張機能に組み込まれているのでインストールは不要。shellcheckは外部コマンドを利用するのでインストールが必要。
コード補完、外部コマンドをホバーするとmanから読み取った使い方を表示してくれるなどがある。
shellcheckを使わない設定にする場合は以下のようにする。
{
"bashIde.shellcheckPath": "",
}
ShellCheck
shellcheckを利用した拡張機能。
外部コマンドshellcheckを利用するのでインストールが必要。Bash IDEと被るので上記の設定にするかこの拡張機能を外すかのどちらかにする。
文法が間違っている、良くない書き方の場合はお知らせしてくれる。
表示したくない項目は以下のように全体適用かコマンド適用のどちらかを利用する。
例えばSC2164を外したい場合は以下のようにコメントアウトか設定で
# shellcheck disable=SC2164
script_dir=$(cd "$(dirname "$0")"; pwd)
{
"shellcheck.exclude": ["2164"]
}
shell-format
shfmtを利用した拡張機能。
外部コマンドshfmtを利用するのでインストールが必要。
ctrl+shift+f(formatのf)によりフォーマットされる。
以下のようにshfmtの場所を指定する必要がある。
加えてEditorConfigと連携する場合は以下のようにする。
{
"shellformat.path": "/usr/bin/shfmt",
"shellformat.useEditorConfig": true,
}
Bash Debug
bashdbをGUI操作で可能にした拡張機能。
bashdbは組み込まれているのでインストール不要。
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "bashdb",
"request": "launch",
"name": "Bash-Debug (simplest configuration)",
"program": "${file}",
"args": []
}
]
}
必要ならカレントディレクトリや引数を設定して実行する。
EditorConfg for VS Code
EditorConfgを利用する場合。
ディレクトリにこれを入れて
root = true
[*.sh]
indent_style = tab
indent_size = 4
ctrl+shift+fをするとフォーマットされる。