shell修改配置文件参数
#! /usr/bin/sh # Author: ELIATEN help_info="The script is used to update sdk version in the sdkomatic configuration files which locate in relative directory '../sdkomatic/'.\n Usage: -a add sdk version, -d delete sdk version\n Example: ./updateVersion.sh -a 18.2\n Notes: the sdkomatic cfg file should be located in relative directory '../sdkomatic/' of current script and the valid cfg files' name should start with 'sdkomatic', end with '.cfg', without 'REL'." check_files() { if [[ -z $1 ]] then echo 'Did not find any cfg file matches to "ls ../sdkomatic*.cfg|grep -v REL", exit! Please make sure the cfg files are in relative directory "../sdkomatic/".' exit fi } add_version() { # function used to add version to sdkomatic configuration files # 查找文件, 即使找不到也忽略报错 files=`(ls ../sdkomatic/sdkomatic*.cfg|grep -v REL) 2>/dev/null` check_files $files for file in $files do # 获得sdk_versions的值
versions=`sed '/sdk_versions" :/!d; s/.*://' $file` # delete the space at begging 去掉字符串首的空格 versions=$(echo $versions|sed 's/^\\s//') if [[ $versions =~ $1 ]] then echo "no change to "$file else # add '[' at beginning and delete '],' at last of string 为了后面方便替换在前面添加\,去掉最后的] versions="\\"${versions/\]*/} # add new version new_versions="$versions, $1" # 替换 sed -i "s/$versions/$new_versions/g" $file if [[ $? -eq 0 ]] then echo "add new version "$1" to "$file" successful" else echo "add new version "$1" to "$file" filed" fi fi done } delete_version() { # function used to delete version from sdkomaitc configuration files files=`(ls ../sdkomatic/sdkomatic*.cfg|grep -v REL) 2>/dev/null` for file in $files do versions=`sed '/sdk_versions" :/!d; s/.*://' $file` versions=$(echo $versions|sed 's/^\\s//') if [[ $versions =~ $1 ]] then
# 为了后面方便替换在前面添加\,去掉最后的]
versions="\\"${versions/\]*/} # delete the version from version list new_versions=$(echo $versions|sed "s/$1",*""\\s*"//g") # delete the legacy comma at last if have new_versions=$(echo $new_versions|sed 's/,$//g') sed -i "s/$versions/$new_versions/g" $file if [[ $? -eq 0 ]] then echo "delete version "$1" from "$file" successful" else echo "delete version "$1" from "$file" filed" fi else echo "no change to "$file fi done } while getopts "a:d:h" opt; do case $opt in a) echo "receive request to add new version: "$OPTARG add_version $OPTARG ;; d) echo "receive request to delete version: "$OPTARG delete_version $OPTARG ;; h) echo -e $help_info ;; \?) # echo "Invalid option: $OPTARG" echo -e $help_info ;; esac done
文件内容:
release_definitions:{"sdk_17_3_sdkomatic" : {
"product" : "EPG_MASTER_25",
"sdk_versions" : [17.3],
"artifactory_branch" : "REL_173",
"build_definitions" : "bd_ssr_17_3",
"test_definitions" : {
"default" : "td_pduip_17_3",
"drop" : "td_drop_17_3"
}
}
}