v3.0 - improve readIniFile()

- grep -c equals grep | wc -l
- make output prettier
- work with variable instead of temporary file
This commit is contained in:
earthlng 2021-03-01 16:35:44 +00:00 committed by GitHub
parent 5f9bb59b95
commit a1d26c006f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2,7 +2,7 @@
## arkenfox user.js updater for macOS and Linux ## arkenfox user.js updater for macOS and Linux
## version: 2.9 ## version: 3.0
## Author: Pat Johnson (@overdodactyl) ## Author: Pat Johnson (@overdodactyl)
## Additional contributors: @earthlng, @ema-pe, @claustromaniac ## Additional contributors: @earthlng, @ema-pe, @claustromaniac
@ -122,36 +122,32 @@ open_file () { # expects one argument: file_path
readIniFile () { # expects one argument: absolute path of profiles.ini readIniFile () { # expects one argument: absolute path of profiles.ini
declare -r inifile="$1" declare -r inifile="$1"
declare -r tfile=$(mktemp)
if [ $(grep '^\[Profile' "$inifile" | wc -l) == "1" ]; then ### only 1 profile found # tempIni will contain: [ProfileX], Name=, IsRelative= and Path= (and Default= if present) of the only (if) or the selected (else) profile
grep '^\[Profile' -A 4 "$inifile" | grep -v '^\[Profile' > $tfile if [ $(grep -c '^\[Profile' "${inifile}") == "1" ]; then ### only 1 profile found
tempIni="$(grep '^\[Profile' -A 4 "${inifile}")"
else else
grep -E -v '^\[General\]|^StartWithLastProfile=|^IsRelative=' "$inifile" echo -e "Profiles found:\n"
echo '' ## cmd-substitution to strip trailing newlines and in quotes to keep internal ones:
read -p 'Select the profile number ( 0 for Profile0, 1 for Profile1, etc ) : ' -r echo "$(grep --color=never -E 'Default=[^1]|\[Profile[0-9]*\]|Name=|Path=|^$' "${inifile}")"
echo; read -p 'Select the profile number ( 0 for Profile0, 1 for Profile1, etc ) : ' -r
echo -e "\n" echo -e "\n"
if [[ $REPLY =~ ^(0|[1-9][0-9]*)$ ]]; then if [[ $REPLY =~ ^(0|[1-9][0-9]*)$ ]]; then
grep '^\[Profile'${REPLY} -A 4 "$inifile" | grep -v '^\[Profile'${REPLY} > $tfile tempIni="$(grep "^\[Profile${REPLY}" -A 4 "${inifile}")" || {
if [[ "$?" != "0" ]]; then echo -e "${RED}Profile${REPLY} does not exist!${NC}" && exit 1
echo "Profile${REPLY} does not exist!" && exit 1 }
fi
else else
echo "Invalid selection!" && exit 1 echo -e "${RED}Invalid selection!${NC}" && exit 1
fi fi
fi fi
declare -r profpath=$(grep '^Path=' $tfile) # extracting 0 or 1 from the "IsRelative=" line
declare -r pathisrel=$(grep '^IsRelative=' $tfile) declare -r pathisrel=$(sed -n 's/^IsRelative=\([01]\)$/\1/p' <<< "${tempIni}")
rm "$tfile" # extracting only the path itself, excluding "Path="
PROFILE_PATH=$(sed -n 's/^Path=\(.*\)$/\1/p' <<< "${tempIni}")
# update global variable # update global variable if path is relative
if [[ ${pathisrel#*=} == "1" ]]; then [[ ${pathisrel} == "1" ]] && PROFILE_PATH="$(dirname "${inifile}")/${PROFILE_PATH}"
PROFILE_PATH="$(dirname "$inifile")/${profpath#*=}"
else
PROFILE_PATH="${profpath#*=}"
fi
} }
getProfilePath () { getProfilePath () {