LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Trying find and increment script (https://www.linuxquestions.org/questions/linux-newbie-8/trying-find-and-increment-script-783915/)

buee 01-21-2010 10:34 PM

Trying find and increment script
 
I am working on a script that will search a directory for certain file names and increment a variable by one for each result returned. Each time I search this file name, the file found will also be deleted. I would like this script to be run about every 6 hours or so. We'll call this script A.

I also have a script that runs daily that I would like to tack the count to the end of the text file that it outputs. We'll call this script B. I'm confident that I would want to store the count from script A to a text file to be called in by script B, but don't know how to do so.

Here's what I have so far:

Code:

#!/bin/bash
SD=0        #Resetting variable count for testing purposes, removed this before release
#Below, Smith is used for testing purposes as this is the newest backup with instances found
find /cust_backups/Smith -name *Modified\ Script* && SD=$[SD+1] ;
find /cust_backups/Smith -name *Bitdefender\ Online\ Scanner* && SD=$[SD+1] ;
find /cust_backups/Smith -name *ccleaner.exe* && SD=$[SD+1] ;
find /cust_backups/Smith -name *HJT.exe* && SD=$[SD+1] ;
find /cust_backups/Smith -name *KillBox.exe* && SD=$[SD+1] ;
find /cust_backups/Smith -name *MBAM.exe* && SD=$[SD+1] ;
find /cust_backups/Smith -name *MSE\ V32.exe* && SD=$[SD+1] ;
find /cust_backups/Smith -name *MSE\ V64.exe* && SD=$[SD+1] ;
find /cust_backups/Smith -name *MSE\ XP.exe* && SD=$[SD+1] ;
find /cust_backups/Smith -name *SAS.exe* && SD=$[SD+1] ;
find /cust_backups/Smith -name *spybotsd.exe* && SD=$[SD+1] ;
find /cust_backups/Smith -name *spybotsd_includes.exe* && SD=$[SD+1] ;
find /cust_backups/Smith -name *Utility\ Batch* && SD=$[SD+1] ;
find /cust_backups/Smith -name *Windows\ Update* && SD=$[SD+1] ;
echo "$SD"

A couple of things here, the 2nd line variable, that's just for testing purposes. In the end result, I will not have the variable reset each time the script runs. It will be a year to date count if you will. Also, the echo at the end is for testing purposes. I abbreviated the search scope to one folder under /cust_backups but in the end, it will search the entire folder of /cust_backups for the file names in question.

Now, for those of you that know what you're doing, you're already shaking your head because you know that, no matter what, at the end of the script $SD will end up being 14. The problem is I don't know how to tell bash that if there is a result returned, increment by one. If there is no result returned, move to the next command.

Beyond that, I'll need to know how to call the text of a file in to a variable so I can plug it in to the text output of script B.

Any tips?

chrism01 01-22-2010 12:18 AM

From this page http://tldp.org/LDP/abs/html/ops.html
Quote:

n=$[ $n + 1 ]
# Works even if "n" was initialized as a string.
#* Avoid this type of construct, since it is obsolete and nonportable.
NB: you don't have reqd spaces around the ops anyway.

Use
Quote:

(( n = n + 1 ))

Grab a value from a file
Code:

num=$(cat file)
Oh, BTW, put single quotes around those filenames so 'find' deals with the '*'s , not the current shell.

buee 01-22-2010 12:47 AM

Quote:

Originally Posted by chrism01 (Post 3836057)
From this page http://tldp.org/LDP/abs/html/ops.html

NB: you don't have reqd spaces around the ops anyway.

Use



Grab a value from a file
Code:

num=$(cat file)
Oh, BTW, put single quotes around those filenames so 'find' deals with the '*'s , not the current shell.

Thanks for the reply. Problem is I can't test these because I can't get just a number to output to anything. For testing, I did this abbreviated script:

Code:

#!/bin/bash
linecount=$(wc -l /opt/precount.txt)
find /cust_backups/Murillo -name *Utility\ Batch* > /opt/precount.txt;
chmod 777 /opt/precount.txt
$linecount
(( test1 = linecount + 69 ))
echo $linecount $test1

However, wc -l /opt/precount.txt outputs the line count, a space, then the path. I've spent the last 20 minutes looking over awk and sed commands to remove the last x amount of characters in a file, or specific strings, but everyone's replies are suited to the OPs request and no one explains the commands. To me, awk and sed commands looks like someone sneezed while typing really fast.

chrism01 01-22-2010 02:02 AM

Code:

wc -l test.pl|awk '{print $1}'
returns only the line cnt, not filename
http://www.ibm.com/developerworks/library/l-awk1.html


All times are GMT -5. The time now is 11:31 PM.