[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: script line not working as its supposed to, but why?



On Wed, 11 Dec 2013 12:42:51 +0100
"Gian Uberto Lauri" <saint@eng.it> wrote:

> Sharon Kimble writes:
>  > But there is no consistency with the creation date, the menu
>  > itself is regenerated whenever I install a new programme, and the
>  > old menu is saved with the suffix of the date and time.
> 
>  > I want to delete the 'menu-*' files if there are more than 7, and
>  > the command is parsed when I have 'set -x' at the head of the
>  > script but this line does nothing! It runs but doesn't achieve
>  > anything.
> 
> The find command is not your friend here, I think.
> 
> If you have a single directory where these menu are regenerated
> automatically, you could exploit the fact that lines like
> 
> menu-20131209-11:05
> 
> have an asciibetic order (order based on the ASCII code) that matches
> the age order, but reversed, I mean newer backups come last. The
> option -t of ls fixes this.
> 
> I would try with something like this:
> 
> if [  `ls -1 menu-* | wc -l` -gt 7 ]
> then
>     ls -t menu-* | tail $((7-`ls | wc -l`)) | xargs rm
> fi
> 
> The first test ensures that you have more than 7 files.
> 
> Then you list the files in reverse asciibetical order (that is older
> last), then the expression
> 
> $((7-`ls | wc -l`))
> 
> does the magic to compute the option to pass to tail so that it shows
> the last (number of files - 7). And finally xargs feeds rm. You can
> use rm -v to see them being deleted :)
> 
> I hope this helps.
> 
Thanks for this, I've just got round to testing it. I pasted it into my
bigger script which works as expected, and then it came to your lines
of code, and this is its output from it -

++ wc -l
++ ls -1 'menu-*'
ls: cannot access menu-*: No such file or directory
+ '[' 0 -gt 7 ']'

Does this help in debugging it?

For the record, this is the working parts of the script so you can see
how it all fits together -

mv ~/.fluxbox/menu{,-$(date +%Y%m%d-%R)}; mmaker fluxbox -f; sed -i '\|\[exec[]]\s[(].*[)]\s[{]/usr/lib/xscreensaver/.*\s-root[}]|d' ~/.fluxbox/menu; sed -i '\|\[exec[]]\s[(].*[)]\s[{]\b\(xscreensaver\).*[}]|d' ~/.fluxbox/menu; perl -0777 -pi -e 's/(.*)\[end]/$1/s;s/(.*)\[end]/$1/s;s/^\s*\n//gm' ~/.fluxbox/menu; printf '\t[end]\n\t[separator]\n\t[submenu] (My Menu)\n\t[include] (~/.fluxbox/usermenu)\n\t[end]\n\t[end]' >>~/.fluxbox/menu
chown boudiccas: ~/.fluxbox/menu
#find /home/boudiccas/.fluxbox -type f -mtime +"$num" -name 'menu-*' -delete
if [  `ls -1 menu-* | wc -l` -gt 7 ]
then
    ls -t menu-* | tail $((7-`ls | wc -l`)) | xargs rm
fi

Sharon.
-- 
A taste of linux = http://www.sharons.org.uk
efever = http://www.efever.blogspot.com/
efever = http://sharon04.livejournal.com/
my git repo = https://bitbucket.org/boudiccas/dots
Debian testing, Fluxbox 1.3.5, LibreOffice 4.1.3.2
Registered Linux user 561944

Attachment: signature.asc
Description: PGP signature


Reply to: