0

i have three arrays A, B, C. Array A-B values are being parsed from file and i want them to add up into array C.

#!/bin/bash
i=0
A=()
B=()
C=()
while read line
do
  A[i]="$(echo $line| cut -d\  -f4)"
  B[i]="$(echo $line| cut -d\  -f11)"
  echo ${A[i]} " and " ${B[i]}
  # outputs correct values
  C[i]=`expr ${A[i]} + ${B[i]}`
  echo ${C[i]} 
  # no output
  i=$((i+1))
done < ~/file
exit 0

what is wrong with that assignment?

complete line from script:

hitEnd[i]=`expr ${hitLength[i]}+${hitStart[i]}`
echo "${hitEnd[i]}"
#no output
15
  • Could you paste the complete error output? Commented Dec 1, 2012 at 7:29
  • @louxiu complete error output: ./getSum.sh line 131: 635: command not found.. (635 is the array B's value) Commented Dec 1, 2012 at 7:36
  • @louxiu at the first echo it outputs the correct values, but at the error output there is no value shown for array A's value.. Commented Dec 1, 2012 at 7:45
  • No idea... what about paste the input file? Commented Dec 1, 2012 at 7:51
  • 1
    I can't reproduce the problem. I ran your script and it worked as expected. There must be something in the real script that you're not showing. Commented Dec 1, 2012 at 8:40

2 Answers 2

1

the line:

    C[i]=`expr ${A[i]} + ${B[i]}`

while give an error if one of the two operands is missing. If that is expected, i.e. having empty fields in the files, then:

   C[i]=$((${A[i]:-0}+${B[i]:-0}))

Should work.

Sign up to request clarification or add additional context in comments.

Comments

-1

The script look correctly. You may not construct the array correctly.

And always double quote the variable to avoid some error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.