Sponsored Content
Full Discussion: Array compare bash script
Top Forums Shell Programming and Scripting Array compare bash script Post 303000101 by MadeInGermany on Tuesday 4th of July 2017 09:08:26 AM
Old 07-04-2017
If the array elements are unique and in the same order then you only need one loop.
Example:
Code:
ARRAY=(/ /boot /var /usr) # fstab
ARRAY2=(/ /xboot /var) # df
max=${#ARRAY[@]}
max2=${#ARRAY2[@]}
i=0 j=0
while [ $i -lt $max ] || [ $j -lt $max2 ]
do
  if [[ "${ARRAY[$i]}" != "${ARRAY2[$j]}" ]]
  then
    if [[ "${ARRAY[$i]}" = "${ARRAY2[$((j+1))]}" ]]
    then
      echo "df ${ARRAY2[$j]} doesnt exist in fstab ${ARRAY[$i]}"
      j=$((j+1))
    elif [[ "${ARRAY[$((i+1))]}" = "${ARRAY2[$j]}" ]]
    then
      echo "fstab ${ARRAY[$i]} doesnt exist in df ${ARRAY2[$j]}"
      i=$((i+1))
    else
      echo "mismatch fstab ${ARRAY[$i]} and df ${ARRAY2[$j]}"
    fi
  fi
  i=$((i+1)) j=$((j+1))
done


Last edited by MadeInGermany; 07-05-2017 at 12:45 PM.. Reason: simplification, now also reports mismatches only
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Use Perl In Bash Script To Compare Floationg Points

Is there a way to compare two floating points numbers in a bash script using perl? I've tried just using a bash if statement and it doesn't seem to support floating point numbers. Can the perl line read vars from bash then output a var to bash? a=1.1 #from bash b=1.5 #from bash if... (3 Replies)
Discussion started by: Grizzly
3 Replies

2. Shell Programming and Scripting

count and compare no of records in bash shell script.

consider this as a csv file. H,0002,0002,20100218,17.25,P,barani D,1,2,3,4,5,6,7,8,9,10,11 D,1,2,3,4,5,6,7,8,9,10,11 D,1,2,3,4,5,6,7,8,9,10,11 D,1,2,3,4,5,6,7,8,9,10,11 D,1,2,3,4,5,6,7,8,9,10,11 T,5 N i want to read the csv file and count the number of rows that start with D and... (11 Replies)
Discussion started by: barani75
11 Replies

3. Shell Programming and Scripting

How to compare time in bash script?

Hi, Anyone know how to compare the time in bash script? I want to compare say 30 min. to 45 min. ( AIX ) Thanks. (1 Reply)
Discussion started by: sumit30
1 Replies

4. Shell Programming and Scripting

Bash script to compare two lists

Hi, I do little bash scripting so sorry for my ignorance. How do I compare if the two variable not match and if they do not match run a command. I was thinking a for loop but then I need another for loop for the 2nd list and I do not think that would work as in the real world there could... (2 Replies)
Discussion started by: GermanJulian
2 Replies

5. Shell Programming and Scripting

Compare file to array, replace with corresponding second array

ok, so here is the issue, I have 2 arrays. I need to be able to create a loop that will find ${ARRAY1 in the text doc, and replace it with ${ARRAY2 then write the results. I already have that working. The problem is, I need it to do that same result across however many items are in the 2... (2 Replies)
Discussion started by: gentlefury
2 Replies

6. Shell Programming and Scripting

Compare & Copy Directories : Bash Script Help

Beginner/Intermediate shell; comfortable in the command line. I have been looking for a solution to a backup problem. I need to compare Directory 1 to Directory 2 and copy all modified or new files/directories from Directory 1 to Directory 3. I need the directory and file structure to be... (4 Replies)
Discussion started by: Rod
4 Replies

7. Shell Programming and Scripting

Bash script to compare 2 file

Hello Friends please help me to create script to compare 2 fiile which has rpm info . File 1: glibc-2.12.1.149.el6_6.5.x86_64.rpm glibc-common-2.12-1.149.el6_6.5.x86_64.rpm File 2 : glibc-2.12.123.el6_6.5.x86_64.rpm glibc-common-2.12-123.el6_6.5.x86_64.rpm To compare file1... (1 Reply)
Discussion started by: rnary
1 Replies

8. UNIX for Beginners Questions & Answers

Array problem in Bash Script

I am trying to write a Bash Script using a couple of arrays. I need to perform a countdown of sorts on an array done once daily, but each day would start with the numbers from the previous day. This is what I'm starting with : #!/bin/bash days=(9 8 7 6 5) for (( i = 0 ; i < ${#days} ; i++... (4 Replies)
Discussion started by: cogiz
4 Replies

9. UNIX for Beginners Questions & Answers

Bash script to compare file all the files exits or not

Currently i am building a script like based on region parameter it will filter the records in config file and then it will create a text file like ab.txt and it will read the path location in that file and now i need to compare the files name in the config file to files in the path of the config... (1 Reply)
Discussion started by: saranath
1 Replies

10. UNIX for Beginners Questions & Answers

Compare date bash script

I all I have written a bash script for compare two date. One of those is a result of query, and another is current date. I have a problem with the format, because the first is 09/12/19 18:50:30 but for having this result I have to do d1DB=$(date -d "$valData" +'%m/%d/%y %T') and the second... (9 Replies)
Discussion started by: rdie77
9 Replies
Mail::Box::Tie::ARRAY(3pm)				User Contributed Perl Documentation				Mail::Box::Tie::ARRAY(3pm)

NAME
Mail::Box::Tie::ARRAY - access an existing message folder as array SYNOPSIS
use Mail::Box::Manager; my $mgr = Mail::Box::Manager->new; my $folder = $mgr->open(folder => 'inbox'); use Mail::Box::Tie::ARRAY; tie my(@inbox), 'Mail::Box::Tie::ARRAY', $folder; # deprecated, but works too use Mail::Box::Tie; tie my(@inbox), 'Mail::Box::Tie', $folder; foreach (@inbox) {print $_->short} print $_->print foreach @inbox; my $emails = @inbox; print $inbox[3]; print scalar @inbox; push @inbox, Mail::Box::Message->new(...); delete $inbox[6]; print $inbox[0]->head->get('status'); my $folder = tied @inbox; untie @inbox; DESCRIPTION
Certainly when you look at a folder as a list of messages, it is logical to access the folder through an array. Not all operations on arrays are supported. Actually, most functions which would reduce the size of the array are modified instead to mark messages for deletion. Examples what you cannot do: shift/unshift/pop/splice @inbox; METHODS
Constructors TIEARRAY('Mail::Box::Tie::ARRAY', FOLDER) Create the tie on an existing folder. example: tie an array to a folder my $mgr = Mail::Box::Manager->new; my $inbox = $mgr->new(folder => $ENV{MAIL}); tie my(@inbox), 'Mail::Box::Tie::Array', ref $inbox, $inbox; Tied Interface $obj->DELETE() Flag a message to be removed. Be warned that the message stays in the folder, and is not removed before the folder is written. example: delete $inbox[5]; $inbox[5]->delete; #same $obj->FETCH(INDEX) Get the message which is at the indicated location in the list of messages contained in this folder. Deleted messages will be returned as "undef". example: print $inbox[3]; # 4th message in the folder print @inbox[3,0]; # 4th and first of the folder print $inbox[-1]; # last message $obj->FETCHSIZE() Return the total number of messages in a folder. This is called when the folder-array is used in scalar context, for instance. example: if(@inbox > 10) # contains more than 10 messages? my $nrmsgs = @inbox; $obj->PUSH(MESSAGES) Add MESSAGES to the end of the folder. example: push @inbox, $newmsg; $obj->STORE(INDEX, MESSAGE) Random message replacement is not permitted --doing so would disturb threads etc. An error occurs if you try to do this. The only thing which is allowed is to store a message at the first free index at the end of the folder (which is also achievable with PUSH()). example: $inbox[8] = $add; $inbox[-1] = $add; push @inbox, $add; $obj->STORESIZE(LENGTH) Sets all messages behind from LENGTH to the end of folder to be deleted. DETAILS
Folder tied as array Limitations This module implements "TIEARRAY", "FETCH", "STORE", "FETCHSIZE", "STORESIZE", "DELETE", "PUSH", and "DESTROY". This module does not implement all other methods as described in the Tie::Array documentation, because the real array of messages is not permitted to shrink or be mutilated. SEE ALSO
This module is part of Mail-Box distribution version 2.105, built on May 07, 2012. Website: http://perl.overmeer.net/mailbox/ LICENSE
Copyrights 2001-2012 by [Mark Overmeer]. For other contributors see ChangeLog. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See http://www.perl.com/perl/misc/Artistic.html perl v5.14.2 2012-05-07 Mail::Box::Tie::ARRAY(3pm)
All times are GMT -4. The time now is 12:59 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy