我试图在bash脚本中使用gdal。我有几个输入光栅文件,ENVI格式,在不同的目录中,并想给新的输出名称,在GTiff格式。
这样的想法是在一个循环中运行代码,所以这只是一个初始测试,但不想按预期工作。
这是我的抽象代码
#!/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
下面是循环的一些初始代码,但是上面的1元素测试还不能工作。
for i in ${a[*]}
do
gdal_translate –of GTiff ${a[i]} ${b[i]}
done
有什么建议吗?
最佳答案
对于循环,需要遍历数组的每个索引。代码在for i in ${a[*]}
数组的每个元素上迭代。以下是你真正想要的:
for i in ${!a[@]}; do
gdal_translate –of GTiff "${a[$i]}" "${b[$i]}"
done
这假设每个数组中的索引是相同的。请注意,我将
a
和${a[$i]}
用引号括起来,以便将带有空格的元素作为一个参数提供给命令。我想这就是为什么单个命令${b[$i]}
会出现错误“命令选项太多”。