LoginSignup
18
19

More than 5 years have passed since last update.

管理しやすいシェル設定ファイルの設計例

Last updated at Posted at 2015-06-21

単純に変数を羅列する設定ファイルだと1設定1ファイルとせざるを得ず、結果、ファイル数が膨大になって管理が大変になる。以下のようなファイル構成とすることで、管理しやすく、しかも結合度の低い設定ファイルとすることができる。
なお、設計者は元同僚。

設計例

sample.conf
#No profile command argument
1   profile1    command.exe arg1
2   profile1    command.exe arg2
3   profile1    command.exe arg3
4   profile2    command.exe arg4
5   profile2    command.exe arg5
6   profile3    command.exe arg6
profile1
LANG=C
profile2
LANG=jajs_SJIS
profile3
LANG=C
PATH=$PATH:/home/sampleuser
sample.sh
inputf=$1
No=$2
while read line
do
    # コメント行と空行を無視する
    if echo $line | grep -e '^#' -e '^$' >/dev/null; then
        continue
    fi

    # 行指定した場合に、指定行以外を無視する
    if [ -n $No -a "`echo $line | awk '{print $1}'`" != $No ]; then
        continue
    fi

    # 以下処理行

    profile=`echo $line | awk '{print $2}'`
    command=`echo $line | awk '{print $3}'`
    argument=`echo $line | awk '{print $4}'`

    . $profile
    $command $argument

done < ${inputf}
実行方法
# ./sample.sh sample.conf [1-3]
第二引数(No)を指定するとその行のみを実行する。指定しないと全行を実行する。

複数のソフトウェアの環境変数をまとめて設定する場合に使用する。具体的には、profileに共通設定を、sample.confの各行に個別設定を記載する。
自分の保守するシステムの一部シェルでこの仕組みを使っているが、設定値の追加・変更・削除がとてもしやすい。

なお、エッセンスを伝えることが目的の投稿のためコードは厳密ではありません。あしからず。

18
19
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
19