bashコーディング規約

コーディング規約

参考 参考

  • ヘッダを記載する
  • ヘッダの直後に基本的な環境変数を定義する
    • umask
    • LANG
    • PATH
  • インデントは半角スペース 2 つ
  • | は前後にスペース 1 つ
  • 、»は前にスペース 1 つ、後ろは空けない

  • <、«も前にスペース 1 つ、後ろは空けない
  • if と then は同一行に、セミコロンの直後はスペース 1 つ
  • for、while の do と done は行頭を揃える
  • 関数定義の function は省略してもよい(しなくてもよい)
  • if 文を省略しない
  • 連続した同一ファイルへのリダイレクトはグルーピング
  • 変数の命名規則
    • 定数は大文字
    • 定数以外は小文字
    • 複数の単語を連結する場合は「_」で結合する
    • readonly 宣言を付けて、読み取り専用にすることを推奨
    • ファイル/ディレクトリの絶対パスの場合は、HOGE_FILE または FUGA_DIR
    • ファイル名またはディレクトリ名のみの場合は、HOGE_FILENAME または FUGA_DIRNAME
  • 今後増える見込みのない少ない繰り返しはループしない
  • 正常終了時はリターンコード 0
  • 異常終了時はリターンコード 0 以外
  • ログレベル
    • INFO: 参考情報
    • NOTICE: 注意レベル
    • WARNING: 警告レベル、処理を中断しないスキップ程度の問題
    • ERROR: エラー、処理を中断するレベルの問題

シェルスクリプトのコーディングルール 2014

参考

## 4. フィルタとパイプを意識する
UNIX ツールのほとんどはこの思想のもとに作成されている。データ入力には stdin を使用し、データ出力には
stdout を使用するべきである。こうすることで、パイプによるフィルタリング処理がしやすくなる。パイプでフィル
タしたデータを受け渡し出来るようになれば、他のツールとの連携が行える。

$ cat pipe.sh
#!/bin/bash
if [ -p /dev/stdin ]; then
    echo "Pipe"
    cat -
fi
$ echo "abc" | ./pipe.sh
Pipe
abc

こうすることでパイプを捕捉し、フィルタコマンドとしての役割を担うことができるようになります。
5. 変数を管理する

多くのプログラミング言語では、変数にはスコープが存在しますが、シェルスクリプトの場合、関数内で宣言した変数
もグローバル変数となります(明示的に local コマンドで局所変数化する必要がある)。またシェルスクリプトでは
、環境変数も扱うことが可能です。そこで以下のルールで管理すると良いでしょう(自分なりのルールがあればよい)
。

- 環境変数: 全て大文字(export VARIABLE)
- グローバル変数: 大文字から始まる(Variable)
- ローカル変数: 全て小文字(local variable)

また、定数は readonly を使用し、大文字で宣言します。

- readonly HISTORY_FILE="/path/to/hist_file"
- readonly HISTORY_FILE_NAME="hist_file"

23:54:18.660445 IP (tos 0x0, ttl 249, id 64056, offset 0, flags [DF], proto TCP (6), length 113)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0xcd71 (correct), seq 4343:4404, ack 582, win 127, options [nop,nop,TS val 3757013497 ecr 318662817], length 61

23:54:18.660811 IP (tos 0x0, ttl 249, id 64057, offset 0, flags [DF], proto TCP (6), length 87)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0x30ba (correct), seq 4404:4439, ack 677, win 127, options [nop,nop,TS val 3757013497 ecr 318662817], length 35

23:54:18.661562 IP (tos 0x0, ttl 249, id 64058, offset 0, flags [DF], proto TCP (6), length 83)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0x9550 (correct), seq 4439:4470, ack 677, win 127, options [nop,nop,TS val 3757013497 ecr 318662817], length 31

23:54:18.661955 IP (tos 0x0, ttl 249, id 64059, offset 0, flags [DF], proto TCP (6), length 279)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0x8ab1 (correct), seq 4470:4697, ack 782, win 127, options [nop,nop,TS val 3757013498 ecr 318662817], length 227

23:54:18.662740 IP (tos 0x0, ttl 249, id 64060, offset 0, flags [DF], proto TCP (6), length 1280)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [.], cksum 0xcc8a (correct), seq 4697:5925, ack 782, win 127, options [nop,nop,TS val 3757013498 ecr 318662817], length 1228

23:54:18.663309 IP (tos 0x0, ttl 249, id 64061, offset 0, flags [DF], proto TCP (6), length 1280)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0x4776 (correct), seq 5925:7153, ack 782, win 127, options [nop,nop,TS val 3757013498 ecr 318662817], length 1228

23:54:18.663464 IP (tos 0x0, ttl 249, id 64062, offset 0, flags [DF], proto TCP (6), length 1280)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [.], cksum 0x76f1 (correct), seq 7153:8381, ack 782, win 127, options [nop,nop,TS val 3757013498 ecr 318662817], length 1228

