Bash的文本数据库文件中读取循环 [英] Bash text database file read in loop

查看:171
本文介绍了Bash的文本数据库文件中读取循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能看到我在这里的最后一次语法错误?问题是递增DBXfields数组。在某种程度上,我被存储从每个数据库中的所有记录到它自己的阵列,使他们能够后来被称为模拟mutlidimensional阵列。我无法通过找出语法来声明使用递增嵌入式变量数组DB。也许我只是需要先设置名称(包括变量,然后声明数组。

Can anyone see the last syntax error I have made here? The issue is with incrementing the DB"X"FIELDS array. In a way, I'm simulating a mutlidimensional array by storing all records from each database into its own array so they can be called later. I can't figure out the syntax through to declare the DB array using an embedded variable that increments. Maybe I just need to Set the name first (including the variable, then declare the array.

function DATABASE.READ {
  cd databases
  DBARRAY=(*)
  cd ..
  local i
  local j
  for i in "${!DBARRAY[@]}"; do
    declare -a DB$iFIELDS                 ### Here is the problem. Declaring the array.
    while read LINE; do 
      for (( j=1; j<=25; j++ )); do
        VALUE="`echo $LINE | cut -d"|" -f"$j"`";
        DB$iFIELDS[$j]="$VALUE"           ### Also here, which I believe requires a different syntax
      done
    done < <(grep -v '#' databases/${DBARRAY[$i]})
  done
}

*更新*

好了,我有一个大的意识到,我离开了数据库的整个层面。因为我已经修改了code创建额外的数组来保存所有的数据库信息。是的,我知道这是开始变得有点失控。我AP preciate所有帮助,但。谢谢

Ok, so I had a big realization that I was leaving out an entire dimension of the database. I have since revised the code to create the additional arrays to hold all database information. Yes I know this is starting to get a little out of hand. I appreciate all the help though. Thanks

function DATABASE.READ {
  cd databases
  DBARRAY=(*)
  cd ..
  local i
  local j
  local k
  i=1
  j=1
  for i in "${!DBARRAY[@]}"; do
    while read LINE; do
      declare -a "D${i}R${j}F"
      for (( k=1; k<=25; k++ )); do
        VALUE="`echo $LINE | cut -d"|" -f"$k"`";
        eval "D${i}R${j}F[$k]=\"$VALUE\""
      done
      let j=j+1
    done < <(grep -v '#' databases/${DBARRAY[$i]})
  done
  ########## EXAMPLE ECHO OF ONE SPECIFIC FIELD IN DATABASE1 RECORD1 FIELD1 ##########
  echo "${D1R1F[1]}"
}

*更新*

感谢你们俩。还有,我在遇到麻烦identifing小的逻辑错误,但我敢肯定它比我们最初的故障排除多simplier。原来的问题似乎得到解决感谢你们。这是当前code:

THANKS to both of you. There is still a small logic error that I'm having trouble identifing, but I'm sure it's much simplier than what we were initially troubleshooting. The original issue appears to be resolved thanks to you guys. Here is the current code:

function DATABASE.READ {
  cd databases
  DBARRAY=(*)
  cd ..
  local i
  local j
  local k
  i=1
  for i in "${!DBARRAY[@]}"; do
    j=1
    while read LINE; do
      declare -a "D${i}R${j}F"
      for (( k=1; k<=25; k++ )); do
        VALUE="`echo $LINE | cut -d"|" -f"$k"`";
        printf -v "D${i}R${j}F[$k]" '%s' "$VALUE"
      done
      let j=j+1
    done < <(grep -v '#' databases/${DBARRAY[$i]})
    unset j
  done
  ########## ECHO TWO SPECIFIC FIELDS IN EACH DATABASE ##########
  echo "${D1R1F[1]}"
  echo "${D2R1F[1]}"
  echo "${D3R1F[1]}"
  echo "${D1R1F[3]}"
  echo "${D2R1F[3]}"
  echo "${D3R1F[3]}"
}

这看起来像是有问题既呼应数据库1个记录。试图找出在循环是什么消磨它,或者也许它一个更大的问题。

It looks like it's having trouble echoing both database 1 records. Trying to figure out what in the loop is goofing it up, or if maybe its a bigger problem.

*解决方案*

所以看起来这个问题是与数据库递增。当我不专门设置我,并引用数据库从0开始。它修复它。其实...这是有道理的吧。当DB文件读入DBARRAY,他们开始元素0这样啊......

So it looks like the issue was with the database incrementing. When I don't specifically set i, and reference the databases starting at 0. It fixes it. Actually... it makes sense now. When the DB files are read into DBARRAY, they start with element 0 so yeah.....

所以它的你,我给信用吗? :)

So which of you do I give credit too? :)

推荐答案

试试下面的更新版本:

function DATABASE.READ {
   cd databases
   DBARRAY=(*)
   cd ..
   local i
   local j
   local k
   i=1
   j=1
   for i in "${!DBARRAY[@]}"; do
      declare -a "DB${i}FIELDS"
      while read LINE; do
         declare -a "D${i}R${j}F"
         for (( k=1; k<=25; k++ )); do
            VALUE="`echo $LINE | cut -d"|" -f"$k"`";
   echo "VALUE = '$VALUE'"
            eval "D${i}R${j}F[$k]=\"$VALUE\""
   eval "echo ${D${i}R${j}F[$k]}"
         done
         let j=j+1
      done < <(grep -v '#' databases/${DBARRAY[$i]})
   done
   ########## EXAMPLE ECHO OF ONE SPEFIC FIELD IN ##########
   echo "${D1R1F[1]}"
}

这篇关于Bash的文本数据库文件中读取循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