-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlistKeyMod.sh
More file actions
147 lines (137 loc) · 3.05 KB
/
PlistKeyMod.sh
File metadata and controls
147 lines (137 loc) · 3.05 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/bin/bash
# License: The MIT License (MIT)
# Author Zuzzuc https://github.com/Zuzzuc/
if [ "$(caller 0)" != "" ];then
exit_mode="return"
else
exit_mode="exit"
fi
print_usage(){
echo -e "Usage is PlistKeyMod.sh -[fkmv]=[value] -[h]"
}
catch_err(){
# Input is $1, where $1 is the error code description to display.
if [ -z "$2" ];then
case "$1" in
1)
echo "($1) No file supplied."
;;
2)
echo "($1) Specified key, '$key', was not found in the file."
;;
3)
echo "($1) File '$file' does not exist."
;;
4)
echo "($1) Mode '$mode' is not supported."
;;
5)
echo "($1) File recived is not a plist."
;;
6)
echo "Unknown argument supplied. Failing arg is '$3'"
;;
*)
echo "Unknown error code. No default message to display."
;;
esac
else
echo "$2"
fi
error_code=$1
}
# Setup default vars
file=""
mode="read"
key=""
value=""
output="auto"
# Handle input
if [ "$*" != "" ];then
for i in "$@";do
case "$i" in
"$0")
continue
;;
-f=*|--file=*)
f="${i#*=}" && f="${f/\\/}" && f="${f%${f##*[![:space:]]}}"
if [ "${f:${#f}-6}" == ".plist" ];then
file="$f"
else
# File is not a plist.
catch_err "5" && $exit_mode $error_code
fi
;;
-k=*|--key=*)
key="${i#*=}"
;;
-m=*|--mode=*)
mode="${i#*=}"
;;
-v=*|--value=*)
value="${i#*=}"
;;
-o=*|--output=*)
output="${i#*=}"
;;
-h|--help)
# Print help.
print_usage
;;
*)
# Unknown argument.
catch_err "6" "" "$i" && $exit_mode $error_code
;;
esac
done
else
# User sent no arguments.
catch_err "1" && $exit_mode $error_code
fi
if [ "$output" != "STDIN" ] && [ "$output" != "auto" ];then
output="${output/\\/}" && output="${output%${output##*[![:space:]]}}"
mkdir -p "${output%/*}"
fi
if [ "$mode" == "read" ] || [ -z "$mode" ];then
if [ ! -z "$file" ];then
file="${file/\\/}" && file="${file%${file##*[![:space:]]}}"
if [ -f "$file" ];then
r="$(awk "/$key/{getline; print}" "$file")" && r="${r#"${r%%[![:blank:]]*}"}"
if [ "$output" == "auto" ] || [ "$output" == "STDIN" ];then
if [ "$r" != "" ];then
echo "$r"
else
catch_err "2" && $exit_mode $error_code
fi
else
if [ "$r" != "" ];then
echo "$r" > "$output"
else
catch_err "2" && $exit_mode $error_code
fi
fi
else
# File does not exist
catch_err "3" && $exit_mode $error_code
fi
else
# No filepath supplied.
catch_err "1" && $exit_mode $error_code
fi
elif [ "$mode" == "write" ];then
if line="$(grep -n "$key" "$file")";then
linenr="$((${line/:*/}+1))"
line="${line/*:/}"
if [ "$output" == "auto" ] && [ "$output" != "STDIN" ];then
sed -i '' -e "$linenr s%>.*</%>$value</%" "$file"
else
sed -e "$linenr s%>.*</%>$value</%" "$file"
fi
else
# Didnt find key.
catch_err "2" && $exit_mode $error_code
fi
else
# If mode isnt supported.
catch_err "4" && $exit_mode $error_code
fi