0

I'm trying to load files from the directory to the associative array with the access like "FDN,4" where FND is the basename of the file and 4 - is the line number:

loadFiles() {
local iter
local comname
local lines

echo "# Loading files"
find ./sys -type f | while read iter
do
    comname=$(basename "$iter" .aic)

    echo "# $comname"

    local i
    i=0
    while IFS= read -r line 
    do
       commands["$comname,$i"]="$line"
       #echo "$comname,$i = ${commands[$comname,$i]}"
       ((i++))
    done < "$iter"
    [[ -n $line ]] && commands["$comname,$i"]="$line"
done
}

loadFiles

echo "POP,4 = ${commands[POP,4]}"

I'm getting nothing, the ./sys/dir/POP.aic file exists and the 4th line in this file too. Commented echo inside the cycle shows that value assigns.

Can anyone, please, help and show me where I'm wrong?

1 Answer 1

1

Found the root of evil - the subshell. echo "1 2 3" | while <...> will submit the nex subshell, so the variables will be set only locally. The soultion is to use while <...> done < <(find ./sys -type f)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.