23:54:18.663886 IP (tos 0x0, ttl 249, id 64063, offset 0, flags [DF], proto TCP (6), length 495)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0xda23 (correct), seq 8381:8824, ack 782, win 127, options [nop,nop,TS val 3757013498 ecr 318662817], length 443

23:54:18.663905 IP (tos 0x0, ttl 249, id 64064, offset 0, flags [DF], proto TCP (6), length 2508)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0xe7ea (incorrect -> 0xb83e), seq 8824:11280, ack 782, win 127, options [nop,nop,TS val 3757013498 ecr 318662817], length 2456

23:54:18.732033 IP (tos 0x0, ttl 249, id 64066, offset 0, flags [DF], proto TCP (6), length 1280)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [.], cksum 0x9f86 (correct), seq 11280:12508, ack 782, win 127, options [nop,nop,TS val 3757013568 ecr 318662888], length 1228

23:54:18.732226 IP (tos 0x0, ttl 249, id 64067, offset 0, flags [DF], proto TCP (6), length 1280)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0x7334 (correct), seq 12508:13736, ack 782, win 127, options [nop,nop,TS val 3757013568 ecr 318662888], length 1228

23:54:18.732226 IP (tos 0x0, ttl 249, id 64067, offset 0, flags [DF], proto TCP (6), length 1280)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0x7334 (correct), seq 12508:13736, ack 782, win 127, options [nop,nop,TS val 3757013568 ecr 318662888], length 1228

23:54:18.660445 IP (tos 0x0, ttl 249, id 64056, offset 0, flags [DF], proto TCP (6), length 113)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0xcd71 (correct), seq 4343:4404, ack 582, win 127, options [nop,nop,TS val 3757013497 ecr 318662817], length 61

23:54:18.660811 IP (tos 0x0, ttl 249, id 64057, offset 0, flags [DF], proto TCP (6), length 87)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0x30ba (correct), seq 4404:4439, ack 677, win 127, options [nop,nop,TS val 3757013497 ecr 318662817], length 35

23:54:18.661562 IP (tos 0x0, ttl 249, id 64058, offset 0, flags [DF], proto TCP (6), length 83)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0x9550 (correct), seq 4439:4470, ack 677, win 127, options [nop,nop,TS val 3757013497 ecr 318662817], length 31

23:54:18.661955 IP (tos 0x0, ttl 249, id 64059, offset 0, flags [DF], proto TCP (6), length 279)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0x8ab1 (correct), seq 4470:4697, ack 782, win 127, options [nop,nop,TS val 3757013498 ecr 318662817], length 227

23:54:18.662740 IP (tos 0x0, ttl 249, id 64060, offset 0, flags [DF], proto TCP (6), length 1280)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [.], cksum 0xcc8a (correct), seq 4697:5925, ack 782, win 127, options [nop,nop,TS val 3757013498 ecr 318662817], length 1228

23:54:18.663309 IP (tos 0x0, ttl 249, id 64061, offset 0, flags [DF], proto TCP (6), length 1280)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0x4776 (correct), seq 5925:7153, ack 782, win 127, options [nop,nop,TS val 3757013498 ecr 318662817], length 1228

23:54:18.663464 IP (tos 0x0, ttl 249, id 64062, offset 0, flags [DF], proto TCP (6), length 1280)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [.], cksum 0x76f1 (correct), seq 7153:8381, ack 782, win 127, options [nop,nop,TS val 3757013498 ecr 318662817], length 1228

23:54:18.663886 IP (tos 0x0, ttl 249, id 64063, offset 0, flags [DF], proto TCP (6), length 495)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0xda23 (correct), seq 8381:8824, ack 782, win 127, options [nop,nop,TS val 3757013498 ecr 318662817], length 443

23:54:18.663905 IP (tos 0x0, ttl 249, id 64064, offset 0, flags [DF], proto TCP (6), length 2508)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0xe7ea (incorrect -> 0xb83e), seq 8824:11280, ack 782, win 127, options [nop,nop,TS val 3757013498 ecr 318662817], length 2456

23:54:18.732033 IP (tos 0x0, ttl 249, id 64066, offset 0, flags [DF], proto TCP (6), length 1280)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [.], cksum 0x9f86 (correct), seq 11280:12508, ack 782, win 127, options [nop,nop,TS val 3757013568 ecr 318662888], length 1228

23:54:18.732226 IP (tos 0x0, ttl 249, id 64067, offset 0, flags [DF], proto TCP (6), length 1280)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0x7334 (correct), seq 12508:13736, ack 782, win 127, options [nop,nop,TS val 3757013568 ecr 318662888], length 1228

23:54:18.732226 IP (tos 0x0, ttl 249, id 64067, offset 0, flags [DF], proto TCP (6), length 1280)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0x7334 (correct), seq 12508:13736, ack 782, win 127, options [nop,nop,TS val 3757013568 ecr 318662888], length 1228