So it’s been a little bit and I’m still kind of at a loss as to what to post here regularly. Seeing as how my readers seem content just to read whatever I put up, then I guess I’ll just put up whatever! MUAHAHA Command decision. Since I love linux that’s kind of what you guys are probably going to hear about most. Ah well, I’ve almost converted one or two of ya, or at least gotten you past the idea of fearing change enough to give it a rational chance…Which is all I can really ask of anyone.
One cool thing that I did the other day was write a script – I know most of you are probably thinking ‘I didn’t know he wrote screenplays!’ no. Not that kind of script. Put down the air-freshener and seek help. The type of script I’m talking about is a shell script to do something that I find rather tedious, automatically. See, everyone who knows me knows that I play online text based games (called Mush/mux/moo/mu*). Well, I use a text based client to log in to my different worlds – fitting no? – and whenever I log in, it automatically logs everything that I see on my screen. I set this up a while back within the client to auto-log on connection (the client, by the way is TF5 – Tinyfugue 5) and when it logs, it goes to a directory (folder for you windows users) under my home directory named ’share’. So the path looks like /home/swift/share. Now, underneath share is a directory for each ‘world’ or ‘character’ I connect to. TF automatically seperates the logfiles out by the world, and names the file in a MM-DD-YY.log format, so that I can look back on a particular day and find something. Neat, but what happens after abou three or four months worth of logging? Yeah! You get spammed when you go in and list the directory contents by a bunch of files that look like 08-18-08.log. I mean a /bunch/. I connect a lot. So, usually once a month I go through near the beginning of the month, make a directory underneath each character/world directory named in a MM-YY format, so it’ll look something like this – /home/swift/share/Swift/07-08/’all of july’s files here’
Needless to say I can do it fairly quickly but when you consider that I’ve got eight or nine characters/worlds that I connect to regularly, this shit gets tedious after a while. That’s fifteen or twenty minutes I could spend doing something else…like playing on my game! So I decided after getting fed up with having to hand organize these files “You know what? This is the perfect thing to provide a bash script to”, so that’s what I did. After a bit of trial and error, I came up with a script I call tflogarc.sh that I can schedule to go through once a month and do the following things:
- Go into a game directory recursively
- Make a directory that is named for the last month’s numerical-current year’s numerical (07-08 ferex)
- Move all files that start with the numerical month matching last month’s numerical month into the correct directory.
- Go back up to /home/swift/share
- Rinse and repeat until all played worlds are organized.
- Leave me with free time each month to do something else!
Okay, that last one didn’t really occur to me until about halfway through the project. I just started out seeing if I could do it, and with a little help, I did. Thus is the power of linux! And looking at it now, weeding out all the comments (I tend to comment heavily so I’ll know wtf I was thinking six months from now when I go back to tinker with it) the script itself is only 37 lines long*. Not a huge program at all, and if I were more experienced at scripting it wouldn’t have taken me the couple of hours it did to get everything just right. Add in the fact that with a single line in my crontab (file that the linux/unix cron daemon uses to schedule commands to be run) that it runs every month at 1:05 am for the length of time it takes to do the work (about 3 seconds) and then goes back to sleep until the next month. With windows, I’d just be completely shit out of luck unless I wrote it in something like python or perl, or (God help us) Visual Basic. I suppose you could try and script a .bat (batch file) to do it, but then you have to wrestle with the windows taskmanager and all of that. With this, it’s a built in scripting language that lets you do as much or as little as you want, to your own level of ability, and there are plenty of folks out there online willing to help out when you have questions, which is a bonus. If you’d like to take a look at the script it’s at http://swift.homelinux.net/tflogarc.txt.
* – for the command pipe that gave me that answer it’s grep -v \# tflogarc.sh|wc -l
What this does is greps (searches) the file for everything except the # symbol in the file tflogarc.sh then pipes all of them out to the wc -l command which does a wordcount (in this case -l tells it to count lines only, not words.) Wha? You didn’t think I’d actually count the lines by hand did you?
So you see, sometimes I get on a little kick and make my life a little easier by using the tools that are around, laying there, free for use, all we have to do is pick them up, brush them off and put a little effort into learning to use them.
More later,
S