Array compare bash script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Array compare bash script
# 1  
Old 07-04-2017
Array compare bash script

Hello,

i have a script that should compare between ${ARRAY[@]}
that contains all fstab record like this :
Code:
>>echo ${ARRAY[@]}
/ /boot

between all mountpoints in my df that is stord in ${ARRAY2[@]}
Code:
>>echo ${ARRAY2[@]}
/ /boot /dev/shm /var/spool/asterisk/monitor

now i have this loop:
Code:
for i in ${ARRAY[@]}; do
	for j in ${ARRAY2[@]}; do
	if [[ $i != $j ]];then
		echo "fstab $i doesnt exsist in $j df"
		exit 
		fi
		done
	done

what happend now is that for exsample ${ARRAY[@]} try to compare first value "/" to all values in ${ARRAY2[@]} like this :

Code:
[ / != / ]]
[[ / != /boot ]]

and then the script fails - my point here is to find if i have records in fstab
that are not mounted on df

i want that when ${ARRAY[@]} found a match in ${ARRAY2[@]}
it will continue to next value and not try to match the first value to all ${ARRAY2[@]} values.

thanks

Last edited by batchenr; 07-04-2017 at 07:20 AM.. Reason: code tags not Quote... added icodes
# 2  
Old 07-04-2017
Hmm. Your problem is, I think, df output (a line) does not always exactly match fstab lines. The data needs to be reformatted. Before you compare.

So, your compare loop has problems trying to compare things. Can you please show us how you got values into ARRAY1 and ARRAY2 to begin with?

PS: awk can do what you want in a single line - find excluded stings from one array to another.
# 3  
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
# 4  
Old 07-04-2017
Quote:
Originally Posted by jim mcnamara
Hmm. Your problem is, I think, df output (a line) does not always exactly match fstab lines. The data needs to be reformatted. Before you compare.

So, your compare loop has problems trying to compare things. Can you please show us how you got values into ARRAY1 and ARRAY2 to begin with?

PS: awk can do what you want in a single line - find excluded stings from one array to another.
i dont want to count on the same order i want it indevidualy to check
every val in Array compare to array2

this is the values:

Code:
ARRAY_FILES=$(df -Pk |awk '{print$6}' | grep -v Mounted | xargs)
ARRAY=($ARRAY_FILES)
FSTAB_ARRAY=$(cat /etc/fstab  | awk '{print$1,$2}' | sed '/^#.*/d'| grep -v "devpts\|sysfs\|proc" | awk '{print$2}' | grep ^/|xargs)
ARRAY2=($FSTAB_ARRAY)

---------- Post updated at 09:51 AM ---------- Previous update was at 09:49 AM ----------

Quote:
Originally Posted by MadeInGermany
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[@]}
if [ $max -ne $max2 ]
then
  echo "different no of elements, let's find out"
  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]}"
        i=$((i+1)) j=$((j+1))
      fi
    else
      i=$((i+1)) j=$((j+1))
    fi
  done
fi

Hey-
thanks but you script returns this :

Code:
different no of elements, let's find out
df /boot doesnt exist in fstab /dev/shm
mismatch fstab /boot and df /var/spool/asterisk/monitor

it compare the worng files as theres no order of how fstab presents and df


now, i have managed to make it work but like this :

Code:
echo ${ARRAY[@]} > $FILE

for i in  ${ARRAY2[@]} 
        do
        grep $i $FILE
        if [[ $? -ne 0 ]]
                then
                echo "$i is missing in df "
                exit    
                fi
        done

and$FILE containd df values

Last edited by batchenr; 07-04-2017 at 11:59 AM..
# 5  
Old 07-04-2017
IFS includes a newline, so you do not need xargs to convert newline to a space character.
My script sample needs a sorted input.
For example
Code:
ARRAY2=($(df -Pk | awk '$6~/^[/]/{print$6}' | sort)) # df
ARRAY=($(awk '/^[^#]/ && $2~/[/]/ && $3!~/devpts|sysfs|proc/{print $2}' /etc/fstab | sort)) # fstab

