Synchronisation of 2 arrays


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Synchronisation of 2 arrays
# 1  
Old 05-08-2012
Synchronisation of 2 arrays

If the element found in array2 doesn't exist in array1 i want i to be deleted. This code doesn't work correctly. What's wrong? Is there a simpler solution?


Code:
for (( i=0; i<=${#array2[*]}; i++ ))
do
	for (( v=0; v<=${#array1[*]}; v++ ))
	do
		if [ "{$array2[i]}" = "{$array1[v]}" ]
		then 
			count=1
			break
		fi
	done	

	if [ $count -ne 1 ] 
	then
		rm $MI/${array2[i]}
	fi				
done

# 2  
Old 05-08-2012
Use:
Code:
if [ "${array2[$i]}" = "${array1[$v]}" ]

Don't forget to initialize de count at the beginning of the first for.
This User Gave Thanks to Klashxx For This Post:
# 3  
Old 05-08-2012
thx it works now. How would you solve this btw?

Code:
for (( i=0; i<=${#array2[*]}; i++ ))
do
	for (( v=0; v<=${#array1[*]}; v++ ))
	do
		count=0

		if [ "{$array2[$i]}" = "{$array1[$v]}" ]
		then 
			count=1
			break
		fi
	done	

	if [ $count -ne 1 ] 
	then
		rm $MI/${array2[i]}
	fi				
done

# 4  
Old 05-08-2012
Use:
Code:
#cat test.sh
#!/bin/bash
array1[0]=a
array1[1]=b
array2[0]=c
array2[1]=b
array2[2]=z
i=1
v=2

for (( i=0; i<${#array2[*]}; ++i ))
do
        count=0
        for (( v=0; v<${#array1[*]}; ++v ))
        do
                if [ "${array2[$i]}." = "${array1[$v]}." ]
                then 
                        count=1
                        break
                fi
        done
        if [ $count -ne 1 ] 
        then
                echo "deleting array2 index $i: ${array2[$i]}"
        fi

done

Code:
#./test.sh                                                                                                                                                         
deleting array2 index 0: c
deleting array2 index 2: z

Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Solaris

"synchronisation lost" errors for Solaris NTP server

Hi, This is Solaris 9, which is service as NTP server for many unix clients. At backend, it it synching time with three GPS clocks. From past few days, I am noticing time reset to 1 second. Is this a problem ? I was assuming that if it is a network issue or GPS clock connectivity issue, it... (14 Replies)
Discussion started by: solaris_1977
14 Replies

2. Programming

Thread synchronisation

hi, i have to do a program with following condition. please help me to write the program. conditions-i have to create a thread with handle called first and it should call the member function(may do anything lik print anything) of a class called thread1 and for example let take that this first... (5 Replies)
Discussion started by: senthil.march
5 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. Shell Programming and Scripting

rsync script for synchronisation and backup

hello, i need to modified my synch/back scripts.... i want that this script only syncro folders in destinationfolder. f.e. when in destination are two folders 1) admin 2) users but in SOURCE are three: 1) admin 2) users 3) antivirus the script should only increnmential sync the... (0 Replies)
Discussion started by: onkeldave
0 Replies

5. UNIX for Dummies Questions & Answers

Need help on file synchronisation in unix

i want to do file synchronisation...its a client-server model..have to do system call 'ls -l' in both client and server ..the server has to keeep track of client files and have to keep a back up of client files..and tracking must be done based on time stamp.. suppose if client contains a file... (2 Replies)
Discussion started by: shilpam,edappal
2 Replies

6. UNIX for Advanced & Expert Users

Thread synchronisation problem...

Hello, hope my english will be sufficient to be clear enough... I'm in progress on some script that should copy one big source file (200GB average) on one sata drive, to multiple (30+) sata drives. Hardware is not the problem but copy performance is... If i launch all copy process at the same... (4 Replies)
Discussion started by: Gnaag
4 Replies

7. Shell Programming and Scripting

sh : URGENT synchronisation insmod in script

Hello, By now in linux 2.4, I have a sh script wich start 2 modules as follow : /sbin/insmod module1.o /sbin/insmod module2.o I added an application in user space named "user_app" which communicate with module1 with a /proc. I now tape the commands myself during code execution on a... (1 Reply)
Discussion started by: crip01
1 Replies

8. UNIX for Advanced & Expert Users

shared memory synchronisation

hello everybody i want to do synchronisation to access a shared memory bu i don't know too much how well i know that i should use semaphore have you any example of synchronisation of a shared memory by use of semaphore because i haven't find any thanks (0 Replies)
Discussion started by: student00
0 Replies
Login or Register to Ask a Question