Bash for loop with arrays second variable?

I am fairly new to bash and am not sure how to resolve this:

I have a series of geographical long/lat points eg. 50/-30 listed on separate lines in a file called junk2. I have input these into an array and am then using that array in a for loop. Towards the end of the loop I create a file called profile$NUMBER. For each loop I wish to name the profile "profile1" for the first loop, then "profile2" for the second loop, ie. have $NUMBER increase by 1 each time, but how can I do this?

I have the following code so far:

# !/bin/bash/
array=()

# Read the file in parameter and fill the array named "array"
getArray() {
    i=0
    while read line # Read a line
    do
        array=$line # Put it into the array
        i=$(($i + 1))
    done < $1
}

getArray "junk2"

# Print the file (print each element of the array)
getArray "junk2"
for e in "${array[@]}"
do
    project -C$e -A145 -G0.01k -L-2500/2500 -Q > junk3
    
    cat junk3 | grdtrack -GSWIR_bath.grd > junk4
    cat junk4 | grdtrack -GSWIR_grav.grd > junk5
    cat junk5 | grdtrack -Gnonairyisotopo2.grd > junk6
    cat junk6 | grdtrack -Gmarion_mask.grd >profile$NUMBER.xydzgim
    
    echo "Ridge location $e done"
done

Many thanks :slight_smile:
Lily

# Print the file (print each element of the array)
NUMBER=1
getArray "junk2"
for e in "${array[@]}"
do
    project -C$e -A145 -G0.01k -L-2500/2500 -Q > junk3
    
    cat junk3 | grdtrack -GSWIR_bath.grd > junk4
    cat junk4 | grdtrack -GSWIR_grav.grd > junk5
    cat junk5 | grdtrack -Gnonairyisotopo2.grd > junk6
    cat junk6 | grdtrack -Gmarion_mask.grd >profile$NUMBER.xydzgim
    
    echo "Ridge location $e done"
    ((NUMBER+=1))
done
1 Like

Duh, why didn't I think of that! I was thinking of complicated answers using if and for.

Massive thank you! :slight_smile:

Occam's Razor....

The simplest answer is usually the right one...:wink:

Without UUOC

project -C$e -A145 -G0.01k -L-2500/2500 -Q > junk3
< junk3 grdtrack -GSWIR_bath.grd > junk4
< junk4 grdtrack -GSWIR_grav.grd > junk5
< junk5 grdtrack -Gnonairyisotopo2.grd > junk6
< junk6 grdtrack -Gmarion_mask.grd >profile$NUMBER.xydzgim

The temp files are good for debugging. If not needed, use pipes

project -C$e -A145 -G0.01k -L-2500/2500 -Q |
grdtrack -GSWIR_bath.grd |
grdtrack -GSWIR_grav.grd |
grdtrack -Gnonairyisotopo2.grd |
grdtrack -Gmarion_mask.grd >profile$NUMBER.xydzgim