1

I have just registered here.

I am working on a script which puts data in an array into separate variables.

Example:

for((i=0; i < Counter; i++)); do
    while read -r Parmfilesjobid; do
        IFS=$'\n' read -d '' -r -a  "job$i" < ${Parmfilesjobid[$i]}
    done <<< "${Parmfilesjobid}" 
done

The counter is a seperate variable because the number of times the for loop has to run can differ.

As $I is incremented every time, I am trying to find out how I can turn the job$i into job0, job1, job2.

This because every job$i contains seperate values.

When i use:

echo ${job1[@]}
echo ${job2[@]}
echo ${job3[@]}

I can get the correct output per job$i (job0, job1, job2)

But i want bash to convert the job$i into job0,job1,job2 so i can use them in another loop as seperate variables.

  • $Parmfilesjobid is used both as an array and scalar. Are you sure that's what you want? – choroba Jun 16 '17 at 8:45
1
read -r "job$i" < ...

This should work, if i=3, then read should set job3.

However, your loops look a bit off:

for((i=0; i < Counter; i++)); do
    while read -r Parmfilesjobid; do
        IFS=$'\n' read -d '' -r -a  "job$i" < ${Parmfilesjobid[$i]}

The while read will only put one value in Parmfilesjobid, so e.g. ${Parmfilesjobid[1]} will be unset. (Of course a scalar access like $var is the same as ${var[0]}, so if you had Parmfilesjobid set as an array before, you might have values with other indices too, but you're not using the value from read except when i=0.

You're also using Parmfilesjobid as both the input to the while read... loop, and as the variable used by read, which is a bit confusing, and means that, the latter iterations of the for would give while the last Parmfilesjobid read in the previous iteration as input. That seems somewhat odd.

Since I don't see your input, I'm not exactly sure what you want to do here. But I suspect something like this might be closer:

i=0
while read -r filename ; do
    IFS=$'\n' read -d '' -r -a  "job$i" < "$filename"
    let i++
done <<< "${var_with_filenames}"
0

Thanks for the quick answer!

I will try and expain what the input is:

I have some files which contain some parameters. These parameters are different for every file, but they Always contain the same type of data. It contains a customer ID, order ID and group ID, which when combined form an UNC path.

So far the script locates these files and puts them in an array, this array is then used in the for loop from my first post to extract the data within each seperate file and put it in a different array (the job$i). After that i am stuck.

I can echo each job individually and get the data contained in them with: echo $job0 echo job1 echo job2

I need to find a way to get another for loop working with the job$i, because i need every job$i to run through the for loop in order to extract the data and make it into a path.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.