Count, compare, execute ...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Count, compare, execute ...
# 1  
Old 03-25-2013
Count, compare, execute ...

I'm working on a script that needs to execute only after directory B is equal in file count to directory A.

It needs to check dir B every 5 min or so and when the file count is equal to A, it executes a script.

I'm thinking of using wc -w but I'm not sure how to put it together.

Any help is greatly appreciated.
Thanks
# 2  
Old 03-25-2013
Show us your thoughts.
# 3  
Old 03-25-2013
While rsync's purpose is not counting files, it can do the trick by combining a few flags together and counting its results.

Don't panic because you see the word "deleting". We're using verbose output to our advantage but it won't delete anything at all (it only performs a dry-run).

Code:
if [ $(rsync -rnv --delete /dirA/ /dirB/ | awk '/deleting/ { countA++ }; !/deleting/ {countB++} END { print (countA)-(countB-4)}') -eq 0 ]; then
	echo "Count is the same, execute some script"
else
	echo "Count differs, nothing to do here"
fi

# 4  
Old 03-26-2013
Here's a rough idea so far

Here's what I was thinking but it's totally inoperable so far.

Code:
#Set path from user input
DPX_path="$1"

clear
echo "DPX path is" "$DPX_path"
ls "$DPX_path" | wc -w
dpx="ls "$DPX_path" | wc -w" # set count as variable for comparison
echo "dpx variable is set to " $dpx

echo
echo CDL path is "$DPX_path"_CDL
ls "$DPX_path"_CDL | wc -w
cdl="ls "$DPX_path"_CDL | wc -w" # set count as variable for comparison
echo "cdl variable is set to " $cdl


x=0
while [ $x = 0 ]
do
    echo "Checking for match."
    
        if $cdl=$dpx
            echo "Match!"
            # Execute script
            exit
            x=1
        else

            echo "No match.  Check again in 5 min."
            sleep 300
            # loop back and rerun script
        fi
done

Maybe this will give someone who knows bash well a good idea of what I need to do.

Thanks.

---------- Post updated at 11:18 AM ---------- Previous update was at 10:23 AM ----------

The directories that need to be compared will have the same base path.
/X/Y/DPX/
/X/Y/DPX_CDL/

The CDL directory will always be the exact same name as the DPX directory with the _CDL added as indicated above.

My script takes the given path of DPX and creates the CDL path from that.

Thanks
# 5  
Old 03-27-2013
I think you not that far off.
wc -w will not work correctly if file names contain e.g. white space chars; wc -lshould work better in that case.
Don't use double quotes to assign command output to variables; use "command substitution" $(...) instead.
And, put the second directory check inside the while loop, so it gets fresh data every 5 minutes...
Post your modifications for discussion.
# 6  
Old 03-29-2013
I need some help. I don't understand what you mean by, "Don't use double quotes to assign command output to variables; use "command substitution" $(...) instead." I've never done that whatever it is but I'd like to try it.
Is that referring to the line:
Code:
dpx="ls "$DPX_path" | wc -w"

Should it be:
Code:
dpx=ls "$DPX_path" | wc -w

I'm trying to work out the while loop for the second check.

---------- Post updated at 05:54 PM ---------- Previous update was at 05:51 PM ----------

Code:
#Set path from user input
DPX_path="$1"

clear
echo "DPX path is" "$DPX_path"
ls "$DPX_path" | wc -w
dpx=ls "$DPX_path" | wc -l # set count as variable for comparison
echo "dpx variable is set to " "$dpx"

echo
echo CDL path is "$DPX_path"_CDL

            t=0
            while [ t = 0 ]
            do
                ls "$DPX_path"_CDL | wc -w
                cdl=ls "$DPX_path"_CDL | wc -w # set count as variable for comparison
                echo "cdl variable is set to " "$cdl"
                t=1
            done

x=0
while [ $x = 0 ]
do
    echo "Checking for match."
    
        if $cdl=$dpx
            echo "Match!"
            # Execute script
            exit
            x=1
        else

            echo "No match.  Check again in 5 min."
            sleep 300
            # loop back and rerun script
        fi
