shell script to remove old files and write to a log file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting shell script to remove old files and write to a log file
# 1  
Old 05-20-2008
shell script to remove old files and write to a log file

Hi,

I have a script that works on a unix box but am trying to get it working on a linux box that uses shell. I am not a programmer so this is proving harder than I imagined. I made some changes and ended up with the script below but when I run it I get the following messages. Any help would be great.

messages
./cleanoldbackups.sh: line 18: syntax error near unexpected token `fi'
./cleanoldbackups.sh: line 18: `fi '


script
# -----------------------------------------------------------------------
2>&1
# -----------------------------------------------------------------------
# Check run type - r report, d delete
# -----------------------------------------------------------------------
set RUN_TYPE= $1
if [! [$RUN_TYPE == 'r' || $RUN_TYPE == 'd']] then
echo "Invalid parameter" $RUN_TYPE
exit
fi
#
# -----------------------------------------------------------------------
# Set local variables
# -----------------------------------------------------------------------
set LOGFILE= /exlibris/dtl/d3_1/log/cleanoldbackups.log.`date '+%Y%m%d_%H%M%S'`
# -----------------------------------------------------------------------
# Switch to backup directory. Find and report/delete files
# -----------------------------------------------------------------------
cd /directory/backup
#switch $RUN_TYPE
case r
find . -atime +8 | sort | xargs ls -l >>$LOGFILE
breaksw
case d
find . -atime +8 -exec rm {} \; >>$LOGFILE
esac
# 2  
Old 05-20-2008
I think missing a semicolon

Try:
if [! [$RUN_TYPE == 'r' || $RUN_TYPE == 'd']] ; then

Also, I am a little concerned about the spacing. In the future, post code with codetags. If you highlight your pasted code text, and then click on the pound # sign above, the forum will maintain your spacing.
# 3  
Old 05-21-2008
hi,

thanks for the tip. i did make the suggested change and ran the script again but got the following messages;

./cleanoldbackups.sh: line 15: [!: command not found
./cleanoldbackups.sh: line 15: ==: command not found
./cleanoldbackups.sh: line 30: syntax error near unexpected token `find'
./cleanoldbackups.sh: line 30: ` find . -atime +8 | sort | xargs ls -l >>$LOGFILE'


Code:
2>&1
# --------------------------------------------------------------------------------
# Check run type - r report, d delete
# --------------------------------------------------------------------------------
set RUN_TYPE= $1
  if [! [$RUN_TYPE == 'r' || $RUN_TYPE == 'd']] ; then
  echo "Invalid parameter" $RUN_TYPE
  exit
fi
#
# --------------------------------------------------------------------------------
# Set local variables
# --------------------------------------------------------------------------------
set LOGFILE= /exlibris/dtl/d3_1/log/cleanoldbackups.log.`date '+%Y%m%d_%H%M%S'`
# --------------------------------------------------------------------------------
# Switch to backup directory.  Find and report/delete files not accessed for two weeks.
# --------------------------------------------------------------------------------
cd /san/exlibris1/ueabackup
#switch $RUN_TYPE
   case r
      find .  -atime +8 | sort | xargs ls -l >>$LOGFILE
      breaksw
   case d
      find .  -atime +8 -exec rm {} \; >>$LOGFILE
   esac

# 4  
Old 05-21-2008
Your condition is not correct. Try this:
Code:
if [ $RUN_TYPE != 'r' ] && [ $RUN_TYPE != 'd' ] ; then

# 5  
Old 12-09-2008
I make the changes to work fine in linux (my test was in slack)

2>&1
# --------------------------------------------------------------------------------
# Check run type - r report, d delete
# --------------------------------------------------------------------------------
RUN_TYPE=$1
if [ "$RUN_TYPE" != "r" ] && [ "$RUN_TYPE" != "d" ] ; then
echo "Invalid parameter" $RUN_TYPE
exit
fi
--------------------------------------------------------------------------------
# Set local variables
# --------------------------------------------------------------------------------
LOGFILE="/home/cassio/remove.log".`date '+%Y%m%d_%H%M%S'`
# --------------------------------------------------------------------------------
# Switch to backup directory. Find and report/delete files not accessed for two weeks.
# --------------------------------------------------------------------------------
cd /opt/wallis/webserver/logs/

if [ "$RUN_TYPE" == "r" ] ; then
find . -atime +8 | sort | xargs ls -l >>$LOGFILE
elif [ "$RUN_TYPE" == "d" ] ; then
find . -atime +8 -exec rm {} \; >>$LOGFILE
fi
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help to write to log file whether the shell script call pass fail

I have the below script triggered daily at 330am in the morning, since last 7 days job not writing anything to database. below impala shell calling shell file which has sql , it is extracting data and loads to a flat file txt file. which is going wrong for last 1 week. need help, echo... (2 Replies)
Discussion started by: cplusplus1
2 Replies

2. Shell Programming and Scripting

Need help to write a script for moving the log files to some other folder

Hi Experts, I want to write a script, based upon the following requirement 1) I am having 5 application $ cd logs $ ls -l drwxr-xr-x 2 natraj nat 5.0K Sep 20 10:25 one drwxr-xr-x 2 natraj nat 5.0K Sep 20 10:39 two drwxr-xr-x 2 natraj nat 1.5K Sep 20 10:58... (4 Replies)
Discussion started by: natraj005
4 Replies

