单行同时循环更新数组 [英] Single line while loop updating array

查看:99
本文介绍了单行同时循环更新数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to build a while loop that updates the values in an array but I keep getting a command not found error.

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的文件的行数填充。并且我想测试以确保这两个数组的每个条目都是相同的。

The file I am reading from contains a single digit on each line, and I want to fill the array LFT with each entry being the digit. The array RGT should be filled with the line counts of files denoted temp*.txt. And I want to test to make sure each entry of these two arrays are the same.

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

However, I keep getting an error: command =# not found, where # is whatever digit is on the line in the file. Am I assigning values to the arrays incorrectly? Also, I get the error: command true=false not found. I am assuming this has something to do with assigning values to the boolean. Thanks

推荐答案

问题出在以下几行:

${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中的有效分配应为:

Valid assignment in shell should be:

var=<expression>

而不是

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






<这是Bash程序员经常犯的错误之一。在这里重击陷阱

这篇关于单行同时循环更新数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