subtraction in bash arrays


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting subtraction in bash arrays
# 1  
Old 01-08-2009
subtraction in bash arrays

hi i am using bash shell to perform some subraction. here is what i have:
i have a while loop and am using i as a counter.

Code:
diff[i]= `expr ${ARRAY1[i]} - ${ARRAY2[i]}`

for example array1[1] has -0.7145 and array2[1] has -0.7041.
when i try the above command, i get expr: non-numeric argument. any ideas how i can do this subtraction?
thanks
# 2  
Old 01-08-2009
all shell math is integer - you're dealing with floats.
Look into 'man bc'.
# 3  
Old 01-08-2009
Try removing the space after the '=' for starters

Or try...
Quote:
a=${ARRAY1[i]}
b=${ARRAY2[i]}
diff[i]=`expr $a - $b`
and see what happens...
# 4  
Old 01-08-2009
vgersh99 makes a very good point about integer arithmetic and using bc !

Try...
Quote:
diff[i]=`bc <<EOF
a=${ARRAY1[i]}
b=${ARRAY2[i]}
a - b
EOF
`
Personally, I think bc is one of the worst Unix utilities around, but it has its uses!

Jerry
# 5  
Old 01-08-2009
Code:
diff[i]=`echo "${ARRAY1[i]} - ${ARRAY2[i]} | bc`

Quote:
Originally Posted by JerryHone
Personally, I think bc is one of the worst Unix utilities around, but it has its uses!
Why is that?
# 6  
Old 01-08-2009
Quote:
Why is that?
Having been a Unix user for 20 years, most utilities have always seemed to fit into a standard way of doing things, but bc has always seemed to me to be quirky, and I've never got really on with it. awk seemed easy to learn in comparison and I've always found alternative ways to avoid using bc.
But, I love using vi while others dislike it because, IMHO, they've never really bothered to learn to use it properly. Maybe I should take some time to properly learn bc Smilie
# 7  
Old 01-08-2009
Quote:
Originally Posted by npatwardhan
hi i am using bash shell to perform some subraction. here is what i have:
i have a while loop and am using i as a counter.

Code:
diff[i]= `expr ${ARRAY1[i]} - ${ARRAY2[i]}`


There is never any need to use expr in a bash script.
Quote:
for example array1[1] has -0.7145 and array2[1] has -0.7041.
when i try the above command, i get expr: non-numeric argument. any ideas how i can do this subtraction?
thanks

For floating-point arithmetic, you need an external command (like the shell , expr only does integer arithmetic; ksh93 is an exception).

Code:
paste <(printf "%s\n" "${ARRAY1[@]}") <(printf "%s\n" "${ARRAY2[@]}") |
awk '{ print $1 - $2 }'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Syntax error in subtraction in Bash

I am sharing a code snippet. for (( i=0; i<=$(( $count -1 )); i++ )) do first=${barr2} search=${barr1} echo $first echo "loop begins" for (( j=0; j<=5000; j++ )) do if } == $search ]]; then echo $j break; fi done second=${harr2} echo $second (2 Replies)
Discussion started by: ngabrani
2 Replies

2. Shell Programming and Scripting

Basic Bash algorithm with sum/subtraction

Hi all, i'm making some test on a data file. Imagine i have two columns inside it : 80377,20 80377,20 80379,19 80378,20 80380,20 80382,20 80381,21 Just to understand how can it works, imagine to subtract 100 to the number in the first column when the other one in the second... (4 Replies)
Discussion started by: Board27
4 Replies

3. Shell Programming and Scripting

Subtraction using arrays

Hello all . I have two arrays. ${ARRAY_MOUNT_POINT_CAPACITY} ${ARRAY_MOUNT_POINT_CAPACITY}. Whats the synatx of subtracting their values , placing them in variable V1 and then echoeing it ??? Ive tried expr and let ...gives me ./test_code.sh: difference: bad number (3 Replies)
Discussion started by: Junaid Subhani
3 Replies

4. Shell Programming and Scripting

Using arrays in bash using strings to bash built-in true

I have the following code and for some reason when I call the program using /home/tcdata/tatsh/trunk/hstmy/bin/bash/raytrac.bash --cmod=jcdint.cmod I get hasArgument = hasArgument = true Somehow the array element is returning even though I have not chosen the option. ... (41 Replies)
Discussion started by: kristinu
41 Replies

5. Shell Programming and Scripting

bc,getopt and arrays in bash

trying to sum elements in an array using bc and getopt,i have a file with names and thier vaules if the names appears 3 times i should multiply its value with 3 then find the sum of all the elements together cat foo.txt max 2.3 henry 3 fransis 4.5 max 2.3 henry 3 max 2.3 it should... (1 Reply)
Discussion started by: elginmulizwa
1 Replies

6. Shell Programming and Scripting

Yet another bash arrays question

Hi all, I have a file that contains many lines, but only a few are of my interest, so I'm cutting it with grep + awk, and the result I get is for example line 0 line 1 line 2 line 3 line n Now I want to store each line in an array "cell" so I can use it later calling to ${array},... (2 Replies)
Discussion started by: TuxSax
2 Replies

7. Shell Programming and Scripting

arrays and while loops in bash

hi guys, i have an array called ARRAY which has elements in it... i am trying to assign elements of ARRAY to master_array.. i get a =: command not found error.. i=0 while do ${master_array}=${ARRAY} ((i++)) done is there something i am missing? (4 Replies)
Discussion started by: npatwardhan
4 Replies

8. Shell Programming and Scripting

arrays in bash

hi guys, i have the following script and when i run it i get blank lines on the screen.. i am trying to display the contents of array var.. #!/usr/bin/bash var=`awk 'NR>20&&NR<31' try.sum | awk '{print $4}'` echo "${var}" (1 Reply)
Discussion started by: npatwardhan
1 Replies

9. Shell Programming and Scripting

Searching Bash Arrays

Hi, I am writing a bash shell script. I would like to execute a statement only if an array contains a specific value. For example: array=(1 3 5 7) I would like to execute the statement only if the value 3 is present in ${array}. Thanks for any help, Mike (1 Reply)
Discussion started by: msb65
1 Replies

10. Shell Programming and Scripting

Arrays in bash.need help

:confused: Is it possible to delete array elements dynamically.For instance,consider an array( a b c d ) ,now can i delete array (the third element 'c').So that the array becomes array(a b d).. Thanks in advance!! (1 Reply)
Discussion started by: tj23
1 Replies
Login or Register to Ask a Question