Skip to content

Commit 3430507

Browse files
authored
v3.0 - improve readIniFile() (#1128)
- grep -c equals grep | wc -l - make output prettier - work with variable instead of temporary file + a few minor changes/cleanup
1 parent 844f3ce commit 3430507

File tree

1 file changed

+21
-30
lines changed

1 file changed

+21
-30
lines changed

updater.sh

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## arkenfox user.js updater for macOS and Linux
44

5-
## version: 2.9
5+
## version: 3.0
66
## Author: Pat Johnson (@overdodactyl)
77
## Additional contributors: @earthlng, @ema-pe, @claustromaniac
88

@@ -103,7 +103,6 @@ Optional Arguments:
103103
# File Handling #
104104
#########################
105105

106-
# Download files
107106
download_file () { # expects URL as argument ($1)
108107
declare -r tf=$(mktemp)
109108

@@ -122,36 +121,33 @@ open_file () { # expects one argument: file_path
122121

123122
readIniFile () { # expects one argument: absolute path of profiles.ini
124123
declare -r inifile="$1"
125-
declare -r tfile=$(mktemp)
126124

127-
if [ $(grep '^\[Profile' "$inifile" | wc -l) == "1" ]; then ### only 1 profile found
128-
grep '^\[Profile' -A 4 "$inifile" | grep -v '^\[Profile' > $tfile
125+
# tempIni will contain: [ProfileX], Name=, IsRelative= and Path= (and Default= if present) of the only (if) or the selected (else) profile
126+
if [ $(grep -c '^\[Profile' "${inifile}") -eq "1" ]; then ### only 1 profile found
127+
tempIni="$(grep '^\[Profile' -A 4 "${inifile}")"
129128
else
130-
grep -E -v '^\[General\]|^StartWithLastProfile=|^IsRelative=' "$inifile"
131-
echo ''
129+
echo -e "Profiles found:\n––––––––––––––––––––––––––––––"
130+
## cmd-substitution to strip trailing newlines and in quotes to keep internal ones:
131+
echo "$(grep --color=never -E 'Default=[^1]|\[Profile[0-9]*\]|Name=|Path=|^$' "${inifile}")"
132+
echo '––––––––––––––––––––––––––––––'
132133
read -p 'Select the profile number ( 0 for Profile0, 1 for Profile1, etc ) : ' -r
133134
echo -e "\n"
134135
if [[ $REPLY =~ ^(0|[1-9][0-9]*)$ ]]; then
135-
grep '^\[Profile'${REPLY} -A 4 "$inifile" | grep -v '^\[Profile'${REPLY} > $tfile
136-
if [[ "$?" != "0" ]]; then
137-
echo "Profile${REPLY} does not exist!" && exit 1
138-
fi
136+
tempIni="$(grep "^\[Profile${REPLY}" -A 4 "${inifile}")" || {
137+
echo -e "${RED}Profile${REPLY} does not exist!${NC}" && exit 1
138+
}
139139
else
140-
echo "Invalid selection!" && exit 1
140+
echo -e "${RED}Invalid selection!${NC}" && exit 1
141141
fi
142142
fi
143143

144-
declare -r profpath=$(grep '^Path=' $tfile)
145-
declare -r pathisrel=$(grep '^IsRelative=' $tfile)
144+
# extracting 0 or 1 from the "IsRelative=" line
145+
declare -r pathisrel=$(sed -n 's/^IsRelative=\([01]\)$/\1/p' <<< "${tempIni}")
146146

147-
rm "$tfile"
148-
149-
# update global variable
150-
if [[ ${pathisrel#*=} == "1" ]]; then
151-
PROFILE_PATH="$(dirname "$inifile")/${profpath#*=}"
152-
else
153-
PROFILE_PATH="${profpath#*=}"
154-
fi
147+
# extracting only the path itself, excluding "Path="
148+
PROFILE_PATH=$(sed -n 's/^Path=\(.*\)$/\1/p' <<< "${tempIni}")
149+
# update global variable if path is relative
150+
[[ ${pathisrel} == "1" ]] && PROFILE_PATH="$(dirname "${inifile}")/${PROFILE_PATH}"
155151
}
156152

157153
getProfilePath () {
@@ -161,16 +157,14 @@ getProfilePath () {
161157
if [ "$PROFILE_PATH" = false ]; then
162158
PROFILE_PATH="$SCRIPT_DIR"
163159
elif [ "$PROFILE_PATH" = 'list' ]; then
164-
local ini=''
165160
if [[ -f "$f1" ]]; then
166-
ini="$f1"
161+
readIniFile "$f1" # updates PROFILE_PATH or exits on error
167162
elif [[ -f "$f2" ]]; then
168-
ini="$f2"
163+
readIniFile "$f2"
169164
else
170165
echo -e "${RED}Error: Sorry, -l is not supported for your OS${NC}"
171166
exit 1
172167
fi
173-
readIniFile "$ini" # updates PROFILE_PATH or exits on error
174168
#else
175169
# PROFILE_PATH already set by user with -p
176170
fi
@@ -191,9 +185,7 @@ get_updater_version () {
191185
# -d: New version will not be looked for and update will not occur
192186
# -u: Check for update, if available, execute without asking
193187
update_updater () {
194-
if [ $UPDATE = 'no' ]; then
195-
return 0 # User signified not to check for updates
196-
fi
188+
[ $UPDATE = 'no' ] && return 0 # User signified not to check for updates
197189

198190
declare -r tmpfile="$(download_file 'https://raw.githubusercontent.com/arkenfox/user.js/master/updater.sh')"
199191
[ -z "${tmpfile}" ] && echo -e "${RED}Error! Could not download updater.sh${NC}" && return 1 # check if download failed
@@ -214,7 +206,6 @@ update_updater () {
214206
exit 0
215207
}
216208

217-
218209
#########################
219210
# Update user.js #
220211
#########################

0 commit comments

Comments
 (0)