0

I have bash script which runs a SQL query and outputs the following:

*************************** 1. row ***************************
name: danny
id: 123
age: 1
wait: 326.000000
*************************** 2. row ***************************
name: ronny
id: 1234
age: 2
wait: 21.000000

I'm trying to understand how can I create an array and store each record inside, for each record I want to be able to accces its params for example:

Person1["name"] = danny,  Person2["name"]=ronny

1 Answer 1

1

The index of an array must be numeric. But you can use index instead of field name. Variables are known only within the block. But with a simple trick (sourcen) you get them to the outside.

# ASSUMPTION: the input data are in the file

Personen=$(mktemp)
i=0
while read line; do
    [[ "${line:0:1}" == '*' ]] && continue
    key=${line%%:*}
    value=${line##*: }
    [[ "$key" == "name" ]] && ((i++))
    echo "Person_${key}[$i]=\"$value\""
done < file >$Personen

source $Personen

echo "${Person_name[1]} ${Person_age[1]}"
echo "${Person_name[2]} ${Person_wait[2]}"

rm $Personen

output

danny 1
ronny 21.000000
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.