Hi Folks

I need help about a script.

Here are the detail of of Script.

I have total 3 files which have following records.

1. file1 -> contains all userids
2. file2 -> random userids, some are present and some are not.
3. file3 -> ls /home file, whever directories they have that is mentioned in file3.

Now my target is that.

I want to compare file3 records with file2, what userids are same, show them, and also show user which userids are not present in file3.

I also want to search file3 userids against file2, if records find then match those userids with file1.

I am doing this with array, startup thing done, but dont understand the point how to search records in array and achieve that task.

Below is my script ..............................................

#!/bin/bash

filename=p1
filename1=p2

declare -a array1
declare -a array2
declare -a array3

array1=( `cat "$filename"`) # Loads contents
array2=( `cat "$filename1"`) # Loads contents
array3=( `ls /home/`)

element_count1=${#array1[*]}
echo $element_count1

element_count2=${#array2[*]}
echo $element_count2

element_count3=${#array3[*]}
echo $element_count3
sleep 3

number_of_elements=${#array1[@]}
number_of_elements1=${#array2[@]}
number_of_elements2=${#array3[@]}

echo '- ARRAY-1--------------------------------'
#echo "Number of elements: 4" # Hard-coded for illustration.
for (( i = 0 ; i < number_of_elements ; i++ ))
do
echo "Element [$i]: ${array1[$i]}"
done


echo '- ARRAY-2-----------------------------------'
#echo "Number of elements: 4" # Hard-coded for illustration.
for (( i = 0 ; i < number_of_elements1 ; i++ ))

for (( i = 0 ; i < number_of_elements1 ; i++ ))
do
echo "Element [$i]: ${array2[$i]}"
done

echo '- LIST OF DIR ------------------------'
#echo "Number of elements: 4" # Hard-coded for illustration.
for (( i = 0 ; i < number_of_elements2 ; i++ ))
do
echo "Element [$i]: ${array3[$i]}"
done

I am waiting for your answer

Hey,

There's probably a better way, but off of the top of my head, you could cat file3 and run it in a while loop and just do string comparisons with an "

while read line
do
if [ "${array3[$i]}"" = "$line ]
then
do what you want
fi<${array2[@]}

It might be simpler to just read in the files instead of putting them in arrays and then reading those, also

Best wishes,

MIke

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.