Open11

シェルスクリプトを書く技術

YetAnother_ykYetAnother_yk

ちょっとした処理などのために急に必要になり、都度調べるが抜け漏れがあっても嫌だし、調べるのにも時間がかかったりするので事前にまとめておく

YetAnother_ykYetAnother_yk

ShellCheck

ShellCheck – shell script analysis tool
https://www.shellcheck.net/

オープンソースの静的解析ツールで、シェルスクリプト内のバグを自動的に検出する機能を提供する。
公式ページにテキストエリアがあるので、そこで試すことができる。

pre-commit を使っているなら…

.pre-commit-config.yaml
  - repo: https://github.com/shellcheck-py/shellcheck-py
    rev: v0.10.0.1
    hooks:
      - id: shellcheck

GitHub Actions を使っているなら…

      - name: Run ShellCheck
        uses: ludeeus/action-shellcheck@2.0.0
        with:
          check_together: 'yes'

TODO: reviewdog 版も確認する
reviewdog/action-shellcheck: Run shellcheck with reviewdog
https://github.com/reviewdog/action-shellcheck

VSCode を使っているなら…
ShellCheck - Visual Studio Marketplace
https://marketplace.visualstudio.com/items?itemName=timonwong.shellcheck

YetAnother_ykYetAnother_yk

shfmt

mvdan/sh: A shell parser, formatter, and interpreter with bash support; includes shfmt
https://github.com/mvdan/sh

フォーマッター、このスクラップのために調べたら出てきたので、使うときに詳細更新します。

pre-commit を使っているなら…
https://github.com/scop/pre-commit-shfmt

GitHub Actions を使っているなら…
https://github.com/luizm/action-sh-checker

      - name: Run the sh-checker
        uses: luizm/action-sh-checker@master
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          SHELLCHECK_OPTS: -e SC1004 # exclude some shellcheck warnings.
          SHFMT_OPTS: -s # arguments to shfmt.
        with:
          sh_checker_comment: true
          sh_checker_exclude: ".terraform ^dir/example.sh"

shellcheck,checkbashisms も合わせて実行してくれる

VSCode を使っているなら…
https://marketplace.visualstudio.com/items?itemName=foxundermoon.shell-format
https://marketplace.visualstudio.com/items?itemName=mkhl.shfmt

YetAnother_ykYetAnother_yk

checkbashisms

POSIX準拠のシェルスクリプトとなるように、bash 依存のコマンドや構文をある程度指摘してくれる。
#!/bin/sh とするときに大事そう。
このスクラップのために調べたら出てきたので、使うときに詳細更新します。

YetAnother_ykYetAnother_yk

snippets

YetAnother_ykYetAnother_yk

ディレクトリをシェルファイルのディレクトリに変更する

SCRIPT_DIR=$(dirname "$0")
cd "$SCRIPT_DIR" || exit
YetAnother_ykYetAnother_yk

エラーで続行しないようにする等

set -euo pipefail

エラーで落ちる、初期化していない変数の使用で落ちる、パイプの途中でエラーがあれば落ちる

x を入れると実行するコマンドを出力してくれる

YetAnother_ykYetAnother_yk

区切り文字の変更

デフォルトだとスペースも区切られてしまい、for 文で扱うときに好ましくないことも多い。
タブ・改行のみに絞るなら…

IFS=$'\n\t'
YetAnother_ykYetAnother_yk

PDF系で使えそうなコマンド

pdftoppm

【 pdftoppm 】コマンド――PDFファイルを画像に変換する:Linux基本コマンドTips(288) - @IT
https://atmarkit.itmedia.co.jp/ait/articles/1903/08/news039.html

pdfimages

【 pdfimages 】コマンド――PDFファイルから画像を抽出する:Linux基本コマンドTips(287) - @IT
https://atmarkit.itmedia.co.jp/ait/articles/1903/07/news003.html

pdftotext

【 pdftotext 】コマンド――PDFファイルからテキストを抽出する:Linux基本コマンドTips(286) - @IT
https://atmarkit.itmedia.co.jp/ait/articles/1903/01/news029.html

pdfunite

【 pdfunite 】コマンド――複数のPDFファイルを結合する:Linux基本コマンドTips(284) - @IT
https://atmarkit.itmedia.co.jp/ait/articles/1902/22/news041.html

pdfseparate

【 pdfseparate 】コマンド――PDFファイルを1ページごとに分割する:Linux基本コマンドTips(283) - @IT
https://atmarkit.itmedia.co.jp/ait/articles/1902/21/news033.html

pdfinfo

【 pdfinfo 】コマンド――PDFファイルの情報を表示する:Linux基本コマンドTips(282) - @IT
https://atmarkit.itmedia.co.jp/ait/articles/1902/15/news034.html