LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-02-2010, 03:03 AM   #1
unihiekka
Member
 
Registered: Aug 2005
Distribution: SuSE Linux / Scientific Linux / [K|X]ubuntu
Posts: 273

Rep: Reputation: 32
Comparing arrays in bash


Hi, guys!

Is there a way to compare an array in a while conditions? I have one array that contains the results of some search and if the script has found all the items, then it should stop, so my idea is to have a while loop la:

Code:
while [seekarr -neq foundarr] do ... done
where foundarr=(1 1 1 ... 1). Probably you have a better idea, so I'm interested to hear it! Thanks.
 
Old 06-02-2010, 03:38 AM   #2
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
You could try storing the arrays in (small) text files (perhaps sorted) and use diff/cmp exit code.
Alternative could be a function, that loops over the foundarr entries to check if any are not found ("break" on first instance that is not found yet).

But maybe you could share some details on the search performed and what elements need to be found, because I think it may just be
easier to list all expected elements (in array, file,...), loop over those in a for-loop and then if (not found); do additional_search_to_update_results; done
Something like
Code:
allOK=0;
while (( $allOK == 0 )); do
 for i in expectedarr; do
  if (inSearchResults == 0)
   searchMissingItem; # append to search results if found
   break;
  fi;
 done;
 allOK=1;
done;
You should add a check on exit value of searchMissingItem, to see if the search finds the missing item, else it could turn into an infinite while-loop.
 
Old 06-02-2010, 04:03 AM   #3
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 88
Are the contents of the two arrays going to be identical, when complete (same elements in the same order)?

if so, using[*] as the array subscript will reference the entire array, so you could use:

while ! echo "${foundarr[*]}" | grep "^${seekarr[*}}$" >/dev/null; do...
 
Old 06-03-2010, 06:01 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
How about
Code:
[[ "${array1[@]}" = "${array2[@]}" ]] && echo same || echo different
It works on a few simple tests even finding array1[0]=a array1[1]=b array2[0]= array2[1]='a b' different.
 
Old 06-03-2010, 06:08 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by unihiekka View Post
Hi, guys!

Is there a way to compare an array in a while conditions? I have one array that contains the results of some search and if the script has found all the items, then it should stop, so my idea is to have a while loop la:

Code:
while [seekarr -neq foundarr] do ... done
where foundarr=(1 1 1 ... 1). Probably you have a better idea, so I'm interested to hear it! Thanks.
The syntax is wrong; should be
Code:
while [ <numeric expression> -ne <another numeric expression> ]
that is with a space after [ and before ] and using numeric comparison operator -ne.

If you want to compare all the elements of two arrays, assuming they have the same number of elements this would do it
Code:
for (( i=0; i<${#array1[*]}; i++ ))
do
    if [[ ${array1[i]} -ne ${array2[i]} ]]; then
       <whatever you want to do>
    fi
done
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash file comparing question. angeloban Programming 8 06-10-2009 12:51 PM
Bash Arrays Simon256 Programming 2 02-17-2009 01:39 PM
Comparing file sizes using a bash script. IanChristie Programming 5 12-19-2003 10:14 PM
bash - comparing a variable to several values davee Programming 3 05-05-2003 07:26 AM
Comparing Char arrays n_ick2000 Programming 5 02-25-2003 09:02 AM


All times are GMT -5. The time now is 08:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration