プログラマ行進曲第二章

主にソフトウェア関連の技術をネタにした記事を執筆するためのブログ

シェルスクリプトでsprintfのようなことをしたくて調べた

タイトルのようにsprintfのようなことをしたかったのですが、少しググったら以下のブログ記事にバッチリやりたいことが書かれていたので、特に私の方で追加することもないのですが、こういうところで詰まったという記録残しの意味でブログを書きます。

d.hatena.ne.jp

上記の記事の著者が書いているように、シェルスクリプトの文法や書き方を知っている人からしたら特に問題ないのでしょうが、私は全くシェルスクリプトに慣れていないのでここに辿り着くまでに時間がかかってしまいました。

なんでこういうことをしたかったかというと、awscliを使ってRDS for PostgreSQLの調査をしているときにRDSのログを一括して調べる必要があったのですが、そのRDSのログの書式が error/postgresql.log.2018-12-23-09 のように、時刻が24h形式だったので、sprintfみたいなことをしてシェルスクリプトを組み立てて表示したいなと思ったからです。

以下のように formatted_hour=$(printf "%02d" $hour) みたいに書けば aws rds describe-db-log-files コマンドで得られるRDSのログの形式に沿ったファイル名が得られるので、それを aws rds download-db-log-file-portion に喰わせてやればエラーログの中身が簡単に見られるという寸法です。

for hour in `seq 1 23`
  do
  formatted_hour=$(printf "%02d" $hour)
  for day in `seq 24 26`
    do
    echo error/postgresql.log.2018-12-${day}-${formatted_hour}
    done
  done
error/postgresql.log.2018-12-24-01
error/postgresql.log.2018-12-25-01
error/postgresql.log.2018-12-26-01
error/postgresql.log.2018-12-24-02
error/postgresql.log.2018-12-25-02
error/postgresql.log.2018-12-26-02
error/postgresql.log.2018-12-24-03
error/postgresql.log.2018-12-25-03
error/postgresql.log.2018-12-26-03
error/postgresql.log.2018-12-24-04
error/postgresql.log.2018-12-25-04
error/postgresql.log.2018-12-26-04
error/postgresql.log.2018-12-24-05
error/postgresql.log.2018-12-25-05
error/postgresql.log.2018-12-26-05
error/postgresql.log.2018-12-24-06
error/postgresql.log.2018-12-25-06
error/postgresql.log.2018-12-26-06
error/postgresql.log.2018-12-24-07
error/postgresql.log.2018-12-25-07
error/postgresql.log.2018-12-26-07
error/postgresql.log.2018-12-24-08
error/postgresql.log.2018-12-25-08
error/postgresql.log.2018-12-26-08
error/postgresql.log.2018-12-24-09
error/postgresql.log.2018-12-25-09
error/postgresql.log.2018-12-26-09
error/postgresql.log.2018-12-24-10
error/postgresql.log.2018-12-25-10
error/postgresql.log.2018-12-26-10
error/postgresql.log.2018-12-24-11
error/postgresql.log.2018-12-25-11
error/postgresql.log.2018-12-26-11
error/postgresql.log.2018-12-24-12
error/postgresql.log.2018-12-25-12
error/postgresql.log.2018-12-26-12
error/postgresql.log.2018-12-24-13
error/postgresql.log.2018-12-25-13
error/postgresql.log.2018-12-26-13
error/postgresql.log.2018-12-24-14
error/postgresql.log.2018-12-25-14
error/postgresql.log.2018-12-26-14
error/postgresql.log.2018-12-24-15
error/postgresql.log.2018-12-25-15
error/postgresql.log.2018-12-26-15
error/postgresql.log.2018-12-24-16
error/postgresql.log.2018-12-25-16
error/postgresql.log.2018-12-26-16
error/postgresql.log.2018-12-24-17
error/postgresql.log.2018-12-25-17
error/postgresql.log.2018-12-26-17
error/postgresql.log.2018-12-24-18
error/postgresql.log.2018-12-25-18
error/postgresql.log.2018-12-26-18
error/postgresql.log.2018-12-24-19
error/postgresql.log.2018-12-25-19
error/postgresql.log.2018-12-26-19
error/postgresql.log.2018-12-24-20
error/postgresql.log.2018-12-25-20
error/postgresql.log.2018-12-26-20
error/postgresql.log.2018-12-24-21
error/postgresql.log.2018-12-25-21
error/postgresql.log.2018-12-26-21
error/postgresql.log.2018-12-24-22
error/postgresql.log.2018-12-25-22
error/postgresql.log.2018-12-26-22
error/postgresql.log.2018-12-24-23
error/postgresql.log.2018-12-25-23
error/postgresql.log.2018-12-26-23

いわゆるシェル芸と呼ばれる能力をもっと高める必要があるのかもしれませんね。