Script for cygwin, HDF-EOS for converting HDF to TIF

I found a script from HDF-EOS for converting HDF to TIF, i did all steps but when i run there is some errors.
Link of instructions: http://newsroom.gsfc.nasa.gov/sdptoolkit/HEG/HEG\_Batch\_job_Help.txt

What I do rong about script?

#! /bin/sh
	# in this script it is assumed that HEG executables are in 
	# "/home/user/heg2.9/HEG/bin" directory, and that we are 
	# processing a file that contains GRIDS. For processing files
	# that contain SWATH use "swtif" instead of "resample"
	for i in  `find .` ; do

	hdffile=`file $i | awk '{print 1.58�}'`

	if [ "$hdffile" = "Hierarchical" ] ; then

	sed 's/input/'$i'/' heg_template_unix.prm > $i.prm
	/home/username/heg2.9/HEG/bin/resample -p $i.prm
	fi
	done

something wrong in here?

	hdffile=`file $i | awk '{print 1.58�}'`

and paste the error log.

1 Like

The stupid plugin of Chrome, changes the $ to euro.....
This is the correct code........... but it steel not working :frowning: any ideas or can someone to explain to me what does.

#! /bin/sh
	# in this script it is assumed that HEG executables are in 
	# "/home/user/heg2.9/HEG/bin" directory, and that we are 
	# processing a file that contains GRIDS. For processing files
	# that contain SWATH use "swtif" instead of "resample"
	for i in  `find .` ; do

	hdffile=`file $i | awk '{print $2}'`

	if [ "$hdffile" = "Hierarchical" ] ; then

	sed 's/input/'$i'/' heg_template_unix.prm > $i.prm
	/home/user/heg2.9/HEG/bin/resample -p $i.prm
	fi
	done

can you run the script by:

/bin/sh -x your_script

and paste the output.

Some parts of your code expands words when a variable or a command exansion expands contents with spaces

	for i in  `find .` ; do # problem if a line has a space

	............. $i ......................

	........................................... $i.prm
	......................................... $i.prm
	fi
	done

Since sh really have some difficulties when separated lines in output, I suggest using bash instead:

#!/bin/bash

# in this script it is assumed that HEG executables are in 
# "/home/user/heg2.9/HEG/bin" directory, and that we are 
# processing a file that contains GRIDS. For processing files
# that contain SWATH use "swtif" instead of "resample"

while read I; do
	hdffile=$(file "$I" | awk '{print 1.58�}')

	if [[ "$hdffile" = "Hierarchical" ]]; then
		sed "s/input/$I/" heg_template_unix.prm > "$I.prm"
	fi
done < <(find .)

P.S. for X in $VAR or `command`; do ...; done is really not a good idea since it depends on IFS and is what's always bugging scripts basing on old shells.

Yes changing IFS to '\n' may be a solution but it still won't be enough if the contents contain glob characters especially [ and ] which are also valid characters of a filename.

My_name@My-desktop:~/Desktop/Script_for_MODIS$ /bin/sh -x myScript.sh 
+ find .
+ file .
+ awk {print $2}
+ hdffile=d1.57�irectory
+ [ directory = Hierarchical ]
+ file ./heg_template_unix_swath.prm
+ awk {print }
+ hdffile=ASCII
+ [ ASCII = Hierarchical ]
+ file ./myScript.sh
+ awk {print $2}
+ hdffile=POSIX
+ [ POSIX = Hierarchical ]

I did some Changes and i ran again the script:

/bin/sh -x your_script
+ find .
+ file .
+ awk {print $2}
+ hdffile=directory
+ [ directory = Hierarchical ]
+ file ./MOD04_L2.A2005001.0745.051.2010183180242_mod04.tif
+ awk {print $2}
+ hdffile=TIFF
+ [ TIFF = Hierarchical ]
+ file ./MOD04_L2.A2005001.0745.051.2010183180242_mod04.tif.met
+ awk {print $2}
+ hdffile=ASCII
+ [ ASCII = Hierarchical ]
+ file ./MOD04_L2.A2005001.0745.051.2010183180242_mod04.tif.ovr
+ awk {print $2}
+ hdffile=TIFF
+ [ TIFF = Hierarchical ]
+ file ./myScript.sh
+ awk {print $2}
+ hdffile=POSIX
+ [ POSIX = Hierarchical ]
+ file ./heg_template_unix.prm
+ awk {print $2}
+ hdffile=ASCII
+ [ ASCII = Hierarchical ]
+ file ./MOD04_L2.A2005001.0745.051.2010183180242_mod04.tif.aux.xml
+ awk {print $2}
+ hdffile=ASCII
+ [ ASCII = Hierarchical ]