diff options
author | Priit Liivak <priitliivak@gmail.com> | 2016-05-23 13:12:47 +0300 |
---|---|---|
committer | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2016-05-23 16:43:34 +0200 |
commit | 047e954bd5ea2699776ac982e0570fc301c83c18 (patch) | |
tree | 22a46052aeb791991be578dcfc5d116359231255 /scripts | |
parent | 5edd6db5e0e233ac77cda7e7ef5c254b2eccd875 (diff) | |
download | sonarqube-047e954bd5ea2699776ac982e0570fc301c83c18.tar.gz sonarqube-047e954bd5ea2699776ac982e0570fc301c83c18.zip |
[script] make property_utils.sh run on OSX + improve "if grep"
Now works on OSX: inline is required since in OSX providing '' as variable does not work.
More reliable OS independent version of this script.
Fixed incorrect backslash count in REGEX variable
Applied recommendations for "if grep" http://mywiki.wooledge.org/BashPitfalls#if_.5Bgrep_foo_myfile.5D
Diffstat (limited to 'scripts')
-rwxr-xr-x | scripts/property_utils.sh | 28 |
1 files changed, 15 insertions, 13 deletions
diff --git a/scripts/property_utils.sh b/scripts/property_utils.sh index e301bb47165..a58fb22b64e 100755 --- a/scripts/property_utils.sh +++ b/scripts/property_utils.sh @@ -8,12 +8,6 @@ set -euo pipefail -if [[ "$OSTYPE" == "darwin"* ]]; then - SED_DISABLE_BACKUP=" ''" -else - SED_DISABLE_BACKUP="" -fi - function cnt_lines() { FILE=$1 cat $1 | wc -l @@ -36,19 +30,27 @@ function set_property() { VALUE=$2 FILE=$3 - REGEXP="${1//\./\\\.}\s*=" - if [ $(grep $REGEXP $FILE | wc -l) -eq 0 ]; then - write_prop $1 $2 $3 - else - # delete line of specified property + REGEXP="${PROPERTY//\./\\.}\\s*=" + + if grep -q "$REGEXP" "$FILE"; then + # delete line of specified property LINE_COUNT=$(cnt_lines $FILE) - sed -i $SED_DISABLE_BACKUP "/${REGEXP}/d" $FILE + + if [[ "$OSTYPE" == "darwin"* ]]; then + sed -i '' /${REGEXP}/d "$FILE" + else + sed -i /${REGEXP}/d "$FILE" + fi # add property if at least one line deleted NEW_LINE_COUNT=$(cnt_lines $FILE) - if [ $LINE_COUNT -gt $NEW_LINE_COUNT ]; then + + if [[ $LINE_COUNT -gt $NEW_LINE_COUNT ]]; then write_prop $1 $2 $3 fi + + else + write_prop $1 $2 $3 fi } |