Search code examples
bashgdal

Bash: how to turn list of unique no data values into variables that can be used?


basepath=Desktop/DEM

dir=(ls -1 type -f)

cd $dir
for f in *.tif; do gdalinfo "$f" | grep -o 'NoData Value\=[-0-9]*' || echo "NoData Value=None"; done > test.txt
cat test.txt | sort | uniq > uniquenodata.txt #this is to find unique no data values in a directory 

nodatalist=$(cat uniquenodata.txt)
rightnodata=-9999

I have made the BASH script above to find out the different no data values in a directory. My goal is to have separate folders that have only one type of no data value, I need to somehow create a for loop that will convert the list of unique no data values ($nodatalist) and check each tif's no data value and send it to the corresponding folder that has these no data values. I am very new to BASH and do not know how to turn a list of values into a variable that can be used in a for loop.


Solution

  • A more efficient approach is to move the files immediately. Create the destination directory if it doesn't exist.

    for f in *.tif; do
        i=$(gdalinfo "$f" | grep -o 'NoData Value=[-0-9]*') && d=${i#NoData Value=} || d="None"
        mkdir -p "$d"
        mv "$f" "$d"/
    done 
    

    As an aside, these lines look like a syntax error:

    dir=(ls -1 type -f)
    cd $dir
    

    This will effectively cd test if you have a directory by this name. Maybe you actually mean find -type f but this obviously doesn't produce a directory (-type f specifically selects regular files which aren't directories).