done


Last edited by Franklin52; 03-30-2013 at 11:27 AM.. Reason: Code tags
# 7  
Old 03-30-2013
a) read man pages! Or, any search engine will spit out zillions of results for "command substitution".
b) DON'T use wc -c!
c) dpx=$(ls -1 "$DPX_path" | wc -l)
d) dummy code:
Code:
dpx=something1
cdl=something2
while [ $dpx -ne $cdl ]
  do sleep 300
     cdl=something2
  done
execute script

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to compare files and count

I am trying to compare 2 files and output the results in two files. The lines that are the same output to concordant.txt while the lines that do not match output to discordant.txt. Is there a way to count the lines after specific text (#CHROM POS ID REF ALT QUAL FILTER INFO FORMAT E1) in each... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

Compare two files and count number of matching lines

Dear All, I would like to compare two files and return the number of matches found. Example File A Lx2 L1_Mus1 L1Md_T Lx5 L1M2 L1_Mus3 Lx3_Mus Lx9 Lx2A L1Md_A L1Md_F2 File B L1_Mus3 L1_Mus3 (3 Replies)
Discussion started by: paolo.kunder
3 Replies

3. Shell Programming and Scripting

Compare file1 header count with file2 line count

What I'm trying to accomplish. I receive a Header and Detail file for daily processing. The detail file comes first which holds data, the header is a receipt of the detail file and has the detail files record count. Before processing the detail file I would like to put a wrapper around another... (4 Replies)
Discussion started by: pone2332
4 Replies

4. Shell Programming and Scripting

Script to compare count of two csv files

Hi Guys, I need to write a script to compare the count of two csv files each having 5 columns. Everyday a csv file is recived. Now we need to compare the count of todays csv file with yesterday's csv file and if the total count of records is same in todays csv file and yesterday csv file out... (3 Replies)
Discussion started by: Vivekit82
3 Replies

5. Shell Programming and Scripting

Compare values for a pattern match and execute script

Here in the input file 23:59:13,devicename,21,server1,700 23:59:13,devicename,22,server2,200 23:59:13,devicename,23,server3,200 23:59:13,devicename,24,server4,200 23:59:13,devicename,25,server5,200 23:59:13,devicename,26,server6,200 23:59:13,devicename,27,server7,200... (6 Replies)
Discussion started by: necro98
6 Replies

6. Shell Programming and Scripting

compare 2 files and show count same content.

$ cat File1 Non HTTP response code:java.net.ConnectException225073X 000000005143329Load time: 402335410224 Non HTTP response code: ava.net.ConnectException206423X 000000005143330Load time: 402305687161 Non HTTP response code: ava.net.ConnectException290212X 000000005143331Load time:... (1 Reply)
Discussion started by: ooilinlove
1 Replies

7. Shell Programming and Scripting

Compare and Count...

Hello all.. i am new to shell scripting.. learning the things..i am struck at a place here.. I have divided one table in two two tables for comparision.. X1|Left|10000|10025 X2|Left|1050|1075 X2|Left|3050|3075 X3|Left|9000|9025 Y1|Left|3000|3025 Y1|Left|4500|4525 Y2|Left|7000|7025... (4 Replies)
Discussion started by: empyrean
4 Replies

8. Shell Programming and Scripting

How to execute a no of SELECT COUNT(*) statements using a loop

HI Unix Gurus, I have a number of SELECT count(*) statements in an input file and I want to execute it using a shell script but one by one using loop in script.... How can I do this..... (7 Replies)
Discussion started by: ustechie
7 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

Compare lines and execute

Greetings, I'm new to scripting and need a little help. I have a NAS server and want to make a script that bans IP if they fail login several times. The problem is that I dont know how to compare the IP's in the file. If they exist more than 3 times do .... #!/bin/sh set -x X=4 rm... (2 Replies)
Discussion started by: ntenz
2 Replies
Login or Register to Ask a Question