I have an installed library called Gdal which runs certain GIS commands.

This command runs for a single file

gdal_translate -a_srs EPSG:25832 INPUT_FILE OUTPUT_FILE

but I would like to run a batch command which iterates through all *.tif files so I don´t have to write the name of each one (i´ve got 1300 files!)

I tried this in a .sh file...but it didn´t work

#!/bin/bash

for FILE in *.tif
do
  BASE=$FILE .tif
  NEWFILE=test/${BASE}.tif
  gdal_translate -s_srs EPSG:25832 $FILE $NEWFILE
done

could anyone show me how to do this?

yours,

Robert

有帮助吗?

解决方案

$FILE includes the .tif extension. Also BASE=$FILE .tif doesn't do what you think (it executes .tif with $BASE set to $FILE for the duration of the command).

You also have the difference between -a_srs and -s_srs. I don't know which you intended.

The end result is, I think, that you want to use test/$FILE as the output filename.

#!/bin/bash

for FILE in *.tif; do
  gdal_translate -s_srs EPSG:25832 "$FILE" "test/$FILE"
done

(The quotes make it work with a path with spaces in it. Putting the for and do on the same line is a common way of writing it to save space.)

其他提示

Your FILE variable already has .tif on the end, so you are appending .tif again. Try this instead:

#!/bin/bash

for FILE in *.tif 
do 
  NEWFILE=test/${FILE}
  gdal_translate -s_srs EPSG:25832 $FILE $NEWFILE
done
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top