I'm not really great at scripting but I've been fighting and trying every variation I can find on different threads.
I'm rewriting some old scripts I created for backup/restore of virtualhosts and attempting to use a single config file (blah.cfg) for the scripts, most use the same variables anyway..
settings.cfg
enabled=yes
password=thisisastring
script1.sh
comparepass=thisisastring
##############################################################
typeset -A config # init array
config=()
while read line
do
if echo $line | grep -F = &>/dev/null
then
varname=$(echo "$line" | cut -d '=' -f 1)
config[$varname]=$(echo "$line" | cut -d '=' -f 2-)
fi
done < settings.cfg
clear
echo 1: $comparepass
echo 2: ${config[password]}
echo -----------------------------------------------------------------------
if [ $comparepass == "${config[password]}" ]; then
echo matches: $comparepass vs ${config[password]}
else
echo does not match: $comparepass vs ${config[password]}
fi
echo -----------------------------------------------------------------------
exit
#############################################################################
Nothing I try matches comparepass (set in the script) and password (set in the cfg file), but if I echo them to output, they are the same..
1: thisisastring
2: thisisastring
-----------------------------------------------------------------------
does not match: thisisastring vs thisisastring
-----------------------------------------------------------------------
I've tried different variations depending on what thread I've read:
if [ "$comparepass" == ${config[password]} ];
if [ $comparepass == "${config[password]}" ];
if [ "$comparepass" == "${config[password]}" ];
if [[ $comparepass == ${config[password]} ]]; #this is a binary check?
I also tried tried assigning the array into a regular variable
compareagainst=${config[password]}
if [ $comparepass == $compareagainst ]; then
echo matches: $comparepass vs $compareagainst
else
echo does not match: $comparepass vs $compareagainst
fi
with the same result:
-----------------------------------------------------------------------
does not match: thisisastring vs thisisastring
-----------------------------------------------------------------------
I'm confused.. out of the code world for so long I just don't remember anymore..
dev@sandbox:~/backup-tools$ bash --version GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-gnu)
sed -n l settings.cfg
?