"Add to filetool.lst" buttonForum: DSL Ideas and Suggestions Topic: "Add to filetool.lst" button started by: RoGuE_StreaK Posted by RoGuE_StreaK on Mar. 27 2005,02:19
Another half-baked idea, is it possible to create a script, to attach to a button in emelfm, that would add the selected file to the filetool.lst?ie., you're browsing through the files, you find something of interest, select it, hit the button, and the script appends the full file location to the backup list - simplifying (well, in some cases) the process of adding files to the list? No idea of the logistics of this. Something along the lines of get full file location and append this to the text file, starting a new line on the append? Posted by cbagger01 on Mar. 27 2005,04:39
This sounds like a cool idea.The script would need to "grep" the existing filetool.lst to prevent duplicates, but otherwise I like the concept. Posted by RoGuE_StreaK on Mar. 27 2005,05:45
RE: duplicates in the list, wouldn't duplicate files just overwrite themselves in the backup, rather than double-up? If they did, you wouldn't need to worry about the grep complications?I'm not sure how tarring works, if you try to add a file to a tar when that particular file already exists, does it ignore it, overwrite it, duplicate it, or come to a screaming halt? Posted by mikshaw on Mar. 27 2005,06:16
It duplicates inside the tar file, but apparently overwrites on extraction
Posted by RoGuE_StreaK on Mar. 27 2005,06:46
OK, so will need to grep.Um, what's a grep? (kidding, know what it is, just not how to utilise it properly. I guess if I sat down and actually learned how to write scripts properly, I wouldn't need to keep making these vague suggestions for other people to implement!) Posted by ke4nt1 on Mar. 27 2005,07:48
I find this "very handy" , and I miss not having it in emelfm.With the F keys being so similar to NC, MC, and others, I used to hit the F6 key to rename/move , but in emelfm, it only moves.. So, I programmed the F9 key to rename files.. Open emelfm Click on the "configure" button in the center row Highlight the title "buttons" in the left-hand row In the center portion, click "add" In the new window- type 'Rename' as a label Click the 'system' option, and select RENAME Click OK to save.. Remember to add the .emelfm dir to your filetool.lst to save between backups! Another annoying thing is when your in a long list of files, looking to get to the top of the list ( .. ) , and you hit the 'home' key, and find yourself back in /home/dsl , rather than at the top of the dir you 'were' in .. grrr... I'd like to reprogram that as well.. 73 ke4nt Posted by mikshaw on Mar. 27 2005,16:00
Rogue Streak:Configure/Buttons/Add... command: [removed -- see below] I'm not sure if the preceding slash is a problem. if so you might need to change the command a bit. EDIT: That didn't work with multiple files, but this one will. It also removes the leading slash.
EDIT2: In case you are interested in learning some script (and since i'm just twiddling my thumbs on a sunday afternoon), i'll try to break this down for you.... It uses a "for" loop, which basically does everything within the loop, applied to whatever immediately follows "for". In this case it is $i, which is a variable that contains the selected file(s). EmelFM uses %f to specify the files selected. If there are multiple files selected, %f would be a list of all those files. So $i represents the list of files, one at a time. Example: foo, bar, and foobar are selected, so %f would be "foo bar foobar". "for i in %f" would be the same as "for i in foo bar foobar", which says "do the upcoming actions for foo, then bar, then foobar". `pwd|cut -c 2-`/$i pwd returns the current directory, which is piped to cut. Cut returns everything from the second character on to the end of the line, and /$i appends a slash and the selected file. The backticks say "do everything in the backticks as a command, and the result is what we work with". So if "foo" is the currently selected file, and the current directory is /usr/bin, then it would do this: 1) pwd = /usr/bin 2) cut -c 2- = usr/bin 3) /$i = /foo 4) result: usr/bin/foo Grep looks for the result (usr/bin/foo) in /home/dsl/filetool.lst the "||" means "or". If the grep succeeds, the following command is not run. If the grep fails, then "echo usr/bin/foo >>/home/dsl/filetool.lst" is run. This appends usr/bin/foo to filetool.lst Posted by RoGuE_StreaK on Mar. 27 2005,22:54
Very nicely explained, mikshaw! I have some programming experience, but mainly in Flash Actionscript. Should this code be all one line, or can it be formatted in a structured manner, eg.
I see how it's operating once I understood the basic if/else structure. So "pwd" would be "present working directory"? "cut -c 2-" - is that "cut everything from the 2nd character on, and pass it on", or "cut up to the second character"? "echo something >> some.file" adds the results to the specified file? RE: foo & bar, just tried to track down it's origins and original meaning, seems nothing is clear cut: < Etymology of "Foo" > Looks like there could be at least 5 possible origins. Interesting note is that "fubar" could possibly have come from the German "furchtbar", which is (apparently, I don't speak German, though kinda trying to learn a bit) "terrible"... And now back to our regular broadcast. Posted by mikshaw on Mar. 27 2005,23:26
I also have had a decent amount of actionscript experience, which made learning bash much easier than it might have been.Your multiline format is close. Typically it's like this: for blah in something something; do command1 command2 command3 done If you use && or || to chain commands, the commands should be on the same line. You can escape the newline character with a backslash if you want to keep things compact: for i in %f; do grep `pwd|cut -c 2-`/$i /home/dsl/filetool.lst || \ echo `pwd|cut -c 2-`/$i >>/home/dsl/filetool.lst; done Using the escape, you can break up the lines wherever you think they would be most readable. Just make sure the character immediately following the backslash is really a newline and not a space. Also, in a multiline script you can save yourself some typing, and possible retyping if you want to make a future change, by replacing redundant parts with variables: list=/home/dsl/filetool.lst file="`pwd|cut -c 2=`/$i" for i in %f; do grep $file $list || echo $file >> $list ; done EmelFM probably won't read a multiline command from its config files, so that's why i put it all on one line. In a script I try to keep most lines within 70-80 characters just so it's easy to read in a small terminal. pwd is what you said "echo something >> somefile" adds(appends) "something" to somefile "echo something > somefile" replaces somefile with a new somefile containing only "something" the cut command starts at the second character and prints everything from that point to the end of the line. If the command were "cut -c 2" instead of "cut -c 2-" (note the hyphen after the 2) it would print the second character only. "-c -2" would print the first and second characters, and "-c 2-6" prints from the second to sixth characters inclusive. The "-c" option specifies counting per character, as opposed to byte(-b) or field(-f). Posted by RoGuE_StreaK on Mar. 28 2005,00:40
Mikshaw, if you aren't a lecturer/tutor/teacher, you should be! I'll be a referee for you if you want to apply for said positions!
Posted by clivesay on April 18 2005,13:39
I just wanted to let everyone know that mikshaw's little emelfm button script works very well. It is at the top of my button list right under mydsl. This is a little gem to have around.Chris bookmark Posted by ke4nt1 on April 18 2005,15:29
Agreed, tnx mikshaw....and I understood the word fubar to mean .. ( Fscked Up Beyond All Repair ) 73 ke4nt Posted by roberts on April 18 2005,23:20
Accepted and will be in the next release. This should help make it easier to explain to new users what to add to their filetool.lst depending on their needs.
Posted by mikshaw on April 29 2005,12:20
Malfunction! *zzztrrrrgtz!*There's a potential problem with that command. Suppose 'pwd' returns '/ramdisk/home/dsl'. The line(s) added will begin with 'ramdisk/'. This isn't a problem with a vanilla liveCD or Frugal, but suppose you switch to using a persitent home with no symlink to it in /ramdisk. Those files you previously backed up will have nowhere to go. To fix this i've changed the cut command to sed: pwd|sed 's/^\/ramdisk//;s/^\///' Cut removed only the leading slash, but this one will remove the slash, plus a leading '/ramdisk' if it exists. Posted by WoofyDugfock on April 30 2005,12:13
Hey guys, how about something like this???
Even with the console messages and temp file, it ran a little faster (using time) for me than Mik's script on embedded. `join` used like this is something like `cat - $a|sort|uniq`, except that (again) it seems to run slightly faster here (every bit of speed helps with embedded!). Posted by mikshaw on April 30 2005,14:21
hey...that's cool. I'd never heard of 'join'...or 'done'.gonna need to read some more =o) could you please verify a few things? (these are assumptions): %d = `pwd` join replaces grep done checks the exit status of the last command It looks like it's doing more work, but if it's faster it's better =o) Posted by WoofyDugfock on April 30 2005,16:31
Thx, I wouldn't have heard of `join' either except it was mentioned in passing recently in Linux Magazine (though they didn't use it this way) and I've been working hard on my bash (slow period at work!).
%d returns the active directory from emelfm while %f returns the basename of the file selected (emelfm help page) - so in this case the same result as `pwd`. join file1 file 2 -- compares lines in both files, actually the default is to compare the first field in the line that ends with the first whitespace (ie in our case this compares whole lines). If there is a match, it will append the remainder of the matching line in one file to the line in the other, except if the line in file 1 is identical to that in file 2 there's nothing to append and it won't double that line to output (so if the line is already in filetool.lst it won't be duplicated to stdout). One of these files can be stdin `-' ie the output from a pipe. If there is no match it won't return anything, unless you use option `-a filenumber' ("append"), in which case it will append all of file1 to output. Since we use -a for both files, it will write the contents of both stdin and $a to the temp file, which then overwrites filetool.lst ie the line is written to filetool.lst just as desired. So used here, in short, it does replace "(test) grep pattern append || don't append ", but it's in one command. Clear as mud, I know. Took me a bit of thought, I haven't seen this anywhere (although it'd have to be around). The "done" is just the "done" that terminates the "for" loop. Though this use of "join" looks nice, I expected it to be slower than either Mik's grep test version or using "cat - $a|sort|uniq" (my first attempt - same result as join here except join puts the new lines at the beginning of filetool.lst) - but putting`time` in front of it suggested otherwise. Posted by mikshaw on April 30 2005,21:01
heh...i should have realized what "done" is. From my first pre-coffee look, i was thinking it was an additional command =opOne thing to consider, though i don't know the whole story.... filetool.lst is executable. I'm not sure why, but i assume there's a reason for it. Does your script overwrite this permission when it overwrites the file? Posted by WoofyDugfock on May 01 2005,15:37
Hang on I've gotta think about it
Posted by WoofyDugfock on May 01 2005,19:19
You must have been reading my thoughts because it occurred to me that overwriting filetool.lst was DUMB and redundant, let alone that temp file. Just where does my brain go when it's not there? Dunno, I can't find it to tell me !!I have a verson that doesn't use the temp file or overwrite filetool.lst either, but it's buggy and I've about had it for today. Does anybody else think it's worth chewing on/comparing implementations like this - after all, if-grep does work ....? Regardless I intend to try to use this join trick to speed up a long script I'm writing that currently 'if-greps-appends-else' a lot on largish files.... Posted by clacker on May 01 2005,21:06
WoofyDugFock, I was really impressed by your code. I learned a lot from the simplicity of your use of && and ||, as well as being able to use |'s rather than /'s in sed. I didn't know that. Having the error displayed after the procedure was really an eye opener for me. I know I'll be doing that myself in the future. I got duplicates when I tried your code though. When I had the same files selected, I got multiple entries in filetool.lst when I hit the button multiple times.If join works eliminating duplicates, then what was the problem with a duplicated filetool.lst? Why should you need the extra sed statements? Do all files get duplicated? I went back to sort and uniq to change your code to:
I didn't get duplicates, and filetool.lst wasn't repeated either. The raw example is great: it's like a little microcosm of bash wonders. Posted by WoofyDugfock on May 02 2005,13:14
Thanks heaps for your encouraging remarks, Clacker. I was beginning to wish I'd never opened my big fat mouth! Oh, these stumbling steps into bashhood!Vertical pipes in sed? I think you must be referring to my censored previous post - you must have been quick and read it before I deleted the "fix" - I found the same annoying bug myself so went back and plucked the foul offensive thing out! (The one with the echo at the front as you have it and the tee ..?). I came to the same conclusion about |sort|uniq for appending rather than overwriting - I've been trying today to make join work for appending inside the do loop ( "join -v 1 - $a | tee -a $a etc )" but I'm tired of the buggy issues. If it is of interest, I found getting rid of "join -v 1 - $a" writing filetool.lst's own name to $a though can be done - change the sed filter so that it blocks that filename -
Posted by clacker on May 02 2005,14:45
The verticle pipes I was talking about was using the formsed "s|whatever|whatevuh|" instead of sed "s/whatever/whatevuh/" That helps with using /'s in the "whatever" sections. I was using the \ character way to much because I didn't know you could start off and end using the | character. |