2013-10-04 52 views
0

我試圖在變量中存儲命令的結果,但無法弄清楚正確的語法。此命令的工作perferctly:將命令的結果存儲在變量中

echo "-868208.53 -1095793.57 512.30" | cs2cs +init=esri:102067 +towgs84=570.8,85.7,462.8,4.998,1.587,5.261,3.56 +to +init=epsg:4326 

這是怎麼了,我試圖用這個例子,它適用於我的數據,但outcord分配是錯誤的:

#!/usr/bin/bash 
filename="$1" 
while read -r line 
do 
    name=$line 
    a=($name) 
    coord= ${a[1]} ${a[2]} 
    outcoord= $("echo $coord | cs2cs +init=esri:102067 +towgs84=570.8,85.7,462.8,4.998,1.587,5.261,3.56 +to +init=epsg:4326") 
    echo $outcoord 
done < "$filename" 

樣的錯誤,我得到:

remap.sh: line 7: -515561.05: command not found 
remap.sh: line 8: echo | cs2cs +init=esri:102067 +towgs84=570.8,85.7,462.8,4.998,1.587,5.261,3.56 +to +init=epsg:4326: command not found 
remap.sh: line 7: -515542.01: command not found 
remap.sh: line 8: echo | cs2cs +init=esri:102067 +towgs84=570.8,85.7,462.8,4.998,1.587,5.261,3.56 +to +init=epsg:4326: command not found 

樣本數據:

1 -515561.05 -1166540.03 
2 -515542.01 -1166548.76 
3 -515519.61 -1166552.19 
4 -515505.29 -1166550.25 
5 -515477.05 -1166546.82 
6 -515431.12 -1166534.06 
7 -515411.45 -1166517.39 

如何將命令結果分配給變量outcoord

回答

2

你有很多問題。在下面的行的第一:

coord= ${a[1]} ${a[2]} 
  • 不要把周圍=空間。
  • 如果這些變量包含空格,則引用變量。

的以下行:

name=$line 
a=($name) 
coord= ${a[1]} ${a[2]} 
outcoord= $("echo $coord | cs2cs +init=esri:102067 +towgs84=570.8,85.7,462.8,4.998,1.587,5.261,3.56 +to +init=epsg:4326") 
echo $outcoord 

outcoord=$(echo "${line[@]}" | cs2cs +init=esri:102067 +towgs84=570.8,85.7,462.8,4.998,1.587,5.261,3.56 +to +init=epsg:4326) 
echo "$outcoord" 

,它應該是不錯的。

如果只想數組的最後兩個元素,你可以說:

outcoord=$(echo "${line[1]} ${line[2]}" | cs2cs +init=esri:102067 +towgs84=570.8,85.7,462.8,4.998,1.587,5.261,3.56 +to +init=epsg:4326) 

而且,代替:

while read -r line 

while read -a line 

這將分配行讀取到數組line

0

嘗試刪除雙引號;即:

outcoord=$(echo $coord | cs2cs +init=esri:102067 +towgs84=570.8,85.7,462.8,4.998,1.587,5.261,3.56 +to +init=epsg:4326) 

否則,bash將整個字符串解釋爲命令的名稱。

+0

我試過了,但仍然收到錯誤:'remap.sh:line 7:-515561.05:command not found remap.sh:line 8:24d49'46.026「E:command not found' –

+0

啊,我錯過了問題在線7.我認爲devnull的答案應該可以解決你的問題。 –

相關問題