[英]Bash scripting and gdal - how to refer to elements in an array variable?
I am trying to use gdal in a bash script. 我试图在bash脚本中使用gdal。 I have several input raster files, in ENVI format, in different directories, and want to give new outputnames, in GTiff format.
我在不同的目录中有几个ENVI格式的输入栅格文件,并希望以GTiff格式提供新的输出名称。
The idea is then to run the code in a loop, so this is just an initial test, but does not want to work as expected. 然后的想法是在循环中运行代码,因此这只是一个初始测试,但不想按预期工作。
This is my abstracted code 这是我的抽象代码
#!/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
Some initial code for a loop below, but the 1-element test above does not work yet. 下面是循环的一些初始代码,但是上面的1元素测试尚不起作用。
for i in ${a[*]}
do
gdal_translate –of GTiff ${a[i]} ${b[i]}
done
Any suggestions? 有什么建议么?
For your loop, you want to iterate over each index of the array. 对于循环,您要遍历数组的每个索引。 The code
for i in ${a[*]}
iterates over each element of the a
array. 该代码
for i in ${a[*]}
超过的每个元素迭代a
阵列。 Here's what you actually want: 这是您真正想要的:
for i in ${!a[@]}; do
gdal_translate –of GTiff "${a[$i]}" "${b[$i]}"
done
This assumes that the indexing is the same in each array. 假定每个数组中的索引均相同。 Note that I wrapped
${a[$i]}
and ${b[$i]}
in quotes so that elements with spaces are given to the command as one argument. 请注意,我将
${a[$i]}
和${b[$i]}
括在引号中,以便将带空格的元素作为一个参数提供给命令。 I assume this is why the single command gdal_translate –of GTiff ${a[1]} ${b[1]}
gives the error "too many command options". 我假设这就是为什么
gdal_translate –of GTiff ${a[1]} ${b[1]}
的单个命令gdal_translate –of GTiff ${a[1]} ${b[1]}
给出错误“太多命令选项”的原因。
In order to close this question, this is what I did. 为了解决这个问题,这就是我所做的。 I found a work-around for this situation:
我发现了这种情况的解决方法:
.tif
files I could use .tif
文件 : :
for filename in *.img;
do
gdal_translate -of GTiff -a_nodata 0 $filename ../d_Separate_tif/$filename.tif
done
I'll close this question now. 我现在结束这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.