1

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)

3
  • I might just have an idea. Can you show the output of sed -n l settings.cfg ? Commented May 4, 2023 at 18:45
  • oh yea! i didn't think about that.. /r's everywhere it appears. Actually.. stuff all over the place. sed -n l settings.cnf enabled=yes\r$ password=thisisastring\r$ #####################################################################\ ##\r$ #\r$ # base location where your virtual hosts reside\r$ # example 1: you have only one host on your server and the Directory\ structure is:\r$ #\r$ # \342\224\224\342\224\200\342\224\200 var\r$ # \342\224\224\342\224\200\342\224\200 www\r$ #\t\t\342\224\224\342\224\200\342\224\200 html\r$
    – Dennis
    Commented May 4, 2023 at 18:51
  • Great, so we are making progress. I've also refactored your script a bit to make it less complex. See answer: Commented May 4, 2023 at 19:00

1 Answer 1

0

Make sure your settings.cfg file doesn't contain carriage return characters at the end (which is usually the case)

To fix the settings.cfg file run

dos2unix settings.cfg

script refactored:

#!/usr/bin/env bash

comparepass=thisisastring
##############################################################
while IFS== read -r _ value || [[ -n $line ]]
do
  val="$value" 
done < settings.cfg
printf 'pass from config:\t%s\n'  "$val"
printf 'pass from script:\t%s\n'  "$comparepass"
echo -----------------------------------------------------------------------
if [[ $val == $comparepass ]]; then
    printf '%s and %s match\n' "$val" "$comparepass"
else
    printf '%s and %s do not match\n' "$val" "$comparepass"
fi
echo -----------------------------------------------------------------------
exit

Also I'd advise not to put an extension to your script. Just name it script

Here is the piece where you store the init formatted settings.cfg file into an "associative" array in bash

# Declare the associative array
declare -A settings

while IFS='=' read -r key value; do
    # Strip leading/trailing whitespace from the key and value in case there are any
    key="${key// /}"
    value="${value// /}"
    settings[$key]="$value"
done < settings.cfg

# Loop through the keys and print the values of the array
for key in "${!settings[@]}"; do
    echo "$key = ${settings[$key]}"
done
4
  • 1
    excellent, thank you for the help.. I used sed -i -e 's/\r$//' settings.cfg and it appears to have worked as well.. I will read on the differences between dos2unix . For the refactoring, it doesn't appear to be creating an array? I will be using 20 other variables from the cfg across a few different scripts.
    – Dennis
    Commented May 4, 2023 at 19:08
  • can you explain why not use .sh on the end? sorry for the rudimentary questions.. i thought extensionless indicated a binary file, not a script?
    – Dennis
    Commented May 4, 2023 at 19:09
  • 1
    There are a few reasons why not to use .sh extension. One being convention. See google.github.io/styleguide/shellguide.html . Furthermore, .sh extension might confuse users and in some cases it breaks bash functionality if the script is written in bash. See also mywiki.wooledge.org/FullBashGuide which explains not to use .sh extension Commented May 4, 2023 at 19:24
  • 1
    Ok, in that case, see my update Commented May 7, 2023 at 20:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .