Operating System - HP-UX
1831645 Members
2818 Online
110029 Solutions
New Discussion

Question about James/Victor's Script???

 
Shaun Aldrich
Frequent Advisor

Question about James/Victor's Script???

Hello Victor/James/Everyone,

Thanks a lot James/Victor...

Just a quick question for you James. In the last response you answered I noticed that in the following line -

FILES=`echo $FILES | cut -f1-$N -d " "` #...list's

in the cut portion you have -d. What does that do? If you get a chance what exactly does this whole line do?

I really like the logic that you put into this. I will have to learn this a lot better.

One other question is why do you have the head statement by itself above the rm?

Does it not need any options? Look forward to hear from you. Thanks

MYDIR=/tmp/mylog* #...whatever...
FILES=`ls -t $MYDIR` #...collect by mod time
COUNT=`echo $FILES | wc -w` #...get how many
if (( COUNT < 100 ))
then
exit 0
fi
let N=COUNT-100 #...keep 100 of them...
FILES=`echo $FILES | cut -f1-$N -d " "` #...list's
head
rm $FILES
7 REPLIES 7
James R. Ferguson
Acclaimed Contributor

Re: Question about James/Victor's Script???

Shaun:

FILES=`echo $FILES | cut -f1-$N -d " "`

The variable FILES is updated with the result of itself after 'cut'ting fields one (1) through the value of N. The cut operation's delimiter (d) is the blank character (" ").

Check the manual pages for cut. Do:

# man cut

Victor's solution is a good one too, and he and other can be awarded points for their responses to your queries, too.

...JRF...


Shaun Aldrich
Frequent Advisor

Re: Question about James/Victor's Script???

Thanks for the response James... I am going back to award Points to everyone right now.

Thanks for the script... Should I keep the head statement in there? It's by itself with no parameters just before you do the rm.

Sorry for all the questions but it has helped me a great deal....

Scott D. Allen
Regular Advisor

Re: Question about James/Victor's Script???

Shaun,

Looks like he's getting a list of all the files in MYDIR and then using the N variable to count the total number of files in the list and then truncate the list to only include the last N files in the list, where N is the number of files OVER 100.

FILES=`echo $FILES | cut -f1-$N -d" "`

This line echos the current value of FILES and cuts out the first N (-f1-$N) files separated by a space delimiter (-d" ") and sets them to be the new value of FILES. The script then removes these files, just leaving the last 100 files in MYDIR.

--Scott
"Sometimes the devil you know is better than the devil you don't know."
Shaun Aldrich
Frequent Advisor

Re: Question about James/Victor's Script???

Thanks Scott...

Thanks for the help on describing the script... Should I keep the head statement in there? It's by itself with no parameters just before you do the rm. Normally I have seen parameters used with it in the past.

That's the last of my questions once I have it figured out... Thanks for all your help...
Scott D. Allen
Regular Advisor

Re: Question about James/Victor's Script???

Shaun,
No. You don't need the head statement. I'm actually a little curious what that's doing in there. ;)
--Scott
"Sometimes the devil you know is better than the devil you don't know."
Scott D. Allen
Regular Advisor

Re: Question about James/Victor's Script???

I just reread the script. The head is actually a spill over from the comment at the end of the previous line. Not a separate command. So, NO don't include a head command there.
--Scott
"Sometimes the devil you know is better than the devil you don't know."
James R. Ferguson
Acclaimed Contributor

Re: Question about James/Victor's Script???

Shaun (and Scott):

Scott's comments are absolutely on target. He descibed for you perfectly my method of attack. The 'head' should not be there -- it was part of a different solution provided by Victor. Thanks, Scott.

...JRF...