LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 01-21-2010, 11:34 PM   #1
buee
Member
 
Registered: May 2009
Posts: 81

Rep: Reputation: 16
Angry 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?

Last edited by buee; 01-21-2010 at 11:55 PM.
 
Old 01-22-2010, 01:18 AM   #2
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
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.

Last edited by chrism01; 01-22-2010 at 01:41 AM. Reason: Note re wildcards use
 
Old 01-22-2010, 01:47 AM   #3
buee
Member
 
Registered: May 2009
Posts: 81

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by chrism01 View Post
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.
 
Old 01-22-2010, 03:02 AM   #4
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

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


Reply

Tags
bash, if, script, true


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to increment the date in a file name via bash script tensigh Programming 10 07-23-2010 04:49 AM
find values and increment akelder Programming 3 07-16-2009 04:30 AM
how to auto increment 13 digit hexadecimal using script hchoonbeng Linux - Newbie 1 09-25-2008 12:03 AM
increment in hex snutz411 Linux - Newbie 2 11-01-2005 06:54 PM
how to find the pid of a perl script from shell script toovato Linux - General 1 12-19-2003 07:25 PM


All times are GMT -5. The time now is 04:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration