Added Two Arrays But With Errors


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Added Two Arrays But With Errors
# 1  
Old 10-04-2018
Added Two Arrays But With Errors

Please could someone have a look at the code below and spot the cause of the error.
Thanks in advance.
CODE BELOW: (code tags now added) My apologies please, that was my first time here.Smilie

Code:
#! /bin/bash
# file: whileloop.sh

arr1=(2 4 6 8)
arr2=(3 6 9 12)
arr3=()

indextotal=(${#arr1[@]})
i=0
sum=0
while [[ ${#arr1[@]} -eq ${#arr2[@]} ]] && [[ $i -le $indextotal ]] # ${#arr[@]} depicts the size of an array
do
    sum=$(( "${arr1[i]}"+"${arr2[i]}" )) 
    #echo $sum
    arr3=("${arr3[@]}" "$sum")
    i=$(( i+1 ))
done
echo "${arr3[@]}"

OUTPUT BELOW
Code:
whileloop.sh: line 13: +  : syntax error: operand expected (error token is "+  ")
5 10 15 20

Thanks,
Chiadi




Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 10-04-2018 at 10:57 AM..
# 2  
Old 10-04-2018
That happens in the last loop. You are running loop variable i one too high for the array elements (i = 4; with elements only from 0 to 3). Try
Code:
[[ $i -lt $indextotal ]]

in lieu of -le.


Run script with the xtrace (-x) option set to see what is going on.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 10-04-2018
Quote:
Originally Posted by RudiC
That happens in the last loop. You are running loop variable i one too high for the array elements (i = 4; with elements only from 0 to 3). Try
Code:
[[ $i -lt $indextotal ]]

in lieu of -le.


Run script with the xtrace (-x) option set to see what is going on.
You nailed it.
Thanks a lot.
I am a newbie.

------ Post updated at 01:51 PM ------

Thanks to you RudiC for the extra suggestion of using xtrace in troubleshooting. I'm grateful. Smilie
That was my first post here and I only joined yesterday.

Regards,
Chiadi
# 4  
Old 10-04-2018
You're welcome.


Thanks for trying to edit code tags in; but - use code tags for code and data, not PHP tags. I removed those in post#1 for you.
# 5  
Old 10-04-2018
Thanks a lot.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. AIX

LPAR cannot added disk

Dear All, I created a new partition through "Integrated Virtualization Manager" but there have an error when I added a new disk to the partition. The disk already created without any issue, Below error is to add the disk to the partition An error occured while modifying the assignments... (5 Replies)
Discussion started by: lckdanny
5 Replies

2. Programming

Arrays of strings in GFORTRAN errors

Hello I am using gfortran and I intended to do thiis: Module variables character(len=:), dimension(:), allocatable, array end module variables Sub test use variables integer (max_len) max_len=len_trim("something here") if(.not.allocated(array))... (1 Reply)
Discussion started by: pepe
1 Replies

3. Programming

question about int arrays and file pointer arrays

if i declare both but don't input any variables what values will the int array and file pointer array have on default, and if i want to reset any of the elements of both arrays to default, should i just set it to 0 or NULL or what? (1 Reply)
Discussion started by: omega666
1 Replies

4. Web Development

PHP arrays in arrays

PHP question... I have an SQL query that's pulled back user IDs as a set of columns. Rather than IDs, I want to use their names. So I have an array of columns $col with values 1,7,3,12 etc and I've got an array $person with values "Fred", "Bert", "Tom" etc So what I want to do is display the... (3 Replies)
Discussion started by: JerryHone
3 Replies

5. Shell Programming and Scripting

Unique value added

Guys, I am trying to get unique numbers which i can use as a primary key in a table. I tried using the <seconds_since_epoch>.<hostname> combination but since more than one process can run on the same machine at the same time, this won't be unique either. Can anyone tell me another way to do... (2 Replies)
Discussion started by: garric
2 Replies

6. UNIX for Dummies Questions & Answers

Major OS errors/Bash errors help!!!!

Hi all, dummy here.... I have major errors on entering the shell. On login I get: -bash: dircolors: command not found -bash: tr: command not found -bash: fgrep: command not found -bash: grep: command not found -bash: grep: command not found -bash: id: command not found -bash: [: =: unary... (12 Replies)
Discussion started by: wcmmlynn
12 Replies

7. Shell Programming and Scripting

New Person Added to the Forum

Hello, I'm brand new to this forum. I am working on my first Bash shell script. We were given an exercise to get ready for the real assignment. I could use some help. The exercise is to "set two variables (i. e., file1 & file2) on the command line to the paths of the text files. We are to... (1 Reply)
Discussion started by: wcarp05
1 Replies

8. AIX

Adapter Errors and Link Errors

$ errpt | more IDENTIFIER TIMESTAMP T C RESOURCE_NAME DESCRIPTION 3074FEB7 0802050205 T H fscsi1 ADAPTER ERROR B8113DD1 0802050205 T H fcs1 LINK ERROR B8113DD1 0802050205 T H fcs1 LINK ERROR 3074FEB7 0802050205 T H fscsi0 ADAPTER ERROR B8113DD1 ... (2 Replies)
Discussion started by: mcastill66
2 Replies

9. UNIX for Advanced & Expert Users

Adapter Errors and Link Errors

$ errpt | more IDENTIFIER TIMESTAMP T C RESOURCE_NAME DESCRIPTION 3074FEB7 0802050205 T H fscsi1 ADAPTER ERROR B8113DD1 0802050205 T H fcs1 LINK ERROR B8113DD1 0802050205 T H fcs1 LINK ERROR 3074FEB7 0802050205 T H fscsi0 ADAPTER ERROR B8113DD1 ... (0 Replies)
Discussion started by: mcastill66
0 Replies
Login or Register to Ask a Question