将 bash 数组值与脚本中定义的字符串进行比较时出现问题

将 bash 数组值与脚本中定义的字符串进行比较时出现问题

我不太擅长编写脚本,但我一直在努力并尝试在不同线程上找到的每种变体。
我正在重写一些为虚拟主机备份/恢复创建的旧脚本,并尝试为脚本使用单个配置文件(blah.cfg),无论如何,大多数都使用相同的变量。

设置.cfg

enabled=yes
password=thisisastring

脚本1.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
#############################################################################

我尝试的任何内容都与comparepass(在脚本中设置)和密码(在cfg文件中设置)匹配,但是如果我将它们回显到输出,它们是相同的。

1: thisisastring
2: thisisastring
-----------------------------------------------------------------------
does not match: thisisastring vs thisisastring
-----------------------------------------------------------------------

根据我读过的线程,我尝试了不同的变体:

if [ "$comparepass" == ${config[password]} ];
if [ $comparepass == "${config[password]}" ];
if [ "$comparepass" == "${config[password]}" ];
if [[ $comparepass == ${config[password]} ]];    #this is a binary check?

我也尝试将数组分配给常规变量

compareagainst=${config[password]}
    if [ $comparepass == $compareagainst ]; then
        echo matches:  $comparepass vs $compareagainst
    else
        echo does not match:  $comparepass vs $compareagainst
    fi

得到相同的结果:

-----------------------------------------------------------------------
does not match: thisisastring vs thisisastring
-----------------------------------------------------------------------

我很困惑..离开代码世界太久了我只是不记得了..

dev@sandbox:~/backup-tools$ bash --version GNU bash,版本 5.1.16(1)-release (x86_64-pc-linux-gnu)

答案1

确保您的settings.cfg文件末尾不包含回车符(通常是这种情况)

修复settings.cfg文件运行

dos2unix settings.cfg

脚本重构:

#!/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

另外我建议不要在脚本中添加扩展名。只要命名它script

settings.cfg这是将 init 格式化文件存储到 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

相关内容