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?

プロパティ一気に追記したいときの自分用シェルメモ

Last updated at Posted at 2024-11-21
#!/bin/bash

# プロパティファイルが格納されているディレクトリ
TARGET_DIR="./config"

# 処理対象のファイルを探す
find "$TARGET_DIR" -type f -name "application.properties" | while read -r FILE; do
  # "parameter.example" が含まれていない場合はスキップ
  if ! grep -q "parameter.example" "$FILE"; then
    echo "Skipping $FILE (no 'parameter.example' found)"
    continue
  fi

  # 1. ファイル末尾に以下を追加
  echo -e "\n## hoge\nparameter=11" >> "$FILE"
  echo "Appended base parameters to $FILE"

  # 2. "parameter.example.****=someparameter_1abc" の行を探す
  grep -E "^parameter\.example\.[a-zA-Z]+=" "$FILE" | while read -r LINE; do
    # "parameter.example.bk.****=someparameter_2abc" を追加
    NEW_LINE=$(echo "$LINE" | sed 's/parameter\.example\./parameter.example.bk./')
    echo "$NEW_LINE" >> "$FILE"
    echo "Added backup line to $FILE: $NEW_LINE"
  done
done

echo "Processing complete."

#!/bin/bash

# プロジェクトディレクトリを探索する親ディレクトリを指定
BASE_DIR="."

# 各プロジェクトディレクトリをループ
for PROJECT_DIR in "$BASE_DIR"/*; do
    # ディレクトリでなければスキップ
    if [ ! -d "$PROJECT_DIR" ]; then
        continue
    fi

    # env/test ディレクトリが存在するか確認
    TEST_DIR="$PROJECT_DIR/env/test"
    if [ -d "$TEST_DIR" ]; then
        echo "Processing $PROJECT_DIR"

        # env/prd ディレクトリを作成(既に存在する場合はスキップ)
        PRD_DIR="$PROJECT_DIR/env/prd"
        mkdir -p "$PRD_DIR"

        # env/test の中身を env/prd にコピー
        cp -r "$TEST_DIR/"* "$PRD_DIR"

        echo "Copied contents from $TEST_DIR to $PRD_DIR"
    else
        echo "Skipping $PROJECT_DIR (env/test not found)"
    fi
done

pause

echo "CopyEnd"

# 置換対象の文字列と置換後の文字列を指定
declare -A replacements=(
    ["hogeTest"]="hogePrd"
    ["exampleKey"]="exampleValue"  # 必要に応じて追加
)

# 各プロジェクトディレクトリをループ
for PROJECT_DIR in "$BASE_DIR"/*; do
    # ディレクトリでなければスキップ
    if [ ! -d "$PROJECT_DIR" ]; then
        continue
    fi

    # env/prd/hoge.json ファイルのパスを確認
    HOGE_JSON="$PROJECT_DIR/env/prd/hoge.json"
    if [ -f "$HOGE_JSON" ]; then
        echo "Processing $HOGE_JSON"

        # 置換を実行
        for search in "${!replacements[@]}"; do
            replace=${replacements[$search]}
            echo "Replacing '$search' with '$replace' in $HOGE_JSON"
            sed -i "s/$search/$replace/g" "$HOGE_JSON"
        done
    else
        echo "Skipping $PROJECT_DIR (env/prd/hoge.json not found)"
    fi
done

echo "Script execution completed."


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?