ゼットログ

よしなしごとを記す

CouchDBが起動するときのこと

CouchDBを読むにあたって、まずは起動プロセスから追っていくことにした。
例えば、こうやってCouchDBを起動させるときのこと。

$ sudo -i -u couchdb couchdb

/usr/local/bin/couchdbの中身を見てみるとシェルで枠内のように書いてある。
getoptっていう便利なものがあるのがわかった。コマンドを作るときの典型的なやり方なんでしょね。

parse_script_option_list () {
    set +e
    options=`getopt hVc:ibp:r:Ro:e:skd $@`
    if test ! $? -eq 0; then
        display_error
    fi
    set -e
    eval set -- $options
    while [ $# -gt 0 ]; do
        case "$1" in
            -h) shift; display_help; exit $SCRIPT_OK;;
            -V) shift; display_version; exit $SCRIPT_OK;;
            -c) shift; INI_FILES="$INI_FILES $1"; shift;;
            -i) shift; INTERACTIVE_BOOLEAN=true;;
            -b) shift; BACKGROUND_BOOLEAN=true;;
            -r) shift; RESPAWN_TIMEOUT=$1; shift;;
            -R) shift; RECURSED_BOOLEAN=true;;
            -p) shift; PID_FILE=$1; shift;;
            -o) shift; STDOUT_FILE=$1; shift;;
            -e) shift; STDERR_FILE=$1; shift;;
            -s) shift; check_status; exit $SCRIPT_OK;;
            -k) shift; KILL_BOOLEAN=true;;
            -d) shift; SHUTDOWN_BOOLEAN=true;;
            --) shift; break;;
            *) display_error "Unknown option: $1" >&2;;
        esac
    done
    if test "$KILL_BOOLEAN" = "true" -o "$SHUTDOWN_BOOLEAN" = "true"; then
        stop_couchdb $KILL_BOOLEAN
    else
        start_couchdb
    fi
}