3. Shell Programming and Scripting

How to write a shell script to display files in single path?

Hello friends, I am a script which dispalys a multiple files with their contents. for exm: suppose two file test1.txt and test2.txt. when I run my script it have to display the below O/P. test1.txt -rw-r----- 1 sranga staff 91 Sep 23 02:18 calc.sh -rw-r----- 1 sranga ... (2 Replies)
Discussion started by: sivaranga001
2 Replies

4. Shell Programming and Scripting

Write an automated shell program(s) that can create, monitor the log files and report the issues for

Hi , Please help me getting this done. Write an automated shell program(s) that can create, monitor the log files and report the issues for matching pattern. (i) Conditions for creating log files. Log file is created with date (example 2010_03_27.log). If the log file size is 10 Mb for... (1 Reply)
Discussion started by: itian2010
1 Replies

5. UNIX for Dummies Questions & Answers

Create a shell script for write files with 2 parameters

Hello, I'm a newbie in shell script. So, i would like to create a shell script which take 2 IN parameters (PARAM1 and PARAM2). This script need to create 2 files as : I need to create this file /etc/apache2/sites-available/PARAM2 : <VirtualHost *:80> DocumentRoot "/home/PARAM1/www"... (0 Replies)
Discussion started by: chatlumo
0 Replies

6. Shell Programming and Scripting

To write a shell script which groups files with certain pattern, create a tar and zip

Hi Guru's, I have to write a shell script which groups file names based upon the certain matching string pattern, then creates the Tar file for that particular group of files and then zips the Tar file created for the respective group of files. For example, In the given directory these files... (3 Replies)
Discussion started by: rahu_sg
3 Replies

7. UNIX for Dummies Questions & Answers

Script to Extract time from log files and write to a excel

Can someone help me with writing a unix script for following requirement 1) I have a log file in which we have start time and end time (format: hh:mm:ss) Example: starting script on Thu Jun 5 20:50:52 --------- Thu Jun 5 21:55:33 - Script Completed 2) I want to extract... (4 Replies)
Discussion started by: santosham
4 Replies

8. Shell Programming and Scripting

Script to Extract time from log files and write to a excel

Can someone help me with writing a unix script for following requirement 1) I have a log file in which we have start time and end time (format: hh:mm:ss) Example: starting script on Thu Jun 5 20:50:52 Thu Jun 5 21:55:33 - Script Completed 2) I want to extract start time and end time of... (0 Replies)
Discussion started by: santosham
0 Replies

9. UNIX for Advanced & Expert Users

Script to Extract time from log files and write to a excel

Can someone help me with writing a unix script for following requirement 1) I have a log file in which we have start time and end time (format: hh:mm:ss) Example: starting script on Thu Jun 5 20:50:52 Thu Jun 5 21:55:33 - Script Completed 2) I want to extract start time and end time of... (0 Replies)
Discussion started by: santosham
0 Replies

10. UNIX for Advanced & Expert Users

How to write Flat Files by shell script using Oracle Database

Hello There.. I came to a situation where I need to write flat files using shell scripts, I need to pull the records from the oracle database and create the flat file, This process should be automated. Can any shell script expert out here to help me.. please.. Will be really glad to... (3 Replies)
Discussion started by: coolbuddy
3 Replies
Login or Register to Ask a Question