-
Notifications
You must be signed in to change notification settings - Fork 0
/
prop.sh
executable file
·40 lines (34 loc) · 935 Bytes
/
prop.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/bin/bash
# gets a value from a key in a properties file
MYSELF="$(readlink -f "$0")"
MYDIR="${MYSELF%/*}"
ME=$(basename $MYSELF)
source $MYDIR/env
[[ -f $LOCAL_ENV ]] && source $LOCAL_ENV
source $MYDIR/log.sh
function prop() {
filename="$1"
thekey="$2"
newvalue="$3"
if [[ ! -n "$newvalue" ]]; then
debug "GETTING '${thekey}'"
sed -rn "s/^${thekey}=([^\n]+)$/\1/p" $filename
exit 0
fi
if [ ! -f "$filename" ]; then
debug "creating config file $filename"
mkdir -p "$(dirname $filename)"
touch "$filename"
fi
if ! grep -R "^[#]*\s*${thekey}=.*" $filename > /dev/null; then
debug "APPENDING '${thekey}'"
echo "$thekey=$newvalue" >> $filename
else
debug "SETTING '${thekey}'"
if [[ "$newvalue" == */* ]]; then
newvalue="${newvalue//\//\\\/}"
debug "value escaped as: $newvalue"
fi
sed -ir "s/^[#]*\s*${thekey}=.*/$thekey=$newvalue/" $filename
fi
}