shell中使用sed正则修改文件

  • sed 使用正则匹配
  • 在shell中 当变量有空格的时候使用双引号包围 “${var}”
function change_version_class() {
    VERSION_CODE_FILE=$1
    BUILD_VERSION=$2

    if [ -f "${VERSION_CODE_FILE}" ]; then
        echo 'change '"${VERSION_CODE_FILE}"' info'

        BUILD_TIME=$(date +%Y%m%d-%H:%M:%S)
        BUILD_USER=$(whoami)
        BUILD_OS=$(uname -snr)
        BUILD_BRANCH=$(git branch |grep '[*]'|awk '{print $2}')
        BUILD_COMMIT=$(git log --oneline |head -n 1|awk '{print $1}')

        sed  -i 's/String BUILD_VERSION =\(.\)\+/String BUILD_VERSION = "'"${BUILD_VERSION}"'";/g'  "${VERSION_CODE_FILE}"
        sed  -i 's/String BUILD_TIME =\(.\)\+/String BUILD_TIME = "'"${BUILD_TIME}"'";/g'  "${VERSION_CODE_FILE}"
        sed  -i 's/String BUILD_USER =\(.\)\+/String BUILD_USER = "'"${BUILD_USER}"'";/g'  "${VERSION_CODE_FILE}"
        sed  -i 's/String BUILD_OS =\(.\)\+/String BUILD_OS = "'"${BUILD_OS}"'";/g'  "${VERSION_CODE_FILE}"
        sed  -i 's/String BUILD_BRANCH =\(.\)\+/String BUILD_BRANCH = "'"${BUILD_BRANCH}"'";/g'  "${VERSION_CODE_FILE}"
        sed  -i 's/String BUILD_COMMIT =\(.\)\+/String BUILD_COMMIT = "'"${BUILD_COMMIT}"'";/g'  "${VERSION_CODE_FILE}"
    fi
}

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.