The UNIX and Linux Forums  


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
Problem in For Loop The Observer Shell Programming and Scripting 2 05-28-2008 03:43 AM
loop problem maskot Shell Programming and Scripting 1 05-25-2007 05:10 AM
Problem with for loop/sed ? chiru_h Shell Programming and Scripting 2 08-27-2006 12:55 PM
loop Problem dhananjaysk Shell Programming and Scripting 3 03-31-2006 02:05 PM
problem with while loop mridula High Level Programming 1 12-11-2005 11:44 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-25-2008
namishtiwari namishtiwari is offline Forum Advisor  
Registered User
  
 

Join Date: Aug 2007
Location: Bangalore
Posts: 377
for loop problem

Hi,
I have a directory called logs in which i have the log files.

i have to touch the file before deleting it.

i am doing like this
filestodelete="*.log* *log*"

for files in $filestodelete
do
touch $files $files.$(date +%a)
rm -f $filestodelete
done

touch is not working here,means it is not touching the file before deleting it.
kindly help me out in this.

Last edited by namishtiwari; 01-25-2008 at 09:32 AM..
  #2 (permalink)  
Old 01-25-2008
V3l0 V3l0 is offline
Registered User
  
 

Join Date: Nov 2007
Location: Belgium & France
Posts: 70
Hello,

First, when you select "*log*", you don't have to select "*.log*" beacause they are already in the first selection !

Second, why "touching" the file before deleting it ?

Third, your script will be runnig with "unable to remove file xxx" beacause of the command "rm -f $filestodelete" that will delete all the files selected by "for files in (*log*|*.log*)"

So, I propose you the script :

Code:
list=$(ls *log*)

for file in $list
do
    cp $file $file.$(date +%a)
    rm -f $file
done

  #3 (permalink)  
Old 01-25-2008
namishtiwari namishtiwari is offline Forum Advisor  
Registered User
  
 

Join Date: Aug 2007
Location: Bangalore
Posts: 377
Quote:
Originally Posted by V3l0 View Post
Hello,

First, when you select "*log*", you don't have to select "*.log*" beacause they are already in the first selection !

Second, why "touching" the file before deleting it ?

Third, your script will be runnig with "unable to remove file xxx" beacause of the command "rm -f $filestodelete" that will delete all the files selected by "for files in (*log*|*.log*)"

So, I propose you the script :

Code:
list=$(ls *log*)

for file in $list
do
    cp $file $file.$(date +%a)
    rm -f $file
done
you gave me good suggestions.Thanks..

i have to touch the file because we have some processes running that need to pick that blank file otherwise we need to restart the aaplication again if i deleted the file without touching it.cp will unnecessarily increase the size.
  #4 (permalink)  
Old 01-25-2008
danmero danmero is offline Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,434
Quote:
Originally Posted by namishtiwari View Post

Code:
list=$(ls *log*)

for file in $list
do
    cp $file $file.$(date +%a)
    rm -f $file
done

i have to touch the file because we have some processes running that need to pick that blank file otherwise we need to restart the aaplication again if i deleted the file without touching it.cp will unnecessarily increase the size.
What about moving the file and touching a new one:

Code:
for file in `ls *log*`
do
    mv $file $file.`date +%a` && touch $file || echo "Can not backup $file";
done

  #5 (permalink)  
Old 01-25-2008
namishtiwari namishtiwari is offline Forum Advisor  
Registered User
  
 

Join Date: Aug 2007
Location: Bangalore
Posts: 377
Thanks for all your replies.

but i got a problem here-- my script is like this--


Code:
BackupLocation="$OPTARG"
                if [[ $BackupLocation != *backup ]]; then
                        echo "Appending backup subdirectories"
                        BackupLocation=$BackupLocation/backup
                        mkdir -p $BackupLocation >/dev/null 2>&1
                        if [[ $? != 0 ]];then
                        echo "First Create The Directory And Then Take backup"
                        fi
                        cd $FileLocation
                        pwd
                        cp -R $FilesToDelete $BackupLocation
                       list=$(ls *log*)
                        for files in $list
                        do
                             rm -f $files
                          touch $files.$(date +%a)
                                                 done
                else
                        mkdir -p $BackupLocation >/dev/null 2>&1
                        cp -R $FilesToDelete $BackupLocation
                        for files in $list
                        do
                          rm -f $files
                          touch $files.$(date +%a)
                          
                        done
                if [[ ! -d $BackupLocation ]]; then
                echo "Unable to make backup directory: $BackupLocation"
                        if [[ $IsCronJob -eq 1 ]]; then
                          SendMiddleTierCleanMail "Middletierclean error message" $mt_clean_errfile
                        fi

here it will touch the file again and again and appending the day after the file name again like->
ansrpt26529.log.Fri.Fri.Fri.Fri
but i do not want it like that.
i have to take backup of the file.then touch it and delete the file.
the pattern matching for the files to delete is
list=$(ls *log*)
it is selecting all the files ansrpt26529.log.Fri.Fri.Fri.Fri like this but i do not want it like this it sould be only ansrpt26529.log.Fri.
pattern matching may be diffrent here so give me some suggestions to solve this problem.

Last edited by namishtiwari; 01-25-2008 at 12:41 PM..
  #6 (permalink)  
Old 01-25-2008
danmero danmero is offline Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,434
What about

Code:
                       
list=$(ls *log.???)
for files in $list
do
    rm -f $files
    touch `echo $files | sed 's [^.]*$  '``date +%a`
done


Last edited by danmero; 01-25-2008 at 01:05 PM..
Closed Thread

Bookmarks

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 11:31 AM.


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