The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
I need to write a shell script. lakshmanrk Shell Programming and Scripting 6 06-20-2007 06:00 AM
please Write a shell script rvrao77 Shell Programming and Scripting 6 11-16-2006 10:16 AM
need help to write shell script getdpg Shell Programming and Scripting 0 04-10-2006 10:30 AM
How to write Flat Files by shell script using Oracle Database coolbuddy UNIX for Advanced & Expert Users 3 02-21-2006 02:50 PM
Help needed to write a shell script! sreedharsn Shell Programming and Scripting 3 10-11-2005 12:21 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-20-2008
yabai yabai is offline
Registered User
  
 

Join Date: May 2008
Posts: 11
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 (permalink)  
Old 05-20-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
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 (permalink)  
Old 05-21-2008
yabai yabai is offline
Registered User
  
 

Join Date: May 2008
Posts: 11
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 (permalink)  
Old 05-21-2008
ripat ripat is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2006
Location: Belgium
Posts: 438
Your condition is not correct. Try this:
Code:
if [ $RUN_TYPE != 'r' ] && [ $RUN_TYPE != 'd' ] ; then
  #5 (permalink)  
Old 12-09-2008
Cassio Seffrin Cassio Seffrin is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 1
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
Closed Thread

Bookmarks

Tags
shell script, shell scripting, unix scripting, unix scripting basics

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:24 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0