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

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 01-25-2008
Registered User
 

Join Date: Aug 2007
Location: Bangalore
Posts: 288
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 06:32 AM.
Reply With Quote
Forum Sponsor
  #2  
Old 01-25-2008
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
Reply With Quote
  #3  
Old 01-25-2008
Registered User
 

Join Date: Aug 2007
Location: Bangalore
Posts: 288
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.
Reply With Quote
  #4  
Old 01-25-2008
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 737
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
Reply With Quote
  #5  
Old 01-25-2008
Registered User
 

Join Date: Aug 2007
Location: Bangalore
Posts: 288
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 09:41 AM.
Reply With Quote
  #6  
Old 01-25-2008
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 737
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 10:05 AM.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 06:29 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0