Bash:迭代二维属性文件时的 if 条件子句

Bash: if conditional clause while iterating over a 2-dimensional properties file

我有一个由 item[i] 定义的数据结构 -- 包含 --> subitem[k]。我想编写一个小的 bash 脚本来解析子项,并且项在命中特定子项时执行操作。

我的sampletext.properties文件是这样的:

Item1.subitem1=tom
Item1.subitem2=bob
Item2.subitem1=alice
Item2.subitem2=cindy

克服Bash中没有二维数组的问题,我写了两个函数,一个是获取键的值,一个是统计字符串出现的次数。

然后我构建了一个 for 循环来解析这些项​​目,我尝试包含一个 IF 子句来点击“bob”并说些什么。不幸的是,脚本没有看到 bob,而且 IF 子句似乎不起作用:

#!/bin/bash

# The input text file 
PROPERTY_FILE=sampletext.properties

#Defining a function to get the value of a key
function getProperty {
   PROP_KEY=
   PROP_VALUE=`cat $PROPERTY_FILE | grep "$PROP_KEY" | cut -d'=' -f2 | tr -d '\n'`
   echo $PROP_VALUE
}

# Counting the number of subitem1 entries
function countsubitem1 {
  toolsnumber=`cat $PROPERTY_FILE | grep "Item[0-9].subitem1" | wc -l`
  echo $toolsnumber
}
countsubitem1


for (( i=1; i<=$(countsubitem1); i++))
do
  subitem1=`getProperty "Item$i.subitem1"`
  subitem2=`getProperty "Item$i.subitem2"`
  echo item$i:$(getProperty "Item$i.subitem1") # Testing the getProperty function again
  echo subitem2:$subitem2 # Testing the for loop and the getProperty function
  if [[ "$subitem2" == "bob" ]]; then
    echo "I see bob!"
  else
    echo "No bob here!"
  fi  
done

我启动脚本的结果:

2
item1:tom
subitem2:bob
No bob here!
item2:alice
subitem2:cindy
No bob here!

我的期望:

./samplebash.sh
2
item1:tom
subitem2:bob
I see bob!
item2:alice
subitem2:cindy
No bob here!

我尝试了 IF 子句的几种更改:

 if [ "$subitem2" == "bob" ]; then
  if [ '$subitem2' == 'bob' ]; then

但我总是收到 'No bob here!' 消息。

感谢@William Pursell 的提示!

添加 | tr -d '\r' 清理字符串并正确解析。

我现在获得:

item1subitem1:tom
subitem2:bob
I see bob!
item2subitem2:alice
subitem2:cindy
No bob here!

使用代码:

#!/bin/bash

# The input text file 
PROPERTY_FILE=sampletext.properties

#Defining a function to get the value of a key
function getProperty {
   PROP_KEY=
   PROP_VALUE=`cat $PROPERTY_FILE | grep "$PROP_KEY" | cut -d'=' -f2 | tr -d '\n' | tr -d '\r'`
   echo $PROP_VALUE
}

# Counting the number of subitem1 entries
function countsubitem1 {
  toolsnumber=`cat $PROPERTY_FILE | grep "Item[0-9].subitem1" | wc -l`
  echo $toolsnumber
}


for (( i=1; i<=$(countsubitem1); i++))
do
  subitem1=`getProperty "Item$i.subitem1"`
  subitem2=`getProperty "Item$i.subitem2"`
  echo item"$i"subitem$i:$(getProperty "Item$i.subitem1") # Testing the getProperty function again
  echo subitem2:$subitem2 # Testing the for loop and the getProperty function
  if [[ "$subitem2" == "bob" ]]; then
    echo "I see bob!"
  else
    echo "No bob here!"
  fi  
done

请考虑此代码:

readarray -t arr < "$PROPERTY_FILE"
getitem () {
    for i in "${arr[@]}"; {
        read item subitem value <<< ${i//[.=]/ }
        case ${1:-1}${2:-2} in $item$subitem|${item}2|1$subitem)
            echo "item=$item subitem=$subitem value=$value";;
        esac
    }
}

用法

$ getitem Item1 
item=Item1 subitem=subitem1 value=tom
item=Item1 subitem=subitem2 value=bob

$ getitem Item1 subitem2
item=Item1 subitem=subitem2 value=bob

$ getitem '' subitem1
item=Item1 subitem=subitem1 value=tom
item=Item2 subitem=subitem1 value=alice

$ getitem Item2
item=Item2 subitem=subitem1 value=alice

使用关联数组

#!/bin/bash

# The input text file 
PROPERTY_FILE=sampletext.properties


# Load the properties file
#
declare -A properties

loadProperties() {
    local key value

    if [[ "${#properties[@]}" -eq 0 ]]
    then
        # Lazy load the properties file
        # Strip Windows-style CR as we go
        #
        while IFS='=' read -r key value
        do
            properties[$key]="$value"

        done < <(tr -d '\r' <"$PROPERTY_FILE")
    fi
}


# Get the value of a key
#
getProperty() {
    local key=""

    loadProperties
    echo "${properties[$key]}"
}

    
# Count the number of "subitem1" items
#
countsubitem1() {
    loadProperties
    printf "%s\n" "${!properties[@]}" | awk -F. ' == "subitem1"' | wc -l
}

现在您可以使用这些函数并获得预期值

countsubitem1
2

getProperty Item1.subitem2
bob

i=1
subitem1="$(getProperty "Item$i.subitem1")"
printf "%s\n" "$subitem1"
tom

不要使用已弃用的 function 关键字来声明函数。旨在使用小写变量名称,这样您的名称就不会与 $PATH 等标准变量发生冲突。使用 $( ... ) 而不是反引号来括起需要执行的值。