Solved

Help with script (rm command)

Posted on 2007-04-01
7
1,467 Views
Last Modified: 2013-12-27
The following script will remove log files that were last modified yesterday:

DATE=`TZ=GMT+24 date`
DATE="`echo ${DATE} | awk '{print $2}'` `echo ${DATE} | awk '{print $3}'`"
rm `ls -ltr ${LOG_PATH}/*.log | grep "${DATE}" |  awk '{print $9}'`

That works fine if there are indeed files modified yesterday that can be deleted. If there are no files to be deleted, the rm command will show the usage statement. How can I add the check before applying the rm command? Thanks.
0
Comment
Question by:integrosys
7 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 18834654
touch -t`TZ=GMT+24 date +%Y%m%d`0000 yesterday
touch -t`TZ=GMT+00 date +%Y%m%d`0000 today
find  . -newer yesterday \! -newer today -exec rm {} \;
0
 

Author Comment

by:integrosys
ID: 18834663
I don't really understand your solution. Is it possible to modify my script a bit to get what I want instead of giving me a totally new solution? Thanks.
0
 
LVL 84

Accepted Solution

by:
ozo earned 20 total points
ID: 18834681
rm `ls -ltr ${LOG_PATH}/*.log | grep "${DATE}" |  awk '{print $9}'` 2> /dev/null
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:integrosys
ID: 18834698
Hi,

That looks fine. But suppose that there are indeed files to delete, but somehow the rm command failed (maybe due to file permission), how would I be able to catch this condition and return an error code from the script? In short, this is what I want:

if (there are files to delete) {
  rm files
  if (rm command failed) {
    exit 1
  }
}
exit 0
0
 
LVL 7

Assisted Solution

by:nixfreak
nixfreak earned 20 total points
ID: 18838592
Replace the below line:
rm `ls -ltr ${LOG_PATH}/*.log | grep "${DATE}" |  awk '{print $9}'`

with:
FILES=`ls -ltr ${LOG_PATH}/*.log | grep "${DATE}" |  awk '{print $9}'`
[ -z "$FILES" ] && exit 1
rm $FILES
0
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 20 total points
ID: 18839800
Just use the

rm -f ......

to stop rm displaying a usage message if no files exist.
0
 

Author Comment

by:integrosys
ID: 18841587
Thanks. All your solutions are useful to me.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
zeroFront challenge 7 114
What is the meaning of mount pont? with respect to windows file server. 7 64
Not needed 13 120
Authenticate using sesu from script 7 60
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question