![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | 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 here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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. |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
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
|
|||
|
|||
|
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. |
|
#4
|
|||
|
|||
|
Quote:
Code:
for file in `ls *log*`
do
mv $file $file.`date +%a` && touch $file || echo "Can not backup $file";
done
|
|
#5
|
|||
|
|||
|
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
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. |
|
#6
|
|||
|
|||
|
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. |
|||
| Google The UNIX and Linux Forums |