66 lines
2.7 KiB
Bash
Executable File
66 lines
2.7 KiB
Bash
Executable File
#!/bin/bash
|
|
set -e
|
|
set -x
|
|
CONFIG_FILE="${2}"
|
|
#arr+=( ["blend_name"]="devuan-live" \
|
|
# ["blend_vers"]="2.0" \
|
|
# ["mirror"]="http://192.168.44.47:3142/pkgmaster.devuan.org/merged" \
|
|
# ["release"]="ascii" )
|
|
|
|
declare -A arr
|
|
#arr+=( "${1}" )
|
|
echo "edit-config.sh file: <"${2}">"
|
|
echo "edit-config.sh arr: <"${1}">"
|
|
eval "arr=(${1})"
|
|
|
|
for TARGET_KEY in "${!arr[@]}"; do
|
|
echo ${TARGET_KEY} ${arr[${TARGET_KEY}]} ${CONFIG_FILE}
|
|
if grep -q "^${TARGET_KEY}[+]*=" ${CONFIG_FILE}; then
|
|
# sed -i "s|^${TARGET_KEY}[+]*.*|${TARGET_KEY}=\"${arr[${TARGET_KEY}]}\"|" ${CONFIG_FILE}
|
|
|
|
# Replace multiline "${TARGET_KEY}=(...)" with "TARGET_KEY=(TARGET_VALUES)".
|
|
# Replace multiline "${TARGET_KEY}+=(...)" with "TARGET_KEY+=(TARGET_VALUES)".
|
|
sed -i -e "/${TARGET_KEY}\([+]*\)=(/{" \
|
|
-e " :loop;s/^${TARGET_KEY}\([+]\)*=(.*)/TARGET_KEY\1=(TARGET_VALUES)/;t;N;b loop" \
|
|
-e "}" \
|
|
${CONFIG_FILE}
|
|
# Replace "TARGET_KEY=(TARGET_VALUES)" with values from ${arr[]}.
|
|
# Works with "TARGET_KEY=(TARGET_VALUES)" and "TARGET_KEY+=(TARGET_VALUES)".
|
|
sed -i -e "s|^TARGET_KEY\([+]*\)=|${TARGET_KEY}\1=|;" \
|
|
-e "s|TARGET_VALUES|${arr[${TARGET_KEY}]}|" \
|
|
${CONFIG_FILE}
|
|
else
|
|
# $TARGET_KEY is new to ${CONFIG_FILE}
|
|
# Assume $TARGET_KEY is not used elsewhere, so use "=" not "+=".
|
|
sed -i "/^blend_name=.*/ s|.*|&\n${TARGET_KEY}=\"${arr[${TARGET_KEY}]}\"|" ${CONFIG_FILE}
|
|
fi
|
|
done
|
|
|
|
## sed -i "s|^${TARGET_KEY}.*|${TARGET_KEY}=\"${arr[${TARGET_KEY}]}\"|" ${CONFIG_FILE}
|
|
##
|
|
## sed -e "/${TARGET_KEY}[+]=(/{" -e ":loop;s/^${TARGET_KEY}=(.*)/(xTARGETx)/;t;N;b loop" -e "}" -e "s/^(xTARGETx)/${TARGET_KEY}=${arr[${TARGET_KEY}]}/"
|
|
## ${CONFIG_FILE}
|
|
##
|
|
## sed -e "/${TARGET_KEY}[+]=(/{" \
|
|
## -e ":loop;s/^${TARGET_KEY}=(.*)/(xTARGETx)/;t;N;b loop" \
|
|
## -e "}" \
|
|
## -e "s/^(xTARGETx)/${TARGET_KEY}=${arr[${TARGET_KEY}]}/"
|
|
##
|
|
## Works: +=
|
|
##
|
|
## cat config| sed -e "/${TARGET_KEY}\([+]*\)=(/{" -e " :loop;s/^${TARGET_KEY}+=(.*)/(xTARGETx)/;t;N;b loop" -e "}" -e "s/^(xTARGETx)/${TARGET_KEY}+=${arr[${TARGET_KEY}]}/"
|
|
##
|
|
## works: =
|
|
##
|
|
## cat config| sed -e "/${TARGET_KEY}\([+]*\)=(/{" -e " :loop;s/^${TARGET_KEY}=(.*)/(xTARGETx)/;t;N;b loop" -e "}" -e "s/^(xTARGETx)/${TARGET_KEY}+=${arr[${TARGET_KEY}]}/"
|
|
##
|
|
## works: both
|
|
##
|
|
## cat config| sed -e "/${TARGET_KEY}\([+]*\)=(/{" -e " :loop;s/^${TARGET_KEY}\([+]\)*=(.*)/TARGET_KEY\1=(TARGET_VALUES)/;t;N;b loop" -e "}" | sed -e "s/^TARGET_KEY\([+]*\)=/${TARGET_KEY}\1=/; s/TARGET_VALUES/${arr[${TARGET_KEY}]}/"
|
|
##
|
|
## formatted:
|
|
##
|
|
## cat config| sed -e "/${TARGET_KEY}\([+]*\)=(/{"
|
|
## -e " :loop;s/^${TARGET_KEY}\([+]\)*=(.*)/TARGET_KEY\1=(TARGET_VALUES)/;t;N;b loop"
|
|
## -e "}" | sed -e "s/^TARGET_KEY\([+]*\)=/${TARGET_KEY}\1=/; s/TARGET_VALUES/${arr[${TARGET_KEY}]}/"
|