Bash-从数组矩阵中添加数字

时间:2019-01-20 05:26:06

标签: bash unix matrix sum add

在矩阵中加数字时,出现此错误: 第271行:1 2 3 4:表达式中的语法错误(错误标记为“ 2 3 4”)

我的添加功能:

add()
{
   #Reading matrices into temp files 
   while read line1 <&3 && read line2 <&4
   do
      echo "$line1" | tr "\n" "\t" >> "temp50"
      echo "$line2" | tr "\n" "\t" >> "temp60"
   done 3<$temp1 4<$fileTwo
   echo >>"temp50"
   echo >>"temp60"

cat "temp60" >> "temp50"

i=1
x=1

while [ $i -le $totalNum ]
do
   sum=0
   cut -f $i "temp50" > "temp55"

   while read num
   do
      sum=$(($sum + $num))
   done <"temp55"

   echo "$sum" | tr "\n" "\t" >> "temp65"

   #try and remove hanging tab
   if [[ "$x" -eq "$numcolOne" ]]
   then
      rev "temp65" > "temp222"
      cat "temp222" | cut -c 1- >"temp333"
      rev "temp333">"temp65"
      x=0
   fi

   i=$((i+1))
   x=$((x+1))

done

矩阵数组(temp1):

1 2 3 4

5 6 7 8

该函数应该在temp1文件中添加矩阵数组;示例输出:

2 4 6 8

10 12 14 16

感谢任何人的帮助!

0 个答案:

没有答案