0

I have a Python code that takes multiple input files and merges them into one single output file. I want to create a bash script that adds the input files automatically, without having me to manually write infile1 infile2, etc. Below is what I came up with:

FILE= `find ~/Desktop/folder -name '*.tif'`
for i in $FILE
do
gdal_merge.py -o mosaic -of GTiff $i
done

But for some reason I am getting this error:

Syntax error: word unexpected (expecting ")")
tshepang
  • 12,111
  • 21
  • 91
  • 136
GIS_DBA
  • 221
  • 2
  • 11
  • Unless you have your heart really set on using `bash`, look into the Python `fileinput` standard library - it handles this kind of thing with very little effort. – Peter DeGlopper Dec 04 '13 at 21:52
  • 1
    Put an echo statement in your for loop that print $i. I suspect you are encountering spaces in filenames or some other problematic character. – EJK Dec 04 '13 at 21:55
  • 2
    You can't put spaces around the equal sign in an assignment: `FILE=\`find ~/Desktop/folder -name '*.tif'\``. This may or may not be related to your syntax error. – chepner Dec 04 '13 at 22:07
  • @chepner: You are right. I removed the space and the script worked but unfortunately did not produce the results I wanted! – GIS_DBA Dec 05 '13 at 09:00
  • The error is fixed but the script is not working the way I wanted it to work! As you might have noticed, the script is supposed to merge more than TIF image and produce one big TIF image. The problem is, after running the script, the resulted image is one of the input images and not a mosaic of all the images! In other words the script is giving me out.tif = in2.tif and not out.tif = in1.tif + in2.tif + in3.tif! – GIS_DBA Dec 05 '13 at 09:04

3 Answers3

0

You could try the -exec option to find:

find ~/Desktop/folder -name '*.tif' -exec gdal_merge.py -o mosaic -of GTiff {}
sixpi
  • 26
  • 4
0

My guess is that some of your files contain special characters like ( or ) or whitespacespace characters that would cause trouble. In general '-x' option would show you what's going on. Either do bash -x my_script or add set -x at the beginning of the script.

As an alternative that's somewhat better at dealing with special characters try this:

find ~/Desktop/folder -name '*.tif' -print0 | xargs -0 -n1 gdal_merge.py -o mosaic -of GTiff 
ArtemB
  • 3,496
  • 17
  • 18
0

this might work :

FILE=`find ~/Desktop/folder -name '*.tif'`
gdal_merge.py -o mosaic -of GTiff "$FILE"
michael501
  • 1,452
  • 9
  • 18
  • The downside of this approach is that there is a limit on how long your command line argument may be. http://stackoverflow.com/questions/6846263/maximum-length-of-command-line-argument-that-can-be-passed-to-sqlplus-from-lin On my Ubuntu box 'xargs --show-limits' reports ~2MB as the command line max, so you're probably good for few hundred thousands of files, but would fail with a million of them. find|xargs don't have this limitation. – ArtemB Dec 19 '13 at 02:29