![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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.. |
|
||||
|
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
|
|
||||
|
Quote:
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. |
|
||||
|
Quote:
Code:
for file in `ls *log*`
do
mv $file $file.`date +%a` && touch $file || echo "Can not backup $file";
done
|
|
||||
|
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.. |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|