Search code examples
arrayslinuxbashloopsgdal

Bash scripting and gdal - how to refer to elements in an array variable?


I am trying to use gdal in a bash script. I have several input raster files, in ENVI format, in different directories, and want to give new outputnames, in GTiff format.

The idea is then to run the code in a loop, so this is just an initial test, but does not want to work as expected.

This is my abstracted code

#!/bin/bash

#Inputfiles a:
echo ${a[*]}
#/home/dir1/dir2/filename1 /home/dir1/dir4/filename2 /home/dir1/dir5/filename3

#outputnames b:
echo ${b[*]}
#outputname1 outputname2 outputname3


#this works, just to test
echo ${a[1]} ${b[1]} > file1.txt

#this works
gdal_translate –of GTiff /home/dir1/dir2/filename1 outputname1

#but this does not want to work? why?
gdal_translate –of GTiff ${a[1]} ${b[1]}

#error: Too many command options

Some initial code for a loop below, but the 1-element test above does not work yet.

for i in ${a[*]}
do
   gdal_translate –of GTiff ${a[i]} ${b[i]}
done

Any suggestions?


Solution

  • In order to close this question, this is what I did. I found a work-around for this situation:

    1. All my files were in separate folders, so I had to move them into a common folder. This was key to looping though the files.
    2. Then to convert all the ENVI files to .tif files I could use

    :

    for filename in *.img;
    do
        gdal_translate -of GTiff -a_nodata 0 $filename ../d_Separate_tif/$filename.tif
    done
    

    I'll close this question now.