BTW df does unneeded stat() and statfs(), a mount -v would be less overhead.

---------- Post updated at 10:17 ---------- Previous update was at 10:14 ----------

Please avoid unneeded cat! If you want to have /etc/fstab first in the line then do
Code:
ARRAY=($( </etc/fstab awk '/^[^#]/ && $2~/[/]/ && $3!~/devpts|sysfs|proc/{print $2}' | sort)) # fstab

This User Gave Thanks to MadeInGermany For This Post:
# 6  
Old 07-05-2017
Quote:
Originally Posted by MadeInGermany
IFS includes a newline, so you do not need xargs to convert newline to a space character.
My script sample needs a sorted input.
For example
Code:
ARRAY2=($(df -Pk | awk '$6~/^[/]/{print$6}' | sort)) # df
ARRAY=($(awk '/^[^#]/ && $2~/[/]/ && $3!~/devpts|sysfs|proc/{print $2}' /etc/fstab | sort)) # fstab

BTW df does unneeded stat() and statfs(), a mount -v would be less overhead.

---------- Post updated at 10:17 ---------- Previous update was at 10:14 ----------

Please avoid unneeded cat! If you want to have /etc/fstab first in the line then do
Code:
ARRAY=($( </etc/fstab awk '/^[^#]/ && $2~/[/]/ && $3!~/devpts|sysfs|proc/{print $2}' | sort)) # fstab

Thanks you the array worked and now ur loop also.
btw - the reason i dont compare it to mount -v is cause the system thinks that the nfs is mounted but its not and only throw df i can see it :


df :
Code:
df: `/var/spool/asterisk/monitor': Stale file handle
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2        39G  9.0G   28G  25% /
tmpfs           1.9G     0  1.9G   0% /dev/shm
/dev/sda1       283M   27M  242M  10% /boot

Code:
/dev/sda2 on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
/dev/sda1 on /boot type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
192.168.223.22:/mnt/pbxarchive/PBXIL-Managment on /var/spool/asterisk/monitor type nfs (rw,vers=4,addr=192.168.223.22,clientaddr=192.168.11.100)

see that /var/spool/asterisk/monitor show like its mounted but its not also
the file looks like that :
Code:
d?????????? ? ?        ?           ?            ? monitor

and df output gives this error :
Code:
df: `/var/spool/asterisk/monitor': Stale file handle

thats why our last script that checked /proc/mounts didnt alerted us.

anyway- thank you so much for your time!

---------- Post updated at 08:03 AM ---------- Previous update was at 03:21 AM ----------

well if anyone will look it up
i have endded up doing this loop :

Code:
for i in $(</etc/fstab awk '/^[^#]/ && $2~/[/]/ && $3!~/devpts|sysfs|proc/{print $2}' | sort)
        do
        df 2> /dev/null |grep -w $i > /dev/null
                if [[ $? -ne 0 ]]
                        then
                        NOT_MOUNTED="$i $NOT_MOUNTED"
                        echo "$NOT_MOUNTED is NOT mounted!"
                        exit
                        fi
                done

Thnaks all
# 7  
Old 07-05-2017
I have updated my post#3 - the original code did not report mismatches-only.
Your short script only checks in one direction.
You can run the df on the desired target only:
Code:
for i in ...
do
  if df "$i" 2>/dev/null >&2
  then
    ...
  fi
done

Or without stat() and statfs()
Code:
for i in ...
do
  if </etc/mtab awk -v fs="$i" '$2==fs {exit 1}'
  then
    ...
  fi
done

--
Regarding your error "Stale NFS handle" is correct, the printing of the many ???? on standard output is a Linux bug IMHO.
The server revoked its NFS share. Maybe it reshared with a new NFS handle. Try to remount it
Code:
umount /var/spool/asterisk/monitor
mount /var/spool/asterisk/monitor


Last edited by MadeInGermany; 07-05-2017 at 01:23 PM..
This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question