问题
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
}
* UPDATE *
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]}"
}
* UPDATE *
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]}"
}
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.
* SOLUTION *
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? :)
回答1:
Try the following updated version:
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]}"
}
来源:https://stackoverflow.com/questions/23395593/bash-text-database-file-read-in-loop