How do I compare two arrays in bash?
Hi All!
I am writing a script to get the multiples of 2 and 3, place them in an 2 arrays, and then show the common integers. So far everything works fine till the comparision. I don't know how to compare them. Here is the code: Code:
#!/bin/bash |
So I would probably like to back up a little and ask what the?
Sorry ... not trying to be rude but you are creating arrays by first creating strings and then creating arrays from that ... why? Why not just put the values straight into the arrays? Code:
num1=2 |
sorry i didnt know how to put it into the array right away. I also dont know what you mean by:
Quote:
|
loop inside a loop, with for loops.
Here's one way, with two `for` loops:
Code:
# loop in a loop (two for loops): |
Code:
awk 'BEGIN{ |
sorry ... mine was a typo, there should be no space between 'be' and 'st' so yes 'best' option is to use a loop of which by the way 'if' is not a loop.
Any of the others can work depending on your choice, but the for loop as shown by GrapefruiTgirl would be my choice. I would offer some slight adjustments: Code:
for xx in ${!multiples2[*]}; do 1. ${!array[*]} - * apparently runs quicker and ! will give you indices that have been assigned 2. [index] - variables used as indices do not require $ to be expanded 3. (( )) - doing number testing should be done using these parentheses |
thanx
|
All times are GMT -5. The time now is 02:28 AM. |