繁体   English   中英

单行 while 循环更新数组

[英]Single line while loop updating array

我正在尝试构建一个更新数组中值的 while 循环,但我不断收到未找到命令的错误。

i=1
bool=true
declare -a LFT
declare -a RGT
while read -r line; do
  ${LFT[$i]}=${line:0:1}; ${RGT[$i]}=$(wc -l < temp$i.txt);
  if [ ${LFT[$i]} -ne ${RGT[$i]} ]; then
    $bool=false;
  fi;
  ((i=i+1));
done<output2.txt

我正在读取的文件每行包含一个数字,我想用每个条目作为数字填充数组 LFT。 数组 RGT 应该用表示为 temp*.txt 的文件的行数填充。 我想测试以确保这两个数组的每个条目都相同。

但是,我不断收到错误消息:command =# not found,其中# 是文件行中的任何数字。 我是否错误地为数组赋值? 另外,我收到错误:找不到命令 true=false。 我假设这与为布尔值分配值有关。 谢谢

问题出在这些行上:

${LFT[$i]}=${line:0:1}; ${RGT[$i]}=$(wc -l < temp$i.txt);

将其更改为:

LFT[$i]=${line:0:1}; RGT[$i]=$(wc -l < temp$i.txt);

shell 中的有效赋值应该是:

var=<expression>

而不是

$var=<expression> ## this will be interpreted by the shell as a command

这是 Bash 程序员常犯的错误之一。 更多Bash 陷阱在这里。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM