Re: script line not working as its supposed to, but why?
On Wed, Dec 11, 2013 at 01:44:51PM +, Sharon Kimble wrote: On Wed, 11 Dec 2013 12:42:51 +0100 Gian Uberto Lauri sa...@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 ']' I think your problem here is to do with globbing. If you look at Gian's example, it has: $ ls -1 menu-* whereas yours has: $ ls -1 'menu-*' i.e. the filename is in single quotes. In the first case, the parameter menu-* is seen by the shell as a wildcard parameter. Before execution of ls, bash will read the directory and find all files that match that pattern and pass those filenames to the command. In other words, ls actually sees the parameters -1, menu-2011..., menu-2012... and so on. Several filenames, all of which exist. In the second case, the last parameter is a literal string. By putting the parameter in quotes, you're protecting it from shell expansion. ls sees only two parameters in this case: -1 and then menu-*. ls DOESN'T do parameter expansion itself, so it tries to list the file called menu-*. This file doesn't exist, so you get the above error. As an aside, if you want to use a parameter and are worried about the parser not knowing where the parameter name starts and ends, use ${}. For example, if I have a parameter $x and I want to echo the contents of that immediately followed by the letter y, I'd say echo ${x}y. ${x} is essentially the same as $x, but shows the parser where to start and end. Hope that all helps. signature.asc Description: Digital signature
script line not working as its supposed to, but why?
I have this line in a bash script that is not doing what its supposed to - 'find /home/boudiccas/.fluxbox -type f -mtime +$num -name 'menu-*' -delete' where $num = 7 and 'menu-*'=menu-20131209-11:05 . Obviously the date and time can be different in each case, so how do I get it so that it deletes the menus if their number is greater than 7 please? All the rest of the script works as expected, except for this one line! Does this make sense? 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 signature.asc Description: PGP signature
Re: script line not working as its supposed to, but why?
On 11/12/13 21:45, Sharon Kimble wrote: I have this line in a bash script that is not doing what its supposed to - 'find /home/boudiccas/.fluxbox -type f -mtime +$num -name 'menu-*' -delete' where $num = 7 and 'menu-*'=menu-20131209-11:05 . Obviously the date and time can be different in each case, so how do I get it so that it deletes the menus if their number is greater than 7 please? That's the tricky part. As it stands the menu is just a string *not* a value. Either convert it into a value or use another method to determine which menu to delete (hint: creation date). All the rest of the script works as expected, except for this one line! Does this make sense? Sharon. Kind regards -- To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org Archive: http://lists.debian.org/52a8454b.9090...@gmail.com
Re: script line not working as its supposed to, but why?
Sharon Kimble writes: I have this line in a bash script that is not doing what its supposed to - 'find /home/boudiccas/.fluxbox -type f -mtime +$num -name 'menu-*' -delete' where $num = 7 and 'menu-*'=menu-20131209-11:05 . Obviously the date and time can be different in each case, so how do I get it so that it deletes the menus if their number is greater than 7 please? All the rest of the script works as expected, except for this one line! Excuse me Sharon, I am not a native English speaker and I think my English skills are not up to the task to fully understand your message. You want to delete file modified more than 7 days ago (-mtime) or if you have more of 7 of them? And what goes wrong with the command execution? -- /\ ___Ubuntu: ancient /___/\_|_|\_|__|___Gian Uberto Lauri_ African word //--\| | \| | Integralista GNUslamicomeaning I can \/ coltivatore diretto di software not install già sistemista a tempo (altrui) perso...Debian Warning: gnome-config-daemon considered more dangerous than GOTO -- To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org Archive: http://lists.debian.org/21160.18398.177539.281...@mail.eng.it
Re: script line not working as its supposed to, but why?
On Wed, 11 Dec 2013 21:58:19 +1100 Scott Ferguson scott.ferguson.debian.u...@gmail.com wrote: On 11/12/13 21:45, Sharon Kimble wrote: I have this line in a bash script that is not doing what its supposed to - 'find /home/boudiccas/.fluxbox -type f -mtime +$num -name 'menu-*' -delete' where $num = 7 and 'menu-*'=menu-20131209-11:05 . Obviously the date and time can be different in each case, so how do I get it so that it deletes the menus if their number is greater than 7 please? That's the tricky part. As it stands the menu is just a string *not* a value. Either convert it into a value or use another method to determine which menu to delete (hint: creation date). 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. This could happen several times a day, or just once a week, there is no pattern to it. Or am I missing something obvious? :) 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 signature.asc Description: PGP signature
Re: script line not working as its supposed to, but why?
On Wed, 11 Dec 2013 12:09:18 +0100 Gian Uberto Lauri sa...@eng.it wrote: Sharon Kimble writes: I have this line in a bash script that is not doing what its supposed to - 'find /home/boudiccas/.fluxbox -type f -mtime +$num -name 'menu-*' -delete' where $num = 7 and 'menu-*'=menu-20131209-11:05 . Obviously the date and time can be different in each case, so how do I get it so that it deletes the menus if their number is greater than 7 please? All the rest of the script works as expected, except for this one line! Excuse me Sharon, I am not a native English speaker and I think my English skills are not up to the task to fully understand your message. You want to delete file modified more than 7 days ago (-mtime) or if you have more of 7 of them? And what goes wrong with the command execution? Thanks for replying. 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. 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 signature.asc Description: PGP signature
Re: script line not working as its supposed to, but why?
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. -- /\ ___Ubuntu: ancient /___/\_|_|\_|__|___Gian Uberto Lauri_ African word //--\| | \| | Integralista GNUslamicomeaning I can \/ coltivatore diretto di software not install già sistemista a tempo (altrui) perso...Debian Warning: gnome-config-daemon considered more dangerous than GOTO -- To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org Archive: http://lists.debian.org/21160.20411.327458.947...@mail.eng.it
Re: script line not working as its supposed to, but why?
On Wed, 11 Dec 2013 12:42:51 +0100 Gian Uberto Lauri sa...@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 signature.asc Description: PGP signature
Re: script line not working as its supposed to, but why?
* Gian Uberto Lauri wrote on 2013-12-11 at 12:42 (+0100): Sharon Kimble writes: 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. 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 :) This is too complicated: too many subshells, too many pipelines. $ cd /dir/with/files LC_ALL=C ls menu-* | head -n -7 | xargs -r rm will do the job: * cd ... and ensure that the directory exists and if (and only if it does the pipeline will be executed. * LC_ALL=C ls menu-* lists all relevant files, one file per line, in the correct ascending order after the shell had expanded the *. * head -n -7 lists all but the last 7 lines. * xargs -r will only call rm if there is some input. For testing purposes run the above line without |xargs... part. Although bash(1) is very long and its learning curve a little bit steep I do recommend reading it. In the long term there is no way avoiding that. Beginners may first tend to [1] and later to [2]. Also, [3] is quite interessting. Regards, Mathias [1] http://tldp.org/LDP/Bash-Beginners-Guide/html/index.html [2] http://tldp.org/LDP/abs/html/ [3] http://www.gnu.org/software/bash/manual/bashref.html -- To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org Archive: http://lists.debian.org/20131211141922.gb11...@gmx.org
Re: script line not working as its supposed to, but why?
Sharon Kimble writes: ++ wc -l ++ ls -1 'menu-*' ls: cannot access menu-*: No such file or directory + '[' 0 -gt 7 ']' I fear that you should either 'cd' to the directory (~/.fluxbox I suppose) you want to clear before running my code or change it this way if [ `ls -1 ~/fluxbox/menu-* | wc -l` -gt 7 ] then ls -t ~/fluxbox/menu-* | tail $((7-`ls | wc -l`)) | xargs rm fi ls returned an error because it did not find any file matching menu-* in the current directory. -- /\ ___Ubuntu: ancient /___/\_|_|\_|__|___Gian Uberto Lauri_ African word //--\| | \| | Integralista GNUslamicomeaning I can \/ coltivatore diretto di software not install già sistemista a tempo (altrui) perso...Debian Warning: gnome-config-daemon considered more dangerous than GOTO -- To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org Archive: http://lists.debian.org/21160.31069.658034.818...@mail.eng.it
Re: script line not working as its supposed to, but why?
On Wed, 11 Dec 2013 15:19:22 +0100 Mathias Bauer mba...@gmx.org wrote: LC_ALL=C ls menu-* | head -n -7 | xargs -r rm Thank you Mathias, I incorporated the line above into the script and it works beautifully, thanks. :) I'll also have a look at the bash tutorials that you posted because I know that they will be useful for me, thanks again. 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 signature.asc Description: PGP signature
Re: script line not working as its supposed to, but why?
Mathias Bauer writes: head -n -7 That's the option I did not recall :) Nice job. -- /\ ___Ubuntu: ancient /___/\_|_|\_|__|___Gian Uberto Lauri_ African word //--\| | \| | Integralista GNUslamicomeaning I can \/ coltivatore diretto di software not install già sistemista a tempo (altrui) perso...Debian Warning: gnome-config-daemon considered more dangerous than GOTO -- To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org Archive: http://lists.debian.org/21160.31329.515448.719...@mail.eng.it
Re: script line not working as its supposed to, but why?
On 11/12/13 22:14, Sharon Kimble wrote: On Wed, 11 Dec 2013 21:58:19 +1100 Scott Ferguson scott.ferguson.debian.u...@gmail.com wrote: On 11/12/13 21:45, Sharon Kimble wrote: I have this line in a bash script that is not doing what its supposed to - 'find /home/boudiccas/.fluxbox -type f -mtime +$num -name 'menu-*' -delete' where $num = 7 and 'menu-*'=menu-20131209-11:05 . Obviously the date and time can be different in each case, so how do I get it so that it deletes the menus if their number is greater than 7 please? That's the tricky part. As it stands the menu is just a string *not* a value. Either convert it into a value or use another method to determine which menu to delete (hint: creation date). 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. This could happen several times a day, or just once a week, there is no pattern to it. Or am I missing something obvious? :) Each menu (like any file) has a creation date. You can use ls to list according to creation date, which allows you to delete all but the most recent 7 menu files. Sharon. I see Gian and Mathias have already provided solutions. Kind regards -- To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org Archive: http://lists.debian.org/52a94b0d.50...@gmail.com