0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

自動ID取得

Last updated at Posted at 2025-05-03
#!/bin/bash

###===============================
### 1. 変数定義
###===============================

LIST_FILE="$1"
ORACLE_USER="sysmngra"
ORACLE_CMD="sqlplus -s / as sysdba"

###===============================
### 2. 関数定義
###===============================

# hh:mm:ss [user@host ~]# を表示
prompt() {
    local now
    now=$(date +%H:%M:%S)
    echo "${now} [$(whoami)@$(hostname) ~]#"
}

# Oracle SQL を su 経由で1つずつ実行(空行・コメント行スキップ)
run_oracle_sql() {
    while IFS= read -r line; do
        [[ -z "$line" || "$line" =~ ^# ]] && continue

        sql="${line%;}"

        for _ in {1..3}; do
            prompt
        done

        echo "$(prompt) echo \"${sql};\" | su - ${ORACLE_USER} -c \"${ORACLE_CMD}\""

        echo "
SET MARKUP CSV ON
${sql};
EXIT
" | su - "${ORACLE_USER}" -c "${ORACLE_CMD}"

    done < "${LIST_FILE}"
}

# RHELコマンドを1つずつ実行(空行・コメント行スキップ)
run_rhel_commands() {
    while IFS= read -r line; do
        [[ -z "$line" || "$line" =~ ^# ]] && continue

        for _ in {1..3}; do
            prompt
        done

        echo "$(prompt) ${line}"
        bash -c "${line}"
    done < "${LIST_FILE}"
}

###===============================
### 3. メイン処理
###===============================

main() {
    if [[ $# -ne 1 ]]; then
        echo "使い方: $0 <コマンドリストファイル(.lst)>"
        exit 1
    fi

    if [[ ! -f "$LIST_FILE" ]]; then
        echo "エラー: ファイルが存在しません → $LIST_FILE"
        exit 1
    fi

    local MODE=""
    case "$LIST_FILE" in
        *Oracle*|*oracle*)
            MODE="oracle"
            ;;
        *RHEL*|*rhel*)
            MODE="rhel"
            ;;
        *)
            echo "不明なモード: ファイル名に 'Oracle' または 'RHEL' を含めてください"
            exit 2
            ;;
    esac

    case "$MODE" in
        oracle)
            run_oracle_sql
            ;;
        rhel)
            run_rhel_commands
            ;;
    esac
}

main "$@"
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?