![while 循环中意外标记“in”附近出现语法错误](https://linux22.com/image/112957/while%20%E5%BE%AA%E7%8E%AF%E4%B8%AD%E6%84%8F%E5%A4%96%E6%A0%87%E8%AE%B0%E2%80%9Cin%E2%80%9D%E9%99%84%E8%BF%91%E5%87%BA%E7%8E%B0%E8%AF%AD%E6%B3%95%E9%94%99%E8%AF%AF.png)
我想处理一些数据,我需要通过读取文本文件中写入的一些坐标来缩小要处理的区域,..
我有以下错误:
./script5.sh: line 59: syntax error near unexpected token
在'./script5.sh:第59行:while IFS="" read -r $L1Aname north south east west || [[ -n "$L1Aname north south east west" ]] in $coord; do'
这就是我所拥有的:
while IFS="" read -r $L1Aname north south east west || [[ -n "$L1Aname north south east west" ]] in $coord; do
Nlat="$north" #name variable north
Slat="$south" #name variable south
Elon="$east" #name variable east
Wlon="$west" #name variable west
done < "$coord";
谢谢!
答案1
正如 Barmar 在对该问题的评论中指出的那样,您的 while 循环看起来好像它最初是一个迭代的 for 循环$coord
(一个变量可能保存文件的全部内容)。
正确的 while 循环可能类似于
while read -r L1Aname north south east west; do
Nlat="$north"
Slat="$south"
Elon="$east"
Wlon="$west"
done <"$coord"
我也已经放弃$
了$L1Aname
。我不完全确定这是正确的,但正如你一样可以 read $L1Aname
(这会将一个值读入变量谁的名字存储在变量中L1Aname
)。我会认为这是无意的(如果我错了,只需更改L1Aname
为下面的内容)。$L1Aname
如果您需要检查非空值,请不要在字符串上进行测试,"$L1Aname north south east west"
因为这保证是非空的。相反,测试各个变量的值:
while read -r L1Aname north south east west
&& [ -n "$north" ] && [ -n "$south" ]
&& [ -n "$east" ] && [ -n "$west" ]
do
Nlat="$north"
Slat="$south"
Elon="$east"
Wlon="$west"
# use "$Nlat", "$Slat", "$Elon" and "$Wlon" here.
done <"$coord"
您不需要测试,$L1Aname
因为这保证包含某物如果他们read
能够阅读一些东西。