[scripting] What is the most useful script you've written for everyday life?

Just wondering what little scripts/programs people here have written that helps one with his or her everyday life (aka not work related).

Anything goes, groundbreaking or not. For me right now, it's a small python script to calculate running pace given distance and time elapsed.

This question is related to scripting

The answer is


I have a Python script that automatically runs when I plug my digital camera in.

It copies all of the pictures off the card on the camera, backs them up, and then uploads them to Flickr.


The upload-to-Flickr piece comes from uploadr.py (which I can't take credit for).

Here's the Python code for unloading the camera. It recurses through SRCDIR and names each image with the date & time before copying the images to DESTDIR.

#!/usr/bin/python

import os
import string
import time
import shutil

###################################################
__SRCDIR__ = "/mnt/camera"
__DESTDIR__ = "/home/pictures/recent"
###################################################
def cbwalk(arg, dirname, names):
    sdatetime = time.strftime("%y%m%d%H%M")
    for name in names:
        if string.lower(name[-3:]) in ("jpg", "mov"):
            srcfile = "%s/%s" % (dirname, name)
            destfile = "%s/%s_%s" % (__DESTDIR__, sdatetime, name)
                    print destfile
            shutil.copyfile( srcfile, destfile)
###################################################
if __name__ == "__main__":
    os.path.walk(__SRCDIR__, cbwalk, None)

[email protected] and [email protected] => modify the hosts file, to go through LAN or WAN addresses.


I used to work at a technology summer camp, and we had to compose these write-ups for each of the kids in the group at the end of the week, which they would then receive and take home as a keepsake. Usually, these consisted of a bunch of generic sentences, and one to two personalized sentences. I wrote a python script which constructed one of these write-ups out of a bank of canned sentences, and allowed the user to add a couple of personalized sentences in the middle. This saved a huge amount of time for me and other counselors I let in on the secret. Even though so much of it was automated, our write-ups still looked better than many of the 'honest' ones, because we could put more time into the personalized parts.


A shell script to perform rotating backups using rsync. It also supports executing arbitrary child programs to support other pre-backup activities (downloading delicious bookmarks, for example).

http://gist.github.com/6806


A python script that does a filewalk and prints my directory tree sorted by disk usage.


Best real-life script?

Me: (Enters room) "Boss, I want a raise."

Boss: (Offers chair from behind desk) "A raise? Please, take my job!"

Then again, that may be the worst script!


Mass file renaming via drag&drop.

Ages ago I've made a small VBScript that accepts a RegEx and replaces file names accordingly. You would simply drop a bunch of files or folders on it. I found that to be very useful throughout the years.

gist.github.com/15824 (Beware, the comments are in German)


alias snoot='find . ! -path "*/.svn*" -print0 | xargs -0 egrep '

A similar backup.sh for each project, that tars and gzips just the source, moves it into a snapshot directory and labels it with timestamp: project-mmddyy-hhmmss. Useful for coding between commits.


Best real-life script?

Me: (Enters room) "Boss, I want a raise."

Boss: (Offers chair from behind desk) "A raise? Please, take my job!"

Then again, that may be the worst script!


I use procmail to sort my incoming email to different folders. Because I have trouble remembering the procmailrc syntax, I use m4 as a preprocessor. Here's how my procmailrc begins (this isn't the script yet):

divert(-1)
changequote(<<, >>)
define(mailinglistrule, 
<<:0:
* $2
Lists/$1
>>)
define(listdt, <<mailinglistrule($1,^Delivered-To:.*$2)>>)
define(listid, <<mailinglistrule($1,^List-Id:.*<$2>)>>)
divert# Generated from .procmailrc.m4 -- DO NOT EDIT

This defines two macros for mailing lists, so e.g. listdt(foo, [email protected]) expands to

:0:
* ^Delivered-To:.*[email protected]
Lists/foo

meaning that emails with a Delivered-To header containing [email protected] should be put in the Lists/foo folder. It also arranges the processed file to begin with a comment that warns me not to edit that file directly.

Now, frankly, m4 scares me: what if I accidentally redefine a macro and procmail starts discarding all my email, or something like that? That's why I have a script, which I call update-procmailrc, that shows me in diff format how my procmailrc is going to change. If the change is just a few lines and looks roughly like what I intended, I can happily approve it, but if there are huge changes to the file, I know to look at my edits more carefully.

#! /bin/sh

PROCMAILRC=.procmailrc
TMPNAM=.procmailrc.tmp.$$
cd $HOME
umask 077
trap "rm -f $TMPNAM" 0

m4 < .procmailrc.m4 > $TMPNAM
diff -u $PROCMAILRC $TMPNAM

echo -n 'Is this acceptable? (y/N) '
read accept

if [ -z "$accept" ]; then
    accept=n
fi

if [ $accept = 'y' -o $accept = 'Y' ]; then
    mv -f $TMPNAM $PROCMAILRC && \
    chmod 400 $PROCMAILRC && \
    echo "Created new $PROCMAILRC"
    if [ "$?" -ne 0 ]; then
        echo "*** FAILED creating $PROCMAILRC"
    fi
else
    echo "Didn't update $PROCMAILRC"
fi

The script hasn't yet prevented any email disasters, but it has made me less anxious about changing my procmailrc.


At some point in the distant past I decided to put all the files for my web host's public_html directory into a subversion repository. Then I wrote a script which:

  1. Creates, mounts, and formats a RAM disk.
  2. Exports the trunk of the repository into the RAM disk.
  3. Calls rsync to upload any changed files from the RAM disk to my hosting provider. I use a public/private key pair to save me from typing my login information each time.
  4. Unmounts the RAM disk.

Thus, pushing updates from the repository to the server is literally a "one touch" operation.

What is most satisfying about the script is that, initially, it was more of a shell scripting exercise than a Grand Project. However, it has probably saved me countless hours of work and makes the prospect of updating a website almost stress-free, maybe more than any other piece of software on my computer.


As a scheduled task, to copy any modified/new files from entire drive d: to backup drive g:, and to log the files copied. It helps me keep track of what I did when, as well.

justdate is a small program to prints the date and time to the screen

g:

cd \drive_d

d:

cd \

type g:\backup_d.log >> g:\logs\backup_d.log

echo ========================================== > g:\backup_d.log

d:\mu\bmutil\justdate >> g:\backup_d.log

xcopy /s /d /y /c . g:\drive_d >> g:\backup_d.log


A Rakefile in my downloads directory containing tasks that copy files from said directory to their respective media archives on external drives. Given my internet speed and storage capacity, it would take me hours out of every week to just copy across and re-name appropriately every piece of media that is downloaded (completely legally, I might add) by hellanzb.

Another very useful task in the same file logs into and scrapes IMDB for episode lists / discographies of all the media I have, and then checks NewzBin for reports that would fill any holes I have.

Combined, this means I have to do absolutely nothing, and in exchange, I wake up every morning with more media than I could possibly consume in that day sitting on my external hard drives.

Did I mention that this is all entirely above-board and legal? d-:

I'll probably merge this all into a sort of command-line media manager/player (farming things out to mplayer), and publish it on GitHub when I have the time.


A perl script that scrapes my local Craigslist, by selected categories, in to a SQL DB which I can then query against.

V2 of this updates the DB with a timer and alerts me if I have a match on any of the queries, basically providing me with a background agent for CL.


This, from a posting in my blog a few months ago, has gone from being an idea that I thought was cool to one of the best little hacks I've coughed up in recent memory. I quote it in full here:

==================

I spend a lot of time in bash. For the uninitiated, bash is a system that you'll find on most unix machines and, thankfully, some windows and every Mac out there. At first blush, it's no more than a command-line interface, and therefore off the radar of most users who see such things as an anachronism they'd rather forget.

I do nearly everything in bash. I READ MY EMAIL FROM A COMMAND LINE, which is why I eschew marked-up email. I navigate directories, edit files, engage in my daily source code checkout and delivery, search for files, search inside files, reboot my machine, and even occasionally browse web pages from the command line. bash is the heart and soul of my digital existence.

The trouble is that I tend to have about 6 bash windows open at a time. At work today, I had one running a web server, another fiddling with my database, a third, fourth, and fifth editing different files, while a sixth was grinding away through my machine trying to record the names of every file on the system. Why? Because it's handy to be able to search through such an archive if you want to know where to find an object by filename.

When you do this, you end up with lots of windows in your control bar named simply, "bash." This is fine if you only have one of them, but its agony when you have 6 or more.... and two dozen other things going on. I have three monitors under the simultaneous command of one keyboard/mouse pair and I still feel the need for more. Each of those windows has several bash terminals open.

So I've plunked this together. First, place these lines in your .bash_profile:

  export PROMPT_COMMAND='export TRIM=`~/bin/trim.pl`'
  export PS1="\[\e]0;\$TRIM\a\]\$TRIM> "
  trap 'CMD=`history|~/bin/hist.pl`;echo -en "\e]0;$TRIM> $CMD\007"' DEBUG

I went through and wrote dozens of paragraphs on how this all works and exactly why it is set up the way it is, but you're not really interested. Trust me. There is an entire chapter of a book in why I did "CMD=...; echo..." on that third line. Many people (including bluehost, where my other domain is hosted) are still using and old version of bash with major bugs in how it handles traps, so we're stuck with this. You can remove the CMD and replace it with $BASH_COMMAND if you are current on your bash version and feel like doing the research.

Anyway, the first script I use is here. It creates a nice prompt that contains your machine name and directory, chopped down to a reasonable length:

                       ============trim.pl===========
  #!/usr/bin/perl

  #It seems that my cygwin box doesn't have HOSTNAME available in the 
  #environment - at least not to scripts - so I'm getting it elsewhere.
  open (IN, "/usr/bin/hostname|");
  $hostname = <IN>;
  close (IN);
  $hostname =~ /^([A-Za-z0-9-]*)/;
  $host_short = $1;

  $preamble = "..." if (length($ENV{"PWD"})>37);

  $ENV{"PWD"} =~ /(.{1,37}$)/;
  $path_short = $1;

  print "$host_short: $preamble$path_short";

                        ==============================

There's a warning at the top of this blog post that you should read now before you start asking stupid questions like, "Why didn't you just use the HOSTNAME environment variable via @ENV?" Simple: Because that doesn't work for all the systems I tried it on.

Now for the really cool bit. Remember line 3 of the .bash_profile addition?

  trap 'CMD=`history|~/bin/hist.pl`;echo -en "\e]0;$TRIM> $CMD\007"' DEBUG

It's dumping the trim.pl script output in the same container as before, printing to both the command prompt and the window title, but this time it's adding the command that you just typed! This is why you don't want to be doing all of this in your .bashrc: any script you run (on my machine, man is one of them) will trigger this thing on every line. man's output gets seriously garbled by what we're doing here. We're not exactly playing nice with the terminal.

To grab the command you just typed, we take the bash's history and dice it up a bit:

                        ===========hist.pl============
#!/usr/bin/perl

while (<STDIN>)
{
        $line = $_
}

chomp $line;
$line =~ /^.{27}(.*)/;
print $1;
                        ==============================

So now, I have a bazillion windows going and they say things like:

  castro: /home/ronb blog
  Ron-D630: /C/ronb/rails/depot script/server
  Ron-D630: /C/ronb/rails/depot mysql -u ron -p
  Ron-D630: /C/ronb/rails/depot find . > /C/ronb/system.map
  Ron-D630: /C/ronb/rails/depot vi app/views/cart.html.erb
  Ron-D630: /C/perforce/depot/ p4 protect
  Ron-D630: /C/perforce/depot/ p4 sync -f
  Ron-D630: /C/perforce/depot/

From the happy little bar at the bottom of the screen, I can now tell which is which at a moment's glance. And because we've set PS1, as soon as a command finishes executing, the command name is replaced by just the output of trim.pl again.

UPDATE (same day): This stuff (the .bash_profile entries) laid all kinds of hell on me when I tried it in my .bashrc. Your .bashrc is executed by non-interactive scripts whenever you invoke bash as a language. I hit this when I was trying to use man. All sorts of garbage (the complete text of my .bashrc, plus escape charecters) showed up at the top of the man page. I would suggest testing this gem with a quick 'man man' invocation at the command line once you get it all together.

I guess it's time for me to pull the custom garbage out of my .bashrc and put it where it belongs...

Incedentally, I found myself typing 'man trap' at one point in this process.


Well an AutoHotkey script that make my life within reach of only a keyboard:

  1. often used app, folder, etc. within one win+ combination. That often means activating the application if already launched, and else launch the application
  2. "double-click" of ctrl to launch Launchy - which leads to a few keypresses from my not so often used apps
  3. add a bunch of missing keyboard shortcuts in windows explorer (XP) such as create new folder, toggle hidden file/show file extension, Ctrl-Enter to open any file as text file in emacs, open command line window (cmd and cygwin shell) with the current path set, etc. etc.
  4. Windows manipulation: move, resize, send to next monitor, max/minimize, toggle always on top, change transparency, etc etc. all with just key combinations
  5. Misc such as hibernate, eject external drives, google any selected word (in any app where ctrl-c as copy works), shutdown timer, etc. etc. Everything with just one key combination

This keyboard script just make me such a happy-camper; and it is in fact the major reason that I'm still using windows instead of linux as my primary platform since autohotkey only works on windows.


My .cmd backup script. It runs on my server every night, and names the backup files according the week day. A full week of backups has saved me (and my family) many times:

:: Backup args:
::   /V Verify? (yes/no)
::   /R Restrict access to owner? (yes/no)
::   /RS Removable storage? (yes/no)
::   /HC Hardware compression (on/off)
::   /M Backup type (normal/copy/differential/incremental/daily)
::   /L Log file type (f/s/n)
::   /D "Description"
::   /J "Job-name"
::   /F "File-name"

SETLOCAL

:: ensure that network drives are mounted
CALL C:\bat\configs\MapShares-home.cmd
echo on

set today=%DATE:~0,3%
if %today%==Mon set yesterday=0Sunday
if %today%==Tue set yesterday=1Monday
if %today%==Wed set yesterday=2Tuesday
if %today%==Thu set yesterday=3Wednesday
if %today%==Fri set yesterday=4Thursday
if %today%==Sat set yesterday=5Friday
if %today%==Sun set yesterday=6Saturday

set configsDir=%~dp0
set storePath=C:\mybackups

:: (eg: Monday C files)
set title=%yesterday% backup set


echo %DATE% %TIME% %title% > "%storePath%\%yesterday%_backup.log"

CALL BackupConfigs.bat

:: Create new BKF file
call C:\WINDOWS\system32\ntbackup.exe backup ^
    "@%configsDir%\daily.bks" ^
    /V:yes /R:no /RS:no /HC:off /M normal /L:s ^
    /D "%title%" ^
    /J "%title%.job" ^
    /F "%storePath%\%yesterday%.bkf" ^
    >> "%storePath%\%yesterday%_backup.log"

echo %DATE% %TIME% Completed >> "%storePath%\%yesterday%_backup.log"

copy "%storePath%\%yesterday%.bkf" "V:\Backups\NEPTUNE"

CALL C:\bat\clean-temps.bat

defrag -v C: > "%storePath%\%yesterday%_defrag.log"

:: display backup directories
start /D"C:\bat\Backups\" checkbkf.bat

ENDLOCAL

::pause


Wrote a script to click my start button, then click it again in half a second, and repeat every 30 seconds.

Keeps me marked Online while at work, and I can get the real work done on my personal laptop right next to it. Not bogged down by work software.

Don't tell the boss :)


A script that reads a config file in the current dir, logs into an FTP account, and uploads all files that have changed since the last time it was run. Really handy for clients who use shared hosting, and FTP is my only option for file access.

http://lucasoman.com/code/updater


A small application that left click (or double-click) every "X" ms for "Y" amount of time. No more need for that drinking bird to work at the nuclear power plant! ;)


On Windows XP, I have set an AT job to run this command daily in C:\

dir /s /b * > dirlist.txt

This lists the full path of all files on the C drive. Then whenever I need to find a file, I can use findstr. This beats using Windows Explorer Search since it allows regular expression matching on the entire path. For example:

findstr ".jpg" dirlist.txt
findstr /i /r "windows.*system32.*png$" dirlist.txt

This is a very fast solution to set up, and great if you find yourself with a fresh Windows install and no internet connection.

If you need to search within certain file types for some pattern, first list all of the files you need to check, then search within them. For example, to find a Java or Python program that flips an image you could do this:

findstr "\.java \.py" dirlist.txt > narrowlist.txt
findstr /i /r /f:narrowlist.txt "flip.*image"

The most useful? But there are so many...

  1. d.cmd contains: @dir /ad /on
  2. dd.cmd contains: @dir /a-d /on
  3. x.cmd contains: @exit
  4. s.cmd contains: @start .
  5. sx.cmd contains: @start . & exit
  6. ts.cmd contains the following, which allows me to properly connect to another machine's console session over RDP regardless of whether I'm on Vista SP1 or not.

    @echo off

    ver | find "6.0.6001"

    if ERRORLEVEL 0 if not errorlevel 1 (set TSCONS=admin) ELSE set TSCONS=console

    echo Issuing command: mstsc /%TSCONS% /v %1

    start mstsc /%TSCONS% /v %1

(Sorry for the weird formatting, apparently you can't have more than one code sample per answer?)

From a command prompt I'll navigate to where my VS solution file is, and then I'll want to open it, but I'm too lazy to type blah.sln and press enter. So I wrote sln.cmd:

@echo off
if not exist *.sln goto csproj
for %%f in (*.sln) do start /max %%f
goto end

:csproj
for %%f in (*.csproj) do start /max %%f
goto end

:end

So I just type sln and press enter and it opens the solution file, if any, in the current directory. I wrap things like pushd and popd in pd.cmd and pop.cmd.


This, from a posting in my blog a few months ago, has gone from being an idea that I thought was cool to one of the best little hacks I've coughed up in recent memory. I quote it in full here:

==================

I spend a lot of time in bash. For the uninitiated, bash is a system that you'll find on most unix machines and, thankfully, some windows and every Mac out there. At first blush, it's no more than a command-line interface, and therefore off the radar of most users who see such things as an anachronism they'd rather forget.

I do nearly everything in bash. I READ MY EMAIL FROM A COMMAND LINE, which is why I eschew marked-up email. I navigate directories, edit files, engage in my daily source code checkout and delivery, search for files, search inside files, reboot my machine, and even occasionally browse web pages from the command line. bash is the heart and soul of my digital existence.

The trouble is that I tend to have about 6 bash windows open at a time. At work today, I had one running a web server, another fiddling with my database, a third, fourth, and fifth editing different files, while a sixth was grinding away through my machine trying to record the names of every file on the system. Why? Because it's handy to be able to search through such an archive if you want to know where to find an object by filename.

When you do this, you end up with lots of windows in your control bar named simply, "bash." This is fine if you only have one of them, but its agony when you have 6 or more.... and two dozen other things going on. I have three monitors under the simultaneous command of one keyboard/mouse pair and I still feel the need for more. Each of those windows has several bash terminals open.

So I've plunked this together. First, place these lines in your .bash_profile:

  export PROMPT_COMMAND='export TRIM=`~/bin/trim.pl`'
  export PS1="\[\e]0;\$TRIM\a\]\$TRIM> "
  trap 'CMD=`history|~/bin/hist.pl`;echo -en "\e]0;$TRIM> $CMD\007"' DEBUG

I went through and wrote dozens of paragraphs on how this all works and exactly why it is set up the way it is, but you're not really interested. Trust me. There is an entire chapter of a book in why I did "CMD=...; echo..." on that third line. Many people (including bluehost, where my other domain is hosted) are still using and old version of bash with major bugs in how it handles traps, so we're stuck with this. You can remove the CMD and replace it with $BASH_COMMAND if you are current on your bash version and feel like doing the research.

Anyway, the first script I use is here. It creates a nice prompt that contains your machine name and directory, chopped down to a reasonable length:

                       ============trim.pl===========
  #!/usr/bin/perl

  #It seems that my cygwin box doesn't have HOSTNAME available in the 
  #environment - at least not to scripts - so I'm getting it elsewhere.
  open (IN, "/usr/bin/hostname|");
  $hostname = <IN>;
  close (IN);
  $hostname =~ /^([A-Za-z0-9-]*)/;
  $host_short = $1;

  $preamble = "..." if (length($ENV{"PWD"})>37);

  $ENV{"PWD"} =~ /(.{1,37}$)/;
  $path_short = $1;

  print "$host_short: $preamble$path_short";

                        ==============================

There's a warning at the top of this blog post that you should read now before you start asking stupid questions like, "Why didn't you just use the HOSTNAME environment variable via @ENV?" Simple: Because that doesn't work for all the systems I tried it on.

Now for the really cool bit. Remember line 3 of the .bash_profile addition?

  trap 'CMD=`history|~/bin/hist.pl`;echo -en "\e]0;$TRIM> $CMD\007"' DEBUG

It's dumping the trim.pl script output in the same container as before, printing to both the command prompt and the window title, but this time it's adding the command that you just typed! This is why you don't want to be doing all of this in your .bashrc: any script you run (on my machine, man is one of them) will trigger this thing on every line. man's output gets seriously garbled by what we're doing here. We're not exactly playing nice with the terminal.

To grab the command you just typed, we take the bash's history and dice it up a bit:

                        ===========hist.pl============
#!/usr/bin/perl

while (<STDIN>)
{
        $line = $_
}

chomp $line;
$line =~ /^.{27}(.*)/;
print $1;
                        ==============================

So now, I have a bazillion windows going and they say things like:

  castro: /home/ronb blog
  Ron-D630: /C/ronb/rails/depot script/server
  Ron-D630: /C/ronb/rails/depot mysql -u ron -p
  Ron-D630: /C/ronb/rails/depot find . > /C/ronb/system.map
  Ron-D630: /C/ronb/rails/depot vi app/views/cart.html.erb
  Ron-D630: /C/perforce/depot/ p4 protect
  Ron-D630: /C/perforce/depot/ p4 sync -f
  Ron-D630: /C/perforce/depot/

From the happy little bar at the bottom of the screen, I can now tell which is which at a moment's glance. And because we've set PS1, as soon as a command finishes executing, the command name is replaced by just the output of trim.pl again.

UPDATE (same day): This stuff (the .bash_profile entries) laid all kinds of hell on me when I tried it in my .bashrc. Your .bashrc is executed by non-interactive scripts whenever you invoke bash as a language. I hit this when I was trying to use man. All sorts of garbage (the complete text of my .bashrc, plus escape charecters) showed up at the top of the man page. I would suggest testing this gem with a quick 'man man' invocation at the command line once you get it all together.

I guess it's time for me to pull the custom garbage out of my .bashrc and put it where it belongs...

Incedentally, I found myself typing 'man trap' at one point in this process.


Various Shortcuts to "net start" and "net stop" commands so I can start and stop services without having to go into the Services MMC


This, from a posting in my blog a few months ago, has gone from being an idea that I thought was cool to one of the best little hacks I've coughed up in recent memory. I quote it in full here:

==================

I spend a lot of time in bash. For the uninitiated, bash is a system that you'll find on most unix machines and, thankfully, some windows and every Mac out there. At first blush, it's no more than a command-line interface, and therefore off the radar of most users who see such things as an anachronism they'd rather forget.

I do nearly everything in bash. I READ MY EMAIL FROM A COMMAND LINE, which is why I eschew marked-up email. I navigate directories, edit files, engage in my daily source code checkout and delivery, search for files, search inside files, reboot my machine, and even occasionally browse web pages from the command line. bash is the heart and soul of my digital existence.

The trouble is that I tend to have about 6 bash windows open at a time. At work today, I had one running a web server, another fiddling with my database, a third, fourth, and fifth editing different files, while a sixth was grinding away through my machine trying to record the names of every file on the system. Why? Because it's handy to be able to search through such an archive if you want to know where to find an object by filename.

When you do this, you end up with lots of windows in your control bar named simply, "bash." This is fine if you only have one of them, but its agony when you have 6 or more.... and two dozen other things going on. I have three monitors under the simultaneous command of one keyboard/mouse pair and I still feel the need for more. Each of those windows has several bash terminals open.

So I've plunked this together. First, place these lines in your .bash_profile:

  export PROMPT_COMMAND='export TRIM=`~/bin/trim.pl`'
  export PS1="\[\e]0;\$TRIM\a\]\$TRIM> "
  trap 'CMD=`history|~/bin/hist.pl`;echo -en "\e]0;$TRIM> $CMD\007"' DEBUG

I went through and wrote dozens of paragraphs on how this all works and exactly why it is set up the way it is, but you're not really interested. Trust me. There is an entire chapter of a book in why I did "CMD=...; echo..." on that third line. Many people (including bluehost, where my other domain is hosted) are still using and old version of bash with major bugs in how it handles traps, so we're stuck with this. You can remove the CMD and replace it with $BASH_COMMAND if you are current on your bash version and feel like doing the research.

Anyway, the first script I use is here. It creates a nice prompt that contains your machine name and directory, chopped down to a reasonable length:

                       ============trim.pl===========
  #!/usr/bin/perl

  #It seems that my cygwin box doesn't have HOSTNAME available in the 
  #environment - at least not to scripts - so I'm getting it elsewhere.
  open (IN, "/usr/bin/hostname|");
  $hostname = <IN>;
  close (IN);
  $hostname =~ /^([A-Za-z0-9-]*)/;
  $host_short = $1;

  $preamble = "..." if (length($ENV{"PWD"})>37);

  $ENV{"PWD"} =~ /(.{1,37}$)/;
  $path_short = $1;

  print "$host_short: $preamble$path_short";

                        ==============================

There's a warning at the top of this blog post that you should read now before you start asking stupid questions like, "Why didn't you just use the HOSTNAME environment variable via @ENV?" Simple: Because that doesn't work for all the systems I tried it on.

Now for the really cool bit. Remember line 3 of the .bash_profile addition?

  trap 'CMD=`history|~/bin/hist.pl`;echo -en "\e]0;$TRIM> $CMD\007"' DEBUG

It's dumping the trim.pl script output in the same container as before, printing to both the command prompt and the window title, but this time it's adding the command that you just typed! This is why you don't want to be doing all of this in your .bashrc: any script you run (on my machine, man is one of them) will trigger this thing on every line. man's output gets seriously garbled by what we're doing here. We're not exactly playing nice with the terminal.

To grab the command you just typed, we take the bash's history and dice it up a bit:

                        ===========hist.pl============
#!/usr/bin/perl

while (<STDIN>)
{
        $line = $_
}

chomp $line;
$line =~ /^.{27}(.*)/;
print $1;
                        ==============================

So now, I have a bazillion windows going and they say things like:

  castro: /home/ronb blog
  Ron-D630: /C/ronb/rails/depot script/server
  Ron-D630: /C/ronb/rails/depot mysql -u ron -p
  Ron-D630: /C/ronb/rails/depot find . > /C/ronb/system.map
  Ron-D630: /C/ronb/rails/depot vi app/views/cart.html.erb
  Ron-D630: /C/perforce/depot/ p4 protect
  Ron-D630: /C/perforce/depot/ p4 sync -f
  Ron-D630: /C/perforce/depot/

From the happy little bar at the bottom of the screen, I can now tell which is which at a moment's glance. And because we've set PS1, as soon as a command finishes executing, the command name is replaced by just the output of trim.pl again.

UPDATE (same day): This stuff (the .bash_profile entries) laid all kinds of hell on me when I tried it in my .bashrc. Your .bashrc is executed by non-interactive scripts whenever you invoke bash as a language. I hit this when I was trying to use man. All sorts of garbage (the complete text of my .bashrc, plus escape charecters) showed up at the top of the man page. I would suggest testing this gem with a quick 'man man' invocation at the command line once you get it all together.

I guess it's time for me to pull the custom garbage out of my .bashrc and put it where it belongs...

Incedentally, I found myself typing 'man trap' at one point in this process.


A similar backup.sh for each project, that tars and gzips just the source, moves it into a snapshot directory and labels it with timestamp: project-mmddyy-hhmmss. Useful for coding between commits.


I use a DOS program that errors out if it's past a certain date. I just looked at the batch file that it was using to start up and changed it so it would first change the date to 2000, then run the program. On the program's exit, it changed the date back to what it was before it was changed.


A Greasemonkey script which removes obviously stupid[*] comments from gaming site Kotaku.com.

[*] As identified by common spelling mistakes, all-caps writing, excessive use of "LOL" and similar heuristics.


Best real-life script?

Me: (Enters room) "Boss, I want a raise."

Boss: (Offers chair from behind desk) "A raise? Please, take my job!"

Then again, that may be the worst script!


Super remote reset button.
A rack of super special simulation hardware (backin the days when a room full of VME crates did less than your GPU) that a user on the other side of the world would crash in the early hours of the morning. It took an hour to get into the lab and through security.

But we weren't allowed to connect to the super special controller or modify the hardware. The solution was an old DEC workstation with an epson dot matrix printer, tape a plastic ruler to the paper feed knob, position the printer near the reset button.
Log in to the WS as a regular user (no root allowed, all external ports locked down), print a document with 24blank lines - which rotated the paper feed knob and the ruler pressed over the reset on the super special hardware.


A script that runs hourly to retrain my spam filters based two IMAP folder where span and ham are put.

#!/bin/sh
FNDIR="train-as-spam"
FPDIR="train-as-ham"

for dir in /home/*/.maildir
do
    cd "${dir}"
    USER=`stat -c %U .`

    SRCDIR="${dir}/.${FNDIR}"
    if [ ! -d ${SRCDIR} ]; then
        echo no "${SRCDIR}" directory
    else
        cd "${SRCDIR}/cur"
        ls -tr | while read file
        do
            if grep -q "^X-DSPAM" "${file}"; then
                SOURCE="error"
            else
                SOURCE="corpus"
            fi

            dspam --user "${USER}" --class=spam --source="${SOURCE}" --deliver=innocent,spam --stdout < "${file}" > "../tmp/${file}"
            mv "../tmp/${file}" "${dir}/new/${file%%:*}" && rm "${file}"
        done
    fi

    SRCDIR="${dir}/.${FPDIR}"
    if [ ! -d ${SRCDIR} ]; then
        echo no "${SRCDIR}" directory
    else
        cd "${SRCDIR}/cur"
        ls -tr | while read file
        do
            if grep -q "^X-DSPAM" "${file}"; then
                SOURCE="error"
            else
                SOURCE="corpus"
            fi

            dspam --user "${USER}" --class=innocent --source="${SOURCE}" --deliver=innocent,spam --stdout < "${file}" > "../tmp/${file}"
            mv "../tmp/${file}" "${dir}/new/${file%%:*}" && rm "${file}"
        done
    fi

done

A simply Python script that converts line endings from Unix to Windows that I stuck in my system32 directory. It's been lost to the ages for a few months, now, but basically it'd convert a list of known text-based file types to Windows line endings, and you could specify which files to convert, or all files, for a wildcard list.


alias dir='ls -al' is my preferred favorite script.


A few years ago I wrote a winforms app with the help of a few win32 api's to completely lock myself out of my computer for an hour so that it would force me to go and exercise. Because I was lazy? No... because I had a personal fitness goal. Sometimes you just need a little kick to get started :)


VBS script to create a YYYY/YYYY-MM/YYYY-MM-DD file structure in my photos folder and move photos from my camera to the appropriate folder.


Wrote a little bash script that knew just enough about fonts to search through about 10k fonts and look for certain key words, in spite of their useless filenames but not return very many false positives. Took a while to run, about a minute on the dinky iMac, but it has saved me probably 50 hrs over the course of the last few years.



For those of us who don't remember where we are on unix, or which SID we are using.
Pop this in your .profile.

<br>function CD
<br>{
<br>   unalias cd
<br>   command cd "$@" && PS1="\${ORACLE_SID}:$(hostname):$PWD> "
<br>   alias cd=CD
<br>}
<br>
alias cd=CD

I wrote a simple VB app that tracked which game numbers of Freecell I had played and successfully completed, and always launched it with a different seed.

....starting from 1....

Max game number is 65k. Rather sadly after more than 5 years I am still in only the hundreds. But at least I know I've never played the same hand twice!

** Postscript - it's the only VB app I've ever written. I ran screaming back to C++....


Anime CRC32 checksum:

#!/usr/bin/python                                                                                                                                                                                  

import sys, re, zlib

c_null="^[[00;00m"
c_red="^[[31;01m"
c_green="^[[32;01m"

def crc_checksum(filename):
    filedata = open(filename, "rb").read()
    sum = zlib.crc32(filedata)
    if sum < 0:
        sum &= 16**8-1
    return "%.8X" %(sum)

for file in sys.argv[1:]:
    sum = crc_checksum(file)
    try:
        dest_sum = re.split('[\[\]]', file)[-2]
        if sum == dest_sum:
            c_in = c_green
        else:
            c_in = c_red
        sfile = file.split(dest_sum)
        print "%s%s%s   %s%s%s%s%s" % (c_in, sum, c_null, sfile[0], c_in, dest_sum, c_null, sfile[1])
    except IndexError:
        print "%s   %s" %(sum, file)

Wrote a little bash script that knew just enough about fonts to search through about 10k fonts and look for certain key words, in spite of their useless filenames but not return very many false positives. Took a while to run, about a minute on the dinky iMac, but it has saved me probably 50 hrs over the course of the last few years.


MySQL backup. I made a Windows batch script that would create incremental backups of MySQL databases, create a fresh dump every day and back them up every 10 minutes on a remote server. It saved my ass countless times, especially in the countless situations where a client would call, yelling their head off that a record just "disappeared" from the database. I went "no problem, let's see what happened" because I also wrote a binary search script that would look for the last moment when a record was present in the database. From there it would be pretty easy to understand who "stole" it and why.
You wouldn't imagine how useful these have been and I've been using them for almost 5 years. I wouldn't switch to anything else simply because they've been roughly tested and they're custom made, meaning they do exactly what I need and nothing more but I've tweaked them so much that it would be a snap to add extra functionalities.
So, my "masterpiece" is a MySQL incremental backup + remote backup + logs search system for Windows. I also wrote a version for Linux but I've lost it somewhere, probably because it was only about 15 lines + a cron job instead of Windows' about 1,200 lines + two scheduled tasks.


"backup.sh" that tars up the contents of a directory and sends it to my gmail account.


I wrote a script for formatting C source files that automatically indents the code using an appropriate combination of tab and space characters, such that the file will appear correct regardless of what the tab setting on your editor is.

Source code is here.


I like to store my photos in a directory based on the date the picture was taken. Therefore I wrote a program that would scan a memory card for pictures, create any folders on my hard disk that it needed to based on the dates of the pictures, then copy them in.


#!/usr/bin/perl
use strict;
use utf8;
use Encode;
use File::Find;
binmode STDOUT, ':utf8';
sub orderly {
    my ($x, $y) = @_{$a, $b};
    if (my $z = $x <=> $y) {return $z}
    $x = length $a;
    $y = length $b;
    my $z = $x < $y ? $x : $y;
    if (substr($a, 0, $z) eq substr($b, 0, $z)) {
        return $y <=> $x;
    }
    else {
        return $a cmp $b;
    }
}
my %conf = map +($_ => 0), split //, 'acsxL';
sub Stat {$conf{L} ? lstat : stat}
my @dirs = ();
while (defined ($_ = shift)) {
    if ($_ eq "--") {push @dirs, @ARGV; last}
    elsif (/^-(.*)$/s) {
        for (split //, $1) {
            if (!exists $conf{$_} or $conf{$_} = 1 and $conf{a} and $conf{s}) {
                print STDERR "$0 [-a] [-c] [-s] [-x] [-L] [--] ...\n";
                exit 1;
            }
        }
    }
    else {push @dirs, $_}
}
s/\/*$//s for @dirs;  # */ SO has crappy syntax highlighting
@dirs = qw(.) unless @dirs;
my %spec = (follow => $conf{L}, no_chdir => 1);
if ($conf{a}) {
    $spec{wanted} = sub {
        Stat;
        my $s = -f _ ? -s _ : 0;
        decode(utf8 => $File::Find::name) =~ /^\Q$dirs[0]\E\/?(.*)$/s;
        my @a = split /\//, $1;
        for (unshift @a, $dirs[0]; @a; pop @a) {
            $_{join "/", @a} += $s;
        }
    };
}
elsif ($conf{s}) {
    $spec{wanted} = sub {
        Stat;
        $_{$dirs[0]} += -f _ ? -s _ : 0;
    };
}
else {
    $spec{wanted} = sub {
        Stat;
        my $s = -f _ ? -s _ : 0;
        decode(utf8 => $File::Find::name) =~ /^\Q$dirs[0]\E\/?(.*)$/s;
        my @a = split /\//, $1;
        ! -d _ and pop @a;
        for (unshift @a, $dirs[0]; @a; pop @a) {
            $_{join "/", @a} += $s;
        }
    };
}
if ($conf{x}) {
    $spec{preprocess} = sub {
        my $dev = (Stat $File::Find::dir)[0];
        grep {$dev == (Stat "$File::Find::dir/$_")[0]} @_;
    };
}
while (@dirs) {
    find(\%spec, $dirs[0] eq "" ? "/" : $dirs[0]);
    $_{""} += $_{$dirs[0]} if $conf{c};
    shift @dirs;
}
$_{$_} < 1024 ** 1 ? printf "%s «%-6.6sB» %s\n", $_{$_}, sprintf("%6.6f", "$_{$_}" / 1024 ** 0), $_ :
$_{$_} < 1024 ** 2 ? printf "%s «%-6.6sK» %s\n", $_{$_}, sprintf("%6.6f", "$_{$_}" / 1024 ** 1), $_ :
$_{$_} < 1024 ** 3 ? printf "%s «%-6.6sM» %s\n", $_{$_}, sprintf("%6.6f", "$_{$_}" / 1024 ** 2), $_ :
$_{$_} < 1024 ** 4 ? printf "%s «%-6.6sG» %s\n", $_{$_}, sprintf("%6.6f", "$_{$_}" / 1024 ** 3), $_ :
$_{$_} < 1024 ** 5 ? printf "%s «%-6.6sT» %s\n", $_{$_}, sprintf("%6.6f", "$_{$_}" / 1024 ** 4), $_ :
$_{$_} < 1024 ** 6 ? printf "%s «%-6.6sP» %s\n", $_{$_}, sprintf("%6.6f", "$_{$_}" / 1024 ** 5), $_ :
$_{$_} < 1024 ** 7 ? printf "%s «%-6.6sE» %s\n", $_{$_}, sprintf("%6.6f", "$_{$_}" / 1024 ** 6), $_ :
$_{$_} < 1024 ** 8 ? printf "%s «%-6.6sZ» %s\n", $_{$_}, sprintf("%6.6f", "$_{$_}" / 1024 ** 7), $_ :
                     printf "%s «%-6.6sY» %s\n", $_{$_}, sprintf("%6.6f", "$_{$_}" / 1024 ** 8), $_
    for grep {$_{$_} > 0} sort orderly keys %_;

I save it in ~/bin/dush, it acts as a sort of du -h/du | sort -n hybrid: sorts and gives human-readable sizes all at once. Very useful for finding what's taking up disk space.

In a similar vein,

#!/usr/bin/perl
$t = 1;
%p = map {$_ => ($t *= 1024)} qw(K M G T P E Z Y);
$t = 4707319808;
if (@ARGV) {
    if (($_ = shift) =~ /^-*dvd/i) {$t = 4707319808}
    elsif (/^-*cd[^w]*$/i) {$t = 737280000}
    elsif (/^-*cd/i) {$t = 681984000}
    elsif (/^-*([\d.]+)([kmgtpezy])/i) {$t = $1 * ($p{"\U$2"} || 1)}
    elsif (/^-*([\d.]+)/) {$t = $1}
    else {unshift @ARGV, $_}
}
($q, $r, $s) = (0, ($ENV{COLUMNS} || 80) - 13, $t);
while (<>) {
    chomp, stat;
    unless (-e _) {
        print STDERR "$_ does not exist\n";
        next;
    }
    if (($s += -s _) > $t) {
        $s && $s < $t && printf "-%7s %s\n",
            sprintf("%2.3f%%", 100 * ($t - $s) / $t), $t - $s;
        printf "-----------%d%*s\n", ++$q, $r, "-" x $r;
        $s = -s _;
    }
    printf "%8s %s\n",
        sprintf("%3.3f%%", $s * 100 / $t),
        /.{4}(.{$r})$/s ? "...$1" : $_;
}
$s && $s < $t && printf "-%7s %s\n",
    sprintf("%2.3f%%", 100 * ($t - $s) / $t), $t - $s;

I save this as ~/bin/fit. When I'm archiving a bunch of files, I run ls | fit or ls | fit -cdrw to help determine if it'll fit on a DVD/CD/CDRW, and where to split them if they don't.


On Windows XP, I have set an AT job to run this command daily in C:\

dir /s /b * > dirlist.txt

This lists the full path of all files on the C drive. Then whenever I need to find a file, I can use findstr. This beats using Windows Explorer Search since it allows regular expression matching on the entire path. For example:

findstr ".jpg" dirlist.txt
findstr /i /r "windows.*system32.*png$" dirlist.txt

This is a very fast solution to set up, and great if you find yourself with a fresh Windows install and no internet connection.

If you need to search within certain file types for some pattern, first list all of the files you need to check, then search within them. For example, to find a Java or Python program that flips an image you could do this:

findstr "\.java \.py" dirlist.txt > narrowlist.txt
findstr /i /r /f:narrowlist.txt "flip.*image"

I wrote a simple Ruby script to help my wife and I when we were considering names for our first child. It generated all name combinations and checked the initials against a blacklist of initials I wanted to avoid, excluding any that didn't match my criteria. It felt like an appropriate thing for a nerdy dad to do and actually proved to be quite worthwhile and useful.

Other than that I've written a couple of Python scripts that serve as IRC bots which I use every day. One saves URLs, via regular expression matching, to delicious. Another serves as a simple IRC Twitter interface, allowing me to check my feed and post updates.


A small task-bar program that extracted every error-code constant out of a third-party JavaDoc and let me lookup the constant-name for a given error code. Plus, add in any conversions from HEX to decimal, etc.

This comes up a lot when working in the debugger--you get back the error code, but then tracking back the code to text is a huge pain. It's even more common when working with software that wraps native methods, OS calls, or COM... often times, the constants are copied straight out of an error header file with no additional context, repeated values, and no enumerations.


A Rakefile in my downloads directory containing tasks that copy files from said directory to their respective media archives on external drives. Given my internet speed and storage capacity, it would take me hours out of every week to just copy across and re-name appropriately every piece of media that is downloaded (completely legally, I might add) by hellanzb.

Another very useful task in the same file logs into and scrapes IMDB for episode lists / discographies of all the media I have, and then checks NewzBin for reports that would fill any holes I have.

Combined, this means I have to do absolutely nothing, and in exchange, I wake up every morning with more media than I could possibly consume in that day sitting on my external hard drives.

Did I mention that this is all entirely above-board and legal? d-:

I'll probably merge this all into a sort of command-line media manager/player (farming things out to mplayer), and publish it on GitHub when I have the time.


I often use a MS Word macro that takes a source-code file, formats it in two columns of monospaced type on a landscape page, numbers the lines, and adds company header and footer info such as filename, print date, page number, and confidentiality statement.

Printing both sides of the page uses about 1/4 the paper as the equivalent lpr command. (Does anyone use lpr anymore???)


Sometimes I forget what are the most recent files I just created in a directory, but a ls command will just show every file in the directory, I just want a few most recent files so I put this in my .cshrc

 ls -l -t | awk 'NR<15{print $0}'

(Actually it is in a file called lt and in the .cshrc it is set with: alias lt '~/lt')

So now lt will show me only a few files.


I often use a MS Word macro that takes a source-code file, formats it in two columns of monospaced type on a landscape page, numbers the lines, and adds company header and footer info such as filename, print date, page number, and confidentiality statement.

Printing both sides of the page uses about 1/4 the paper as the equivalent lpr command. (Does anyone use lpr anymore???)


I use procmail to sort my incoming email to different folders. Because I have trouble remembering the procmailrc syntax, I use m4 as a preprocessor. Here's how my procmailrc begins (this isn't the script yet):

divert(-1)
changequote(<<, >>)
define(mailinglistrule, 
<<:0:
* $2
Lists/$1
>>)
define(listdt, <<mailinglistrule($1,^Delivered-To:.*$2)>>)
define(listid, <<mailinglistrule($1,^List-Id:.*<$2>)>>)
divert# Generated from .procmailrc.m4 -- DO NOT EDIT

This defines two macros for mailing lists, so e.g. listdt(foo, [email protected]) expands to

:0:
* ^Delivered-To:.*[email protected]
Lists/foo

meaning that emails with a Delivered-To header containing [email protected] should be put in the Lists/foo folder. It also arranges the processed file to begin with a comment that warns me not to edit that file directly.

Now, frankly, m4 scares me: what if I accidentally redefine a macro and procmail starts discarding all my email, or something like that? That's why I have a script, which I call update-procmailrc, that shows me in diff format how my procmailrc is going to change. If the change is just a few lines and looks roughly like what I intended, I can happily approve it, but if there are huge changes to the file, I know to look at my edits more carefully.

#! /bin/sh

PROCMAILRC=.procmailrc
TMPNAM=.procmailrc.tmp.$$
cd $HOME
umask 077
trap "rm -f $TMPNAM" 0

m4 < .procmailrc.m4 > $TMPNAM
diff -u $PROCMAILRC $TMPNAM

echo -n 'Is this acceptable? (y/N) '
read accept

if [ -z "$accept" ]; then
    accept=n
fi

if [ $accept = 'y' -o $accept = 'Y' ]; then
    mv -f $TMPNAM $PROCMAILRC && \
    chmod 400 $PROCMAILRC && \
    echo "Created new $PROCMAILRC"
    if [ "$?" -ne 0 ]; then
        echo "*** FAILED creating $PROCMAILRC"
    fi
else
    echo "Didn't update $PROCMAILRC"
fi

The script hasn't yet prevented any email disasters, but it has made me less anxious about changing my procmailrc.


This, from a posting in my blog a few months ago, has gone from being an idea that I thought was cool to one of the best little hacks I've coughed up in recent memory. I quote it in full here:

==================

I spend a lot of time in bash. For the uninitiated, bash is a system that you'll find on most unix machines and, thankfully, some windows and every Mac out there. At first blush, it's no more than a command-line interface, and therefore off the radar of most users who see such things as an anachronism they'd rather forget.

I do nearly everything in bash. I READ MY EMAIL FROM A COMMAND LINE, which is why I eschew marked-up email. I navigate directories, edit files, engage in my daily source code checkout and delivery, search for files, search inside files, reboot my machine, and even occasionally browse web pages from the command line. bash is the heart and soul of my digital existence.

The trouble is that I tend to have about 6 bash windows open at a time. At work today, I had one running a web server, another fiddling with my database, a third, fourth, and fifth editing different files, while a sixth was grinding away through my machine trying to record the names of every file on the system. Why? Because it's handy to be able to search through such an archive if you want to know where to find an object by filename.

When you do this, you end up with lots of windows in your control bar named simply, "bash." This is fine if you only have one of them, but its agony when you have 6 or more.... and two dozen other things going on. I have three monitors under the simultaneous command of one keyboard/mouse pair and I still feel the need for more. Each of those windows has several bash terminals open.

So I've plunked this together. First, place these lines in your .bash_profile:

  export PROMPT_COMMAND='export TRIM=`~/bin/trim.pl`'
  export PS1="\[\e]0;\$TRIM\a\]\$TRIM> "
  trap 'CMD=`history|~/bin/hist.pl`;echo -en "\e]0;$TRIM> $CMD\007"' DEBUG

I went through and wrote dozens of paragraphs on how this all works and exactly why it is set up the way it is, but you're not really interested. Trust me. There is an entire chapter of a book in why I did "CMD=...; echo..." on that third line. Many people (including bluehost, where my other domain is hosted) are still using and old version of bash with major bugs in how it handles traps, so we're stuck with this. You can remove the CMD and replace it with $BASH_COMMAND if you are current on your bash version and feel like doing the research.

Anyway, the first script I use is here. It creates a nice prompt that contains your machine name and directory, chopped down to a reasonable length:

                       ============trim.pl===========
  #!/usr/bin/perl

  #It seems that my cygwin box doesn't have HOSTNAME available in the 
  #environment - at least not to scripts - so I'm getting it elsewhere.
  open (IN, "/usr/bin/hostname|");
  $hostname = <IN>;
  close (IN);
  $hostname =~ /^([A-Za-z0-9-]*)/;
  $host_short = $1;

  $preamble = "..." if (length($ENV{"PWD"})>37);

  $ENV{"PWD"} =~ /(.{1,37}$)/;
  $path_short = $1;

  print "$host_short: $preamble$path_short";

                        ==============================

There's a warning at the top of this blog post that you should read now before you start asking stupid questions like, "Why didn't you just use the HOSTNAME environment variable via @ENV?" Simple: Because that doesn't work for all the systems I tried it on.

Now for the really cool bit. Remember line 3 of the .bash_profile addition?

  trap 'CMD=`history|~/bin/hist.pl`;echo -en "\e]0;$TRIM> $CMD\007"' DEBUG

It's dumping the trim.pl script output in the same container as before, printing to both the command prompt and the window title, but this time it's adding the command that you just typed! This is why you don't want to be doing all of this in your .bashrc: any script you run (on my machine, man is one of them) will trigger this thing on every line. man's output gets seriously garbled by what we're doing here. We're not exactly playing nice with the terminal.

To grab the command you just typed, we take the bash's history and dice it up a bit:

                        ===========hist.pl============
#!/usr/bin/perl

while (<STDIN>)
{
        $line = $_
}

chomp $line;
$line =~ /^.{27}(.*)/;
print $1;
                        ==============================

So now, I have a bazillion windows going and they say things like:

  castro: /home/ronb blog
  Ron-D630: /C/ronb/rails/depot script/server
  Ron-D630: /C/ronb/rails/depot mysql -u ron -p
  Ron-D630: /C/ronb/rails/depot find . > /C/ronb/system.map
  Ron-D630: /C/ronb/rails/depot vi app/views/cart.html.erb
  Ron-D630: /C/perforce/depot/ p4 protect
  Ron-D630: /C/perforce/depot/ p4 sync -f
  Ron-D630: /C/perforce/depot/

From the happy little bar at the bottom of the screen, I can now tell which is which at a moment's glance. And because we've set PS1, as soon as a command finishes executing, the command name is replaced by just the output of trim.pl again.

UPDATE (same day): This stuff (the .bash_profile entries) laid all kinds of hell on me when I tried it in my .bashrc. Your .bashrc is executed by non-interactive scripts whenever you invoke bash as a language. I hit this when I was trying to use man. All sorts of garbage (the complete text of my .bashrc, plus escape charecters) showed up at the top of the man page. I would suggest testing this gem with a quick 'man man' invocation at the command line once you get it all together.

I guess it's time for me to pull the custom garbage out of my .bashrc and put it where it belongs...

Incedentally, I found myself typing 'man trap' at one point in this process.


I've written a small shell script, tapt, for Debian based system. esp. Ubuntu. What it basically does is to post all your "apt-get" activities to your twitter account. It helps me to keep the track of what and when I've installed/remove programs in my Ubuntu system. I created a new Twitter account just for this and kept it private. Really useful. More information here: http://www.quicktweaks.com/tapt/


A small task-bar program that extracted every error-code constant out of a third-party JavaDoc and let me lookup the constant-name for a given error code. Plus, add in any conversions from HEX to decimal, etc.

This comes up a lot when working in the debugger--you get back the error code, but then tracking back the code to text is a huge pain. It's even more common when working with software that wraps native methods, OS calls, or COM... often times, the constants are copied straight out of an error header file with no additional context, repeated values, and no enumerations.


Well an AutoHotkey script that make my life within reach of only a keyboard:

  1. often used app, folder, etc. within one win+ combination. That often means activating the application if already launched, and else launch the application
  2. "double-click" of ctrl to launch Launchy - which leads to a few keypresses from my not so often used apps
  3. add a bunch of missing keyboard shortcuts in windows explorer (XP) such as create new folder, toggle hidden file/show file extension, Ctrl-Enter to open any file as text file in emacs, open command line window (cmd and cygwin shell) with the current path set, etc. etc.
  4. Windows manipulation: move, resize, send to next monitor, max/minimize, toggle always on top, change transparency, etc etc. all with just key combinations
  5. Misc such as hibernate, eject external drives, google any selected word (in any app where ctrl-c as copy works), shutdown timer, etc. etc. Everything with just one key combination

This keyboard script just make me such a happy-camper; and it is in fact the major reason that I'm still using windows instead of linux as my primary platform since autohotkey only works on windows.


copy con c.bat
c:
cd\
cls
^Z

I used to work at a technology summer camp, and we had to compose these write-ups for each of the kids in the group at the end of the week, which they would then receive and take home as a keepsake. Usually, these consisted of a bunch of generic sentences, and one to two personalized sentences. I wrote a python script which constructed one of these write-ups out of a bank of canned sentences, and allowed the user to add a couple of personalized sentences in the middle. This saved a huge amount of time for me and other counselors I let in on the secret. Even though so much of it was automated, our write-ups still looked better than many of the 'honest' ones, because we could put more time into the personalized parts.


A little script that monitors some popular websites for ads that match my skills and email me an email.


I like to store my photos in a directory based on the date the picture was taken. Therefore I wrote a program that would scan a memory card for pictures, create any folders on my hard disk that it needed to based on the dates of the pictures, then copy them in.


I got a script which extracts id3 tags encodes the songs newly in a certain format, and then adds them according to the tags to my music library.

300 lines of python. Mostly because lame isn't able to deal with tags in a nice fashion.


"backup.sh" that tars up the contents of a directory and sends it to my gmail account.


Super remote reset button.
A rack of super special simulation hardware (backin the days when a room full of VME crates did less than your GPU) that a user on the other side of the world would crash in the early hours of the morning. It took an hour to get into the lab and through security.

But we weren't allowed to connect to the super special controller or modify the hardware. The solution was an old DEC workstation with an epson dot matrix printer, tape a plastic ruler to the paper feed knob, position the printer near the reset button.
Log in to the WS as a regular user (no root allowed, all external ports locked down), print a document with 24blank lines - which rotated the paper feed knob and the ruler pressed over the reset on the super special hardware.


A simply Python script that converts line endings from Unix to Windows that I stuck in my system32 directory. It's been lost to the ages for a few months, now, but basically it'd convert a list of known text-based file types to Windows line endings, and you could specify which files to convert, or all files, for a wildcard list.


The most useful? But there are so many...

  1. d.cmd contains: @dir /ad /on
  2. dd.cmd contains: @dir /a-d /on
  3. x.cmd contains: @exit
  4. s.cmd contains: @start .
  5. sx.cmd contains: @start . & exit
  6. ts.cmd contains the following, which allows me to properly connect to another machine's console session over RDP regardless of whether I'm on Vista SP1 or not.

    @echo off

    ver | find "6.0.6001"

    if ERRORLEVEL 0 if not errorlevel 1 (set TSCONS=admin) ELSE set TSCONS=console

    echo Issuing command: mstsc /%TSCONS% /v %1

    start mstsc /%TSCONS% /v %1

(Sorry for the weird formatting, apparently you can't have more than one code sample per answer?)

From a command prompt I'll navigate to where my VS solution file is, and then I'll want to open it, but I'm too lazy to type blah.sln and press enter. So I wrote sln.cmd:

@echo off
if not exist *.sln goto csproj
for %%f in (*.sln) do start /max %%f
goto end

:csproj
for %%f in (*.csproj) do start /max %%f
goto end

:end

So I just type sln and press enter and it opens the solution file, if any, in the current directory. I wrap things like pushd and popd in pd.cmd and pop.cmd.


I wrote a file extraction tool to be used in Linux, that can extract about 20 different file formats and uses the file content, not the file name.

This tool got quite popular, I have a regular stream of people who download it from my blog. Get it here:


I wrote a cron job to grab the ip address of my dads router and ftp it to a secure location so when he needed help I could remote desktop in and fix his comp.


I wrote a Python script that would go to all the web comics I read, and download any new comics. I just run that once a day, and there is no need to visit each site individually, just visit the /Comics/ Folder. ;)


I got a script which extracts id3 tags encodes the songs newly in a certain format, and then adds them according to the tags to my music library.

300 lines of python. Mostly because lame isn't able to deal with tags in a nice fashion.


A script to allow easy greping of ps results:

#!/usr/bin/php -f <?php $process = $argv[1]; echo shell_exec("ps -ef | grep $process | grep -v grep"); exit(0);


I often use a MS Word macro that takes a source-code file, formats it in two columns of monospaced type on a landscape page, numbers the lines, and adds company header and footer info such as filename, print date, page number, and confidentiality statement.

Printing both sides of the page uses about 1/4 the paper as the equivalent lpr command. (Does anyone use lpr anymore???)


I wrote a script for formatting C source files that automatically indents the code using an appropriate combination of tab and space characters, such that the file will appear correct regardless of what the tab setting on your editor is.

Source code is here.


Super remote reset button.
A rack of super special simulation hardware (backin the days when a room full of VME crates did less than your GPU) that a user on the other side of the world would crash in the early hours of the morning. It took an hour to get into the lab and through security.

But we weren't allowed to connect to the super special controller or modify the hardware. The solution was an old DEC workstation with an epson dot matrix printer, tape a plastic ruler to the paper feed knob, position the printer near the reset button.
Log in to the WS as a regular user (no root allowed, all external ports locked down), print a document with 24blank lines - which rotated the paper feed knob and the ruler pressed over the reset on the super special hardware.


A simply Python script that converts line endings from Unix to Windows that I stuck in my system32 directory. It's been lost to the ages for a few months, now, but basically it'd convert a list of known text-based file types to Windows line endings, and you could specify which files to convert, or all files, for a wildcard list.


I have a batch file which runs every morning, which launches a browser with the tabs loaded to all the sites I want to check each day (Woot, Dilbert, Doonesbury, UserFriendly; seasonally, NY Mets scores and electoral-vote.com, plus a few websites that need to be visited regularly to keep membership active)


Super remote reset button.
A rack of super special simulation hardware (backin the days when a room full of VME crates did less than your GPU) that a user on the other side of the world would crash in the early hours of the morning. It took an hour to get into the lab and through security.

But we weren't allowed to connect to the super special controller or modify the hardware. The solution was an old DEC workstation with an epson dot matrix printer, tape a plastic ruler to the paper feed knob, position the printer near the reset button.
Log in to the WS as a regular user (no root allowed, all external ports locked down), print a document with 24blank lines - which rotated the paper feed knob and the ruler pressed over the reset on the super special hardware.


A shell script to perform rotating backups using rsync. It also supports executing arbitrary child programs to support other pre-backup activities (downloading delicious bookmarks, for example).

http://gist.github.com/6806


I'm a private pilot. I wrote a couple of scripts that obtain weather information for local airports from aviationweather.gov. They were useful for a quick answer to the question "Is today a good day to fly?"


A python script that does a filewalk and prints my directory tree sorted by disk usage.


MySQL backup. I made a Windows batch script that would create incremental backups of MySQL databases, create a fresh dump every day and back them up every 10 minutes on a remote server. It saved my ass countless times, especially in the countless situations where a client would call, yelling their head off that a record just "disappeared" from the database. I went "no problem, let's see what happened" because I also wrote a binary search script that would look for the last moment when a record was present in the database. From there it would be pretty easy to understand who "stole" it and why.
You wouldn't imagine how useful these have been and I've been using them for almost 5 years. I wouldn't switch to anything else simply because they've been roughly tested and they're custom made, meaning they do exactly what I need and nothing more but I've tweaked them so much that it would be a snap to add extra functionalities.
So, my "masterpiece" is a MySQL incremental backup + remote backup + logs search system for Windows. I also wrote a version for Linux but I've lost it somewhere, probably because it was only about 15 lines + a cron job instead of Windows' about 1,200 lines + two scheduled tasks.


#!/usr/bin/perl
use strict;
use utf8;
use Encode;
use File::Find;
binmode STDOUT, ':utf8';
sub orderly {
    my ($x, $y) = @_{$a, $b};
    if (my $z = $x <=> $y) {return $z}
    $x = length $a;
    $y = length $b;
    my $z = $x < $y ? $x : $y;
    if (substr($a, 0, $z) eq substr($b, 0, $z)) {
        return $y <=> $x;
    }
    else {
        return $a cmp $b;
    }
}
my %conf = map +($_ => 0), split //, 'acsxL';
sub Stat {$conf{L} ? lstat : stat}
my @dirs = ();
while (defined ($_ = shift)) {
    if ($_ eq "--") {push @dirs, @ARGV; last}
    elsif (/^-(.*)$/s) {
        for (split //, $1) {
            if (!exists $conf{$_} or $conf{$_} = 1 and $conf{a} and $conf{s}) {
                print STDERR "$0 [-a] [-c] [-s] [-x] [-L] [--] ...\n";
                exit 1;
            }
        }
    }
    else {push @dirs, $_}
}
s/\/*$//s for @dirs;  # */ SO has crappy syntax highlighting
@dirs = qw(.) unless @dirs;
my %spec = (follow => $conf{L}, no_chdir => 1);
if ($conf{a}) {
    $spec{wanted} = sub {
        Stat;
        my $s = -f _ ? -s _ : 0;
        decode(utf8 => $File::Find::name) =~ /^\Q$dirs[0]\E\/?(.*)$/s;
        my @a = split /\//, $1;
        for (unshift @a, $dirs[0]; @a; pop @a) {
            $_{join "/", @a} += $s;
        }
    };
}
elsif ($conf{s}) {
    $spec{wanted} = sub {
        Stat;
        $_{$dirs[0]} += -f _ ? -s _ : 0;
    };
}
else {
    $spec{wanted} = sub {
        Stat;
        my $s = -f _ ? -s _ : 0;
        decode(utf8 => $File::Find::name) =~ /^\Q$dirs[0]\E\/?(.*)$/s;
        my @a = split /\//, $1;
        ! -d _ and pop @a;
        for (unshift @a, $dirs[0]; @a; pop @a) {
            $_{join "/", @a} += $s;
        }
    };
}
if ($conf{x}) {
    $spec{preprocess} = sub {
        my $dev = (Stat $File::Find::dir)[0];
        grep {$dev == (Stat "$File::Find::dir/$_")[0]} @_;
    };
}
while (@dirs) {
    find(\%spec, $dirs[0] eq "" ? "/" : $dirs[0]);
    $_{""} += $_{$dirs[0]} if $conf{c};
    shift @dirs;
}
$_{$_} < 1024 ** 1 ? printf "%s «%-6.6sB» %s\n", $_{$_}, sprintf("%6.6f", "$_{$_}" / 1024 ** 0), $_ :
$_{$_} < 1024 ** 2 ? printf "%s «%-6.6sK» %s\n", $_{$_}, sprintf("%6.6f", "$_{$_}" / 1024 ** 1), $_ :
$_{$_} < 1024 ** 3 ? printf "%s «%-6.6sM» %s\n", $_{$_}, sprintf("%6.6f", "$_{$_}" / 1024 ** 2), $_ :
$_{$_} < 1024 ** 4 ? printf "%s «%-6.6sG» %s\n", $_{$_}, sprintf("%6.6f", "$_{$_}" / 1024 ** 3), $_ :
$_{$_} < 1024 ** 5 ? printf "%s «%-6.6sT» %s\n", $_{$_}, sprintf("%6.6f", "$_{$_}" / 1024 ** 4), $_ :
$_{$_} < 1024 ** 6 ? printf "%s «%-6.6sP» %s\n", $_{$_}, sprintf("%6.6f", "$_{$_}" / 1024 ** 5), $_ :
$_{$_} < 1024 ** 7 ? printf "%s «%-6.6sE» %s\n", $_{$_}, sprintf("%6.6f", "$_{$_}" / 1024 ** 6), $_ :
$_{$_} < 1024 ** 8 ? printf "%s «%-6.6sZ» %s\n", $_{$_}, sprintf("%6.6f", "$_{$_}" / 1024 ** 7), $_ :
                     printf "%s «%-6.6sY» %s\n", $_{$_}, sprintf("%6.6f", "$_{$_}" / 1024 ** 8), $_
    for grep {$_{$_} > 0} sort orderly keys %_;

I save it in ~/bin/dush, it acts as a sort of du -h/du | sort -n hybrid: sorts and gives human-readable sizes all at once. Very useful for finding what's taking up disk space.

In a similar vein,

#!/usr/bin/perl
$t = 1;
%p = map {$_ => ($t *= 1024)} qw(K M G T P E Z Y);
$t = 4707319808;
if (@ARGV) {
    if (($_ = shift) =~ /^-*dvd/i) {$t = 4707319808}
    elsif (/^-*cd[^w]*$/i) {$t = 737280000}
    elsif (/^-*cd/i) {$t = 681984000}
    elsif (/^-*([\d.]+)([kmgtpezy])/i) {$t = $1 * ($p{"\U$2"} || 1)}
    elsif (/^-*([\d.]+)/) {$t = $1}
    else {unshift @ARGV, $_}
}
($q, $r, $s) = (0, ($ENV{COLUMNS} || 80) - 13, $t);
while (<>) {
    chomp, stat;
    unless (-e _) {
        print STDERR "$_ does not exist\n";
        next;
    }
    if (($s += -s _) > $t) {
        $s && $s < $t && printf "-%7s %s\n",
            sprintf("%2.3f%%", 100 * ($t - $s) / $t), $t - $s;
        printf "-----------%d%*s\n", ++$q, $r, "-" x $r;
        $s = -s _;
    }
    printf "%8s %s\n",
        sprintf("%3.3f%%", $s * 100 / $t),
        /.{4}(.{$r})$/s ? "...$1" : $_;
}
$s && $s < $t && printf "-%7s %s\n",
    sprintf("%2.3f%%", 100 * ($t - $s) / $t), $t - $s;

I save this as ~/bin/fit. When I'm archiving a bunch of files, I run ls | fit or ls | fit -cdrw to help determine if it'll fit on a DVD/CD/CDRW, and where to split them if they don't.


alias dir='ls -al' is my preferred favorite script.


A small application that left click (or double-click) every "X" ms for "Y" amount of time. No more need for that drinking bird to work at the nuclear power plant! ;)


A Greasemonkey script which removes obviously stupid[*] comments from gaming site Kotaku.com.

[*] As identified by common spelling mistakes, all-caps writing, excessive use of "LOL" and similar heuristics.


I use a DOS program that errors out if it's past a certain date. I just looked at the batch file that it was using to start up and changed it so it would first change the date to 2000, then run the program. On the program's exit, it changed the date back to what it was before it was changed.


MySQL backup. I made a Windows batch script that would create incremental backups of MySQL databases, create a fresh dump every day and back them up every 10 minutes on a remote server. It saved my ass countless times, especially in the countless situations where a client would call, yelling their head off that a record just "disappeared" from the database. I went "no problem, let's see what happened" because I also wrote a binary search script that would look for the last moment when a record was present in the database. From there it would be pretty easy to understand who "stole" it and why.
You wouldn't imagine how useful these have been and I've been using them for almost 5 years. I wouldn't switch to anything else simply because they've been roughly tested and they're custom made, meaning they do exactly what I need and nothing more but I've tweaked them so much that it would be a snap to add extra functionalities.
So, my "masterpiece" is a MySQL incremental backup + remote backup + logs search system for Windows. I also wrote a version for Linux but I've lost it somewhere, probably because it was only about 15 lines + a cron job instead of Windows' about 1,200 lines + two scheduled tasks.


Well back in 2005 I used Gentoo Linux and I used a lot a small program called genlop to show me the history of what I've emerged (installed) on my gentoo box. Well to simplify my work I've written not a small python script but a large one, but at that time I just started using python:

    #!/usr/bin/python
##############################################
# Gentoo emerge status              #   
# This script requires genlop,           #   
# you can install it using `emerge genlop`.  #
# Milot Shala <[email protected]>        #
##############################################

import sys
import os
import time

#colors
color={}
color["r"]="\x1b[31;01m"
color["g"]="\x1b[32;01m"
color["b"]="\x1b[34;01m"
color["0"]="\x1b[0m"


def r(txt):
   return color["r"]+txt+color["0"]
def g(txt):
   return color["g"]+txt+color["0"]
def b(txt):
   return color["b"]+txt+color["0"]

# View Options
def view_opt():   

   print
   print
   print g("full-info - View full information for emerged package")
   print g("cur - View current emerge")
   print g("hist - View history of emerged packages by day")
   print g("hist-all - View full list of history of emerged packages")
   print g("rsync - View rsync history")
   print g("time - View time for compiling a package")
   print g("time-unmerged - View time of unmerged packages")
   print
   command = raw_input(r("Press Enter to return to main "))
   if command == '':
      c()
      program()
   else:
      c()
      program()

# system command 'clear'
def c():
   os.system('clear')


# Base program
def program():
   c()
   print g("Gentoo emerge status script")
   print ("---------------------------")
   print

   print ("1]") + g(" Enter options")
   print ("2]") + g(" View options")
   print ("3]") + g(" Exit")
   print
   command = input("[]> ")


   if command == 1:   
      print
      print r("""First of all  you must view options to know what to use, you can enter option name ( if you know any ) or type `view-opt` to view options.""")
      print
      time.sleep(2)
      command = raw_input(b("Option name: "))
      if (command == 'view-opt' or command == 'VIEW-OPT'):
         view_opt()


      elif command == 'full-info':
         c()
         print g("Full information for a single package")
         print ("-------------------------------------")
         print
         print b("Enter package name")
         command=raw_input("> ")
         c()
         print g("Full information for package"), b(command)
         print ("-----------------------------------")
         print
         pack=['genlop -i '+command]
         pack_=" ".join(pack)
         os.system(pack_)
         print
         print r("Press Enter to return to main.")
         command=raw_input()
         if command == '':
            c()
            program()

         else:
            c()
            program()


      elif command == 'cur':
         if command == 'cur':
            c()
            print g("Current emerge session(s)")
            print ("-------------------------")
            print
            print b("Listing current emerge session(s)")
            print
            time.sleep(1)
            os.system('genlop -c')
            print
            print r("Press Enter to return to main.")
            command = raw_input()
            if (command == ''):
               c()
               program()

            else:
               c()
               program()


      elif command == 'hist':
         if command == 'hist':
            c()
            print g("History of merged packages")
            print ("---------------------------")
            print
            time.sleep(1)
            print b("Enter number of how many days ago you want to see the packages")
            command = raw_input("> ")
            c()
            print g("Packages merged "+b(command)+ g(" day(s) before"))
            print ("------------------------------------")
            pkg=['genlop --list --date '+command+' days ago']
            pkg_=" ".join(pkg)
            os.system(pkg_)
            print
            print r("Press Enter to return to main.")
            command = raw_input()
            if command == '':
               c()
               program()

            else:
               c()
               program()


      elif command == 'hist-all':
            c()
            print g("Full history of merged individual packages")
            print ("--------------------------------------")
            print
            print b("Do you want to view individual package?")
            print r("YES/NO?")
            command = raw_input("> ")
            print
            if (command == 'yes' or command == 'YES'):
               print g("Enter package name")
               command = raw_input("> ")
               print
               pkg=['genlop -l | grep '+command+ ' | less']
               pkg_=" ".join(pkg)
               os.system(pkg_)
               print
               print r("Press Enter to return to main")
               command = raw_input()
               if command == '':
                  c()
                  program()
               else:
                  c()
                  program()

            elif (command == 'no' or command == 'NO'):
               pkg=['genlop -l | less']
               pkg_=" ".join(pkg)
               os.system(pkg_)
               print
               print r("Press Enter to return to main")
               command = raw_input()
               if command == '':
                  c()
                  program()

               else:
                  c()
                  program()

            else:
               c()
               program()


      elif command == 'rsync':
         print g("RSYNC updates")
         print
         print
         print
         print b("You can view rsynced time by year!")
         print r("Do you want this script to do it for you? (yes/no)")
         command = raw_input("> ")
         if (command == 'yes' or command == 'YES'):
            print
            print g("Enter year i.e"), b("2005")
            print
            command = raw_input("> ")
            rsync=['genlop -r | grep '+command+' | less']
            rsync_=" ".join(rsync)
            os.system(rsync_)
            print
            print r("Press Enter to return to main.")
            c()
            program()
         elif (command == 'no' or command == 'NO'):
            os.system('genlop -r | less')
            print
            print r("Press Enter to return to main.")
            command = raw_input()
            if command == '':
               c()
               program()

            else:
               c()
               program()

      elif command == 'time':
         c()
         print g("Time of package compilation")
         print ("---------------------------")
         print
         print

         print b("Enter package name")
         pkg_name = raw_input("> ")
         pkg=['emerge '+pkg_name+' -p | genlop -p | less']
         pkg_=" ".join(pkg)
         os.system(pkg_)
         print
         print r("Press Enter to return to main")
         time.sleep(2)
         command = raw_input()
         if command == '':
            c()
            program()

         else:
            c()
            program()


      elif command == 'time-unmerged':
         c()
         print g("Show when package(s) is/when is unmerged")
         print ("----------------------------------------")
         print

         print b("Enter package name: ")
         name = raw_input("> ")
         pkg=['genlop -u '+name]
         pkg_=" ".join(pkg)
         os.system(pkg_)
         print
         print r("Press Enter to return to main")
         time.sleep(2)
         command = raw_input()
         if command == '':
            c()
            program()

         else:
            c()
            program()

      else:
         print
         print r("Wrong Selection!")
         time.sleep(2)
         c()
         program()


   elif command == 2:
      view_opt()
      command = raw_input(r("Press Enter to return to main "))
      if command == '':
         c()
         program()
      else:
         c()
         program()


   elif command == 3:
      print
      print b("Thank you for using this script")
      print
      time.sleep(1)
      sys.exit()

   else:
      print
      print r("Wrong Selection!")
      time.sleep(2)
      c()
      program()
      command = ("")


program()

Various Shortcuts to "net start" and "net stop" commands so I can start and stop services without having to go into the Services MMC


A Greasemonkey script to add a "press that button a lot" control box to an online game.


"backup.sh" that tars up the contents of a directory and sends it to my gmail account.


I had a version control script that would take a directory as an argument, and recursively copy all files to ../dirname/DATE/TIME/

Obviously it was a crappy way to do things, but it was handy before installing a real version control package.


A bash script called up so that if I'm in /a/very/deeply/nested/path/somewhere and I want to go "up" N directories, I can type up N:

#!/bin/bash
LIMIT=$1
P=$PWD
for ((i=1; i <= LIMIT; i++))
do
    P=$P/..
done
cd $P

For example:

/a/very/deeply/nested/path/somewhere> up 4
/a/very> 

NB by gmatt:

Working off the great work above, it can be extended to a back function by placing the following into your bashrc:

function up( )
{
LIMIT=$1
P=$PWD
for ((i=1; i <= LIMIT; i++))
do
    P=$P/..
done
cd $P
export MPWD=$P
}

function back( )
{
LIMIT=$1
P=$MPWD
for ((i=1; i <= LIMIT; i++))
do
    P=${P%/..}
done
cd $P
export MPWD=$P
}

A Rakefile in my downloads directory containing tasks that copy files from said directory to their respective media archives on external drives. Given my internet speed and storage capacity, it would take me hours out of every week to just copy across and re-name appropriately every piece of media that is downloaded (completely legally, I might add) by hellanzb.

Another very useful task in the same file logs into and scrapes IMDB for episode lists / discographies of all the media I have, and then checks NewzBin for reports that would fill any holes I have.

Combined, this means I have to do absolutely nothing, and in exchange, I wake up every morning with more media than I could possibly consume in that day sitting on my external hard drives.

Did I mention that this is all entirely above-board and legal? d-:

I'll probably merge this all into a sort of command-line media manager/player (farming things out to mplayer), and publish it on GitHub when I have the time.


A script to allow easy greping of ps results:

#!/usr/bin/php -f <?php $process = $argv[1]; echo shell_exec("ps -ef | grep $process | grep -v grep"); exit(0);


A little script that monitors some popular websites for ads that match my skills and email me an email.


I got a script which extracts id3 tags encodes the songs newly in a certain format, and then adds them according to the tags to my music library.

300 lines of python. Mostly because lame isn't able to deal with tags in a nice fashion.


I don't have the code any more, but possibly the most useful script I wrote was, believe it or not, in VBA. I had an annoying colleague who had such a short fuse that I referred to him as Cherry Bomb. He would often get mad when customers would call and then stand up and start ranting at me over the cubicle wall, killing my productivity and morale.

I always had Microsoft Excel open. When he would do this, I would alt-tab to Excel and there, on the toolbar, was a new icon with an image of a cherry bomb. I would discreetly click that ... and nothing would happen.

However, shortly after that I would get a phone call and would say something like "yeah, yeah, that sounds bad. I had better take a look." And then I would get up, apologize to the Cherry Bomb and walk away.

What happened is that we used NetWare and it had a primitive messaging system built in. When I clicked the button, a small VBA script would send out a NetWare message to my friends, telling them that the Cherry Bomb was at it again and would they please call me. He never figured it out :)


I wrote a file extraction tool to be used in Linux, that can extract about 20 different file formats and uses the file content, not the file name.

This tool got quite popular, I have a regular stream of people who download it from my blog. Get it here:


Called assignIisSite_ToAppPool.js

Really useful when you want to make sure that some resources are properly mapped.

:)

SetAppPool("W3SVC/1059997624/Root", "MyAppPool");



function SetAppPool(webId, appPoolName)
{
var providerObj=GetObject("winmgmts:/root/MicrosoftIISv2");
var vdirObj=providerObj.get("IIsWebVirtualDirSetting='" + webId + "'");
vdirObj.AppPoolId=appPoolName;
vdirObj.Put_();
}

I wrote a script for formatting C source files that automatically indents the code using an appropriate combination of tab and space characters, such that the file will appear correct regardless of what the tab setting on your editor is.

Source code is here.


A Greasemonkey script to add a "press that button a lot" control box to an online game.


I have a Python script that automatically runs when I plug my digital camera in.

It copies all of the pictures off the card on the camera, backs them up, and then uploads them to Flickr.


The upload-to-Flickr piece comes from uploadr.py (which I can't take credit for).

Here's the Python code for unloading the camera. It recurses through SRCDIR and names each image with the date & time before copying the images to DESTDIR.

#!/usr/bin/python

import os
import string
import time
import shutil

###################################################
__SRCDIR__ = "/mnt/camera"
__DESTDIR__ = "/home/pictures/recent"
###################################################
def cbwalk(arg, dirname, names):
    sdatetime = time.strftime("%y%m%d%H%M")
    for name in names:
        if string.lower(name[-3:]) in ("jpg", "mov"):
            srcfile = "%s/%s" % (dirname, name)
            destfile = "%s/%s_%s" % (__DESTDIR__, sdatetime, name)
                    print destfile
            shutil.copyfile( srcfile, destfile)
###################################################
if __name__ == "__main__":
    os.path.walk(__SRCDIR__, cbwalk, None)

I suppose I should include my own answer to this, for completion's sake.

I wrote a simple script that takes in the expenses of two people living together, and calculates which person owes the other money at the end of the month so that each person spent equally. I plan to extend it to store the categories of each expense and store them in a database. Sure, I could just use existing software...but where's the fun in that?

Not very complex, sure, but as far as non-work related scripts that I use a lot at home, this one is the current leader.


I wrote a script for formatting C source files that automatically indents the code using an appropriate combination of tab and space characters, such that the file will appear correct regardless of what the tab setting on your editor is.

Source code is here.


A simple all around shell function. Just when I get too lazy to think about what I am trying to do.

Really useful when I am just browsing around some random directory, and I have to switch from ls, to cd, to less constantly.

en() {
if [[ -z $1 ]] ; then
ls '.'

elif [[ -d $1 ]] ; then
cd $1

elif [[ -f $1 ]] ; then
less <$1
fi
}

A few years ago I wrote a winforms app with the help of a few win32 api's to completely lock myself out of my computer for an hour so that it would force me to go and exercise. Because I was lazy? No... because I had a personal fitness goal. Sometimes you just need a little kick to get started :)


An alert box, on a random timer, guaranteed to pop-up at least once an hour to remind me to do some pushups.

I used it when I was in the military.

I also wrote architecture rules (http://architecturerules.org) for me and anyone else.


A Quick and Dirty Python script that looked up the DNS for google.com every 5 seconds and beeped once if it succeeded and twice if it failed.

I wrote this during a time when I had to live with a highly flaky home network. It allowed me to instantly know the state of the network even while I was head first under the desk across the room with both hands full of network cable and a flashlight in my mouth.


Best real-life script?

Me: (Enters room) "Boss, I want a raise."

Boss: (Offers chair from behind desk) "A raise? Please, take my job!"

Then again, that may be the worst script!


I like to store my photos in a directory based on the date the picture was taken. Therefore I wrote a program that would scan a memory card for pictures, create any folders on my hard disk that it needed to based on the dates of the pictures, then copy them in.


VBS script to create a YYYY/YYYY-MM/YYYY-MM-DD file structure in my photos folder and move photos from my camera to the appropriate folder.


[email protected] and [email protected] => modify the hosts file, to go through LAN or WAN addresses.


A script that reads a config file in the current dir, logs into an FTP account, and uploads all files that have changed since the last time it was run. Really handy for clients who use shared hosting, and FTP is my only option for file access.

http://lucasoman.com/code/updater


I wrote a cron job to grab the ip address of my dads router and ftp it to a secure location so when he needed help I could remote desktop in and fix his comp.


I wrote a file extraction tool to be used in Linux, that can extract about 20 different file formats and uses the file content, not the file name.

This tool got quite popular, I have a regular stream of people who download it from my blog. Get it here:


I wrote a script that ended up being used every day in my team. When I used to work for Intel we had an app that talked to an access database to grab a dump of register information (I worked on validating chipsets). It would take this information (from a SQL query) and dump it into a CSV file, HTML file, and an Excel file. The whole process took almost 2 hours. No joke. No idea why it took so long. We would start it up an hour before lunch, go to lunch, and then come back.

I thought that there had to be a better way of doing this. I talked to the team that maintained the registry database and got the SQL code from them. I then wrote a perl script that grabbed the data and outputted it into CSV, HTML, and Excel formats. Runtime? Around 1-2 seconds. A great speed improvement.

I also wrote a few scripts while I was on deployment in Iraq in 2006 (I served in the National Guard for 9 years - got out in December). We used this old app called ULLS-G (Unit Level Logistics System - Ground) that was written in ADA and originally ran on DOS. They hacked it enough to where it would run on Windows XP in a command shell. This system didn't have a mouse interface. Everything was via keyboard and it had NO batch functionality. So let's say you wanted to print out licenses for all vehicle operators? Well... we had 150 soldiers in our unit so it took a LONG time. Let's say everyone got qualified on a new vehicle and you wanted to add it to everyone's operator qualifications? You had to do it one by one.

I was able to find an ODBC driver for the SAGE database (what ULLS-G used) and so I wrote perl scripts that were able to talk to the SAGE database. So things that took over an hour, now took only a few seconds. I also used my scripts and the driver for reporting. We had to report all information up to battalion every morning. Other units would write the information in by hand every morning. I whipped up an Excel macro that talked used the same driver and talked to the SAGE database and updated the Excel spreadsheet that way. It's the most complicated and only Excel macro I've ever written. It paid off because they awarded me the Army Commendation Medal. So yeah, I got a medal in the military for writing perl scripts :) How many can say that? ;)


Sometimes I forget what are the most recent files I just created in a directory, but a ls command will just show every file in the directory, I just want a few most recent files so I put this in my .cshrc

 ls -l -t | awk 'NR<15{print $0}'

(Actually it is in a file called lt and in the .cshrc it is set with: alias lt '~/lt')

So now lt will show me only a few files.


I had a version control script that would take a directory as an argument, and recursively copy all files to ../dirname/DATE/TIME/

Obviously it was a crappy way to do things, but it was handy before installing a real version control package.


alias snoot='find . ! -path "*/.svn*" -print0 | xargs -0 egrep '

Called assignIisSite_ToAppPool.js

Really useful when you want to make sure that some resources are properly mapped.

:)

SetAppPool("W3SVC/1059997624/Root", "MyAppPool");



function SetAppPool(webId, appPoolName)
{
var providerObj=GetObject("winmgmts:/root/MicrosoftIISv2");
var vdirObj=providerObj.get("IIsWebVirtualDirSetting='" + webId + "'");
vdirObj.AppPoolId=appPoolName;
vdirObj.Put_();
}

#! /bin/bash
# check to see if site is up
#   if it is, don't worry
#   if it's down, restart apache after get a process listing
#
# v.1 Warren M Myers - initial stab
#     31 Aug 06
#

ERRCOD='7'
WHEN=`date +%d%b%y`
REPT="~/psaux.$WHEN.txt"
STARS='********************'

curl -I http://www.shodor.org > /var/tmp/curlret.txt

if [ "$?" = "$ERRCOD" ]; then
    # return was unable to connect to host: save ps -aux; mail report
    ps -aux > $REPT
    echo $STARS
    echo 'curl return results'
    echo
    cat curlret.txt
    echo
    echo $STARS
    echo 'ps -aux results'
    cat $REPT
    echo
    echo $STARS
    echo 'restarting apache'
    /etc/init.d/apache2 restart
    echo 'apache restarted'
    echo
    echo "ps -aux results saved in $REPT"
fi

rm -f /var/tmp/curlret.txt

A python script that does a filewalk and prints my directory tree sorted by disk usage.


A threaded HTML scraper to download all available subtitles for series/movies from a site which is a pain to use (you have to click like 4 times after a search to get to the download page, just to display more ads). Now I just put the search criteria and press download.


I like to store my photos in a directory based on the date the picture was taken. Therefore I wrote a program that would scan a memory card for pictures, create any folders on my hard disk that it needed to based on the dates of the pictures, then copy them in.


A perl script that scrapes my local Craigslist, by selected categories, in to a SQL DB which I can then query against.

V2 of this updates the DB with a timer and alerts me if I have a match on any of the queries, basically providing me with a background agent for CL.


A script that runs hourly to retrain my spam filters based two IMAP folder where span and ham are put.

#!/bin/sh
FNDIR="train-as-spam"
FPDIR="train-as-ham"

for dir in /home/*/.maildir
do
    cd "${dir}"
    USER=`stat -c %U .`

    SRCDIR="${dir}/.${FNDIR}"
    if [ ! -d ${SRCDIR} ]; then
        echo no "${SRCDIR}" directory
    else
        cd "${SRCDIR}/cur"
        ls -tr | while read file
        do
            if grep -q "^X-DSPAM" "${file}"; then
                SOURCE="error"
            else
                SOURCE="corpus"
            fi

            dspam --user "${USER}" --class=spam --source="${SOURCE}" --deliver=innocent,spam --stdout < "${file}" > "../tmp/${file}"
            mv "../tmp/${file}" "${dir}/new/${file%%:*}" && rm "${file}"
        done
    fi

    SRCDIR="${dir}/.${FPDIR}"
    if [ ! -d ${SRCDIR} ]; then
        echo no "${SRCDIR}" directory
    else
        cd "${SRCDIR}/cur"
        ls -tr | while read file
        do
            if grep -q "^X-DSPAM" "${file}"; then
                SOURCE="error"
            else
                SOURCE="corpus"
            fi

            dspam --user "${USER}" --class=innocent --source="${SOURCE}" --deliver=innocent,spam --stdout < "${file}" > "../tmp/${file}"
            mv "../tmp/${file}" "${dir}/new/${file%%:*}" && rm "${file}"
        done
    fi

done

I use a DOS program that errors out if it's past a certain date. I just looked at the batch file that it was using to start up and changed it so it would first change the date to 2000, then run the program. On the program's exit, it changed the date back to what it was before it was changed.


This keeps 20 days of diff backups without using a bunch of space. Uses links to copy and rsync copies as necessary


#!/bin/bash                                                                                                                                                                                                                            


BACKUPDIR=/media/proxy/store/backups/                                                                                                                                                                                                  

[ ! -d $BACKUPDIR ] && { echo "BACKUP DIRECTORY NOT AVAILABLE!"; exit; }                                                                                                                                                               

dobackup() {                                                                                                                                                                                                                           
        SDIR=$2                                                                                                                                                                                                                        
        PARENTDIR=$1                                                                                                                                                                                                                   
        echo "BACKING UP $PARENTDIR/$SDIR to $BACKUPDIR"                                                                                                                                                                               
        bnum=20
        count=$bnum
        [ -d ${BACKUPDIR}${SDIR}.$bnum ] && {  mv ${BACKUPDIR}${SDIR}.$bnum ${BACKUPDIR}${SDIR}.tmp; }
        until [ $count -eq 1 ]; do
                let lastdir=$count-1
                [ -d ${BACKUPDIR}${SDIR}.$lastdir ] && { mv ${BACKUPDIR}${SDIR}.$lastdir ${BACKUPDIR}${SDIR}.$count; }
                let count-=1
        done
        cp -al  ${BACKUPDIR}${SDIR}.0  ${BACKUPDIR}${SDIR}.1
        rsync -a --delete --bwlimit=2000  $PARENTDIR/$SDIR ${BACKUPDIR}${SDIR}.0
}

for backup in $(cat /sbin/backup.directories); do
        PDIR=$(echo $backup | awk -F '::' {'print$1'})
        DIR=$(echo $backup | awk -F '::' {'print$2'})
        dobackup $PDIR $DIR
done

exit;


cat /sbin/backup.directories
/media/warehouse::Archive
/media/warehouse::concept


A bash script called up so that if I'm in /a/very/deeply/nested/path/somewhere and I want to go "up" N directories, I can type up N:

#!/bin/bash
LIMIT=$1
P=$PWD
for ((i=1; i <= LIMIT; i++))
do
    P=$P/..
done
cd $P

For example:

/a/very/deeply/nested/path/somewhere> up 4
/a/very> 

NB by gmatt:

Working off the great work above, it can be extended to a back function by placing the following into your bashrc:

function up( )
{
LIMIT=$1
P=$PWD
for ((i=1; i <= LIMIT; i++))
do
    P=$P/..
done
cd $P
export MPWD=$P
}

function back( )
{
LIMIT=$1
P=$MPWD
for ((i=1; i <= LIMIT; i++))
do
    P=${P%/..}
done
cd $P
export MPWD=$P
}

copy con c.bat
c:
cd\
cls
^Z

A script that runs hourly to retrain my spam filters based two IMAP folder where span and ham are put.

#!/bin/sh
FNDIR="train-as-spam"
FPDIR="train-as-ham"

for dir in /home/*/.maildir
do
    cd "${dir}"
    USER=`stat -c %U .`

    SRCDIR="${dir}/.${FNDIR}"
    if [ ! -d ${SRCDIR} ]; then
        echo no "${SRCDIR}" directory
    else
        cd "${SRCDIR}/cur"
        ls -tr | while read file
        do
            if grep -q "^X-DSPAM" "${file}"; then
                SOURCE="error"
            else
                SOURCE="corpus"
            fi

            dspam --user "${USER}" --class=spam --source="${SOURCE}" --deliver=innocent,spam --stdout < "${file}" > "../tmp/${file}"
            mv "../tmp/${file}" "${dir}/new/${file%%:*}" && rm "${file}"
        done
    fi

    SRCDIR="${dir}/.${FPDIR}"
    if [ ! -d ${SRCDIR} ]; then
        echo no "${SRCDIR}" directory
    else
        cd "${SRCDIR}/cur"
        ls -tr | while read file
        do
            if grep -q "^X-DSPAM" "${file}"; then
                SOURCE="error"
            else
                SOURCE="corpus"
            fi

            dspam --user "${USER}" --class=innocent --source="${SOURCE}" --deliver=innocent,spam --stdout < "${file}" > "../tmp/${file}"
            mv "../tmp/${file}" "${dir}/new/${file%%:*}" && rm "${file}"
        done
    fi

done

MySQL backup. I made a Windows batch script that would create incremental backups of MySQL databases, create a fresh dump every day and back them up every 10 minutes on a remote server. It saved my ass countless times, especially in the countless situations where a client would call, yelling their head off that a record just "disappeared" from the database. I went "no problem, let's see what happened" because I also wrote a binary search script that would look for the last moment when a record was present in the database. From there it would be pretty easy to understand who "stole" it and why.
You wouldn't imagine how useful these have been and I've been using them for almost 5 years. I wouldn't switch to anything else simply because they've been roughly tested and they're custom made, meaning they do exactly what I need and nothing more but I've tweaked them so much that it would be a snap to add extra functionalities.
So, my "masterpiece" is a MySQL incremental backup + remote backup + logs search system for Windows. I also wrote a version for Linux but I've lost it somewhere, probably because it was only about 15 lines + a cron job instead of Windows' about 1,200 lines + two scheduled tasks.


I use a DOS program that errors out if it's past a certain date. I just looked at the batch file that it was using to start up and changed it so it would first change the date to 2000, then run the program. On the program's exit, it changed the date back to what it was before it was changed.


A small task-bar program that extracted every error-code constant out of a third-party JavaDoc and let me lookup the constant-name for a given error code. Plus, add in any conversions from HEX to decimal, etc.

This comes up a lot when working in the debugger--you get back the error code, but then tracking back the code to text is a huge pain. It's even more common when working with software that wraps native methods, OS calls, or COM... often times, the constants are copied straight out of an error header file with no additional context, repeated values, and no enumerations.


I've written a small shell script, tapt, for Debian based system. esp. Ubuntu. What it basically does is to post all your "apt-get" activities to your twitter account. It helps me to keep the track of what and when I've installed/remove programs in my Ubuntu system. I created a new Twitter account just for this and kept it private. Really useful. More information here: http://www.quicktweaks.com/tapt/


A threaded HTML scraper to download all available subtitles for series/movies from a site which is a pain to use (you have to click like 4 times after a search to get to the download page, just to display more ads). Now I just put the search criteria and press download.


I don't have the code any more, but possibly the most useful script I wrote was, believe it or not, in VBA. I had an annoying colleague who had such a short fuse that I referred to him as Cherry Bomb. He would often get mad when customers would call and then stand up and start ranting at me over the cubicle wall, killing my productivity and morale.

I always had Microsoft Excel open. When he would do this, I would alt-tab to Excel and there, on the toolbar, was a new icon with an image of a cherry bomb. I would discreetly click that ... and nothing would happen.

However, shortly after that I would get a phone call and would say something like "yeah, yeah, that sounds bad. I had better take a look." And then I would get up, apologize to the Cherry Bomb and walk away.

What happened is that we used NetWare and it had a primitive messaging system built in. When I clicked the button, a small VBA script would send out a NetWare message to my friends, telling them that the Cherry Bomb was at it again and would they please call me. He never figured it out :)


At some point in the distant past I decided to put all the files for my web host's public_html directory into a subversion repository. Then I wrote a script which:

  1. Creates, mounts, and formats a RAM disk.
  2. Exports the trunk of the repository into the RAM disk.
  3. Calls rsync to upload any changed files from the RAM disk to my hosting provider. I use a public/private key pair to save me from typing my login information each time.
  4. Unmounts the RAM disk.

Thus, pushing updates from the repository to the server is literally a "one touch" operation.

What is most satisfying about the script is that, initially, it was more of a shell scripting exercise than a Grand Project. However, it has probably saved me countless hours of work and makes the prospect of updating a website almost stress-free, maybe more than any other piece of software on my computer.


VBS script to create a YYYY/YYYY-MM/YYYY-MM-DD file structure in my photos folder and move photos from my camera to the appropriate folder.


My .cmd backup script. It runs on my server every night, and names the backup files according the week day. A full week of backups has saved me (and my family) many times:

:: Backup args:
::   /V Verify? (yes/no)
::   /R Restrict access to owner? (yes/no)
::   /RS Removable storage? (yes/no)
::   /HC Hardware compression (on/off)
::   /M Backup type (normal/copy/differential/incremental/daily)
::   /L Log file type (f/s/n)
::   /D "Description"
::   /J "Job-name"
::   /F "File-name"

SETLOCAL

:: ensure that network drives are mounted
CALL C:\bat\configs\MapShares-home.cmd
echo on

set today=%DATE:~0,3%
if %today%==Mon set yesterday=0Sunday
if %today%==Tue set yesterday=1Monday
if %today%==Wed set yesterday=2Tuesday
if %today%==Thu set yesterday=3Wednesday
if %today%==Fri set yesterday=4Thursday
if %today%==Sat set yesterday=5Friday
if %today%==Sun set yesterday=6Saturday

set configsDir=%~dp0
set storePath=C:\mybackups

:: (eg: Monday C files)
set title=%yesterday% backup set


echo %DATE% %TIME% %title% > "%storePath%\%yesterday%_backup.log"

CALL BackupConfigs.bat

:: Create new BKF file
call C:\WINDOWS\system32\ntbackup.exe backup ^
    "@%configsDir%\daily.bks" ^
    /V:yes /R:no /RS:no /HC:off /M normal /L:s ^
    /D "%title%" ^
    /J "%title%.job" ^
    /F "%storePath%\%yesterday%.bkf" ^
    >> "%storePath%\%yesterday%_backup.log"

echo %DATE% %TIME% Completed >> "%storePath%\%yesterday%_backup.log"

copy "%storePath%\%yesterday%.bkf" "V:\Backups\NEPTUNE"

CALL C:\bat\clean-temps.bat

defrag -v C: > "%storePath%\%yesterday%_defrag.log"

:: display backup directories
start /D"C:\bat\Backups\" checkbkf.bat

ENDLOCAL

::pause


VBS script to create a YYYY/YYYY-MM/YYYY-MM-DD file structure in my photos folder and move photos from my camera to the appropriate folder.


On Windows XP, I have set an AT job to run this command daily in C:\

dir /s /b * > dirlist.txt

This lists the full path of all files on the C drive. Then whenever I need to find a file, I can use findstr. This beats using Windows Explorer Search since it allows regular expression matching on the entire path. For example:

findstr ".jpg" dirlist.txt
findstr /i /r "windows.*system32.*png$" dirlist.txt

This is a very fast solution to set up, and great if you find yourself with a fresh Windows install and no internet connection.

If you need to search within certain file types for some pattern, first list all of the files you need to check, then search within them. For example, to find a Java or Python program that flips an image you could do this:

findstr "\.java \.py" dirlist.txt > narrowlist.txt
findstr /i /r /f:narrowlist.txt "flip.*image"

A script that reads a config file in the current dir, logs into an FTP account, and uploads all files that have changed since the last time it was run. Really handy for clients who use shared hosting, and FTP is my only option for file access.

http://lucasoman.com/code/updater


A shell script to perform rotating backups using rsync. It also supports executing arbitrary child programs to support other pre-backup activities (downloading delicious bookmarks, for example).

http://gist.github.com/6806


A Greasemonkey script which removes obviously stupid[*] comments from gaming site Kotaku.com.

[*] As identified by common spelling mistakes, all-caps writing, excessive use of "LOL" and similar heuristics.


A shell script to perform rotating backups using rsync. It also supports executing arbitrary child programs to support other pre-backup activities (downloading delicious bookmarks, for example).

http://gist.github.com/6806


My .cmd backup script. It runs on my server every night, and names the backup files according the week day. A full week of backups has saved me (and my family) many times:

:: Backup args:
::   /V Verify? (yes/no)
::   /R Restrict access to owner? (yes/no)
::   /RS Removable storage? (yes/no)
::   /HC Hardware compression (on/off)
::   /M Backup type (normal/copy/differential/incremental/daily)
::   /L Log file type (f/s/n)
::   /D "Description"
::   /J "Job-name"
::   /F "File-name"

SETLOCAL

:: ensure that network drives are mounted
CALL C:\bat\configs\MapShares-home.cmd
echo on

set today=%DATE:~0,3%
if %today%==Mon set yesterday=0Sunday
if %today%==Tue set yesterday=1Monday
if %today%==Wed set yesterday=2Tuesday
if %today%==Thu set yesterday=3Wednesday
if %today%==Fri set yesterday=4Thursday
if %today%==Sat set yesterday=5Friday
if %today%==Sun set yesterday=6Saturday

set configsDir=%~dp0
set storePath=C:\mybackups

:: (eg: Monday C files)
set title=%yesterday% backup set


echo %DATE% %TIME% %title% > "%storePath%\%yesterday%_backup.log"

CALL BackupConfigs.bat

:: Create new BKF file
call C:\WINDOWS\system32\ntbackup.exe backup ^
    "@%configsDir%\daily.bks" ^
    /V:yes /R:no /RS:no /HC:off /M normal /L:s ^
    /D "%title%" ^
    /J "%title%.job" ^
    /F "%storePath%\%yesterday%.bkf" ^
    >> "%storePath%\%yesterday%_backup.log"

echo %DATE% %TIME% Completed >> "%storePath%\%yesterday%_backup.log"

copy "%storePath%\%yesterday%.bkf" "V:\Backups\NEPTUNE"

CALL C:\bat\clean-temps.bat

defrag -v C: > "%storePath%\%yesterday%_defrag.log"

:: display backup directories
start /D"C:\bat\Backups\" checkbkf.bat

ENDLOCAL

::pause


Anime CRC32 checksum:

#!/usr/bin/python                                                                                                                                                                                  

import sys, re, zlib

c_null="^[[00;00m"
c_red="^[[31;01m"
c_green="^[[32;01m"

def crc_checksum(filename):
    filedata = open(filename, "rb").read()
    sum = zlib.crc32(filedata)
    if sum < 0:
        sum &= 16**8-1
    return "%.8X" %(sum)

for file in sys.argv[1:]:
    sum = crc_checksum(file)
    try:
        dest_sum = re.split('[\[\]]', file)[-2]
        if sum == dest_sum:
            c_in = c_green
        else:
            c_in = c_red
        sfile = file.split(dest_sum)
        print "%s%s%s   %s%s%s%s%s" % (c_in, sum, c_null, sfile[0], c_in, dest_sum, c_null, sfile[1])
    except IndexError:
        print "%s   %s" %(sum, file)

Various Shortcuts to "net start" and "net stop" commands so I can start and stop services without having to go into the Services MMC


I'm a private pilot. I wrote a couple of scripts that obtain weather information for local airports from aviationweather.gov. They were useful for a quick answer to the question "Is today a good day to fly?"


A small task-bar program that extracted every error-code constant out of a third-party JavaDoc and let me lookup the constant-name for a given error code. Plus, add in any conversions from HEX to decimal, etc.

This comes up a lot when working in the debugger--you get back the error code, but then tracking back the code to text is a huge pain. It's even more common when working with software that wraps native methods, OS calls, or COM... often times, the constants are copied straight out of an error header file with no additional context, repeated values, and no enumerations.


I wrote a cron job to grab the ip address of my dads router and ftp it to a secure location so when he needed help I could remote desktop in and fix his comp.


I suppose this depends on how you define useful, but my favorite little script is a variant on the *nix fortune program. See below, and you'll get the idea of what it does:

telemachus ~ $ haiku 

   January--
in other provinces,
   plums blooming.
    Issa

It doesn't really get anything done, but a nice haiku goes a long way. (I like how the colorizer decided to interpret the poem.) (Edit: If I really have to be useful, I'd say a script that allows a user to enter a US zipcode and get current weather and 0-3 days of forecast from Google.)


Mass file renaming via drag&drop.

Ages ago I've made a small VBScript that accepts a RegEx and replaces file names accordingly. You would simply drop a bunch of files or folders on it. I found that to be very useful throughout the years.

gist.github.com/15824 (Beware, the comments are in German)


A simply Python script that converts line endings from Unix to Windows that I stuck in my system32 directory. It's been lost to the ages for a few months, now, but basically it'd convert a list of known text-based file types to Windows line endings, and you could specify which files to convert, or all files, for a wildcard list.



For those of us who don't remember where we are on unix, or which SID we are using.
Pop this in your .profile.

<br>function CD
<br>{
<br>   unalias cd
<br>   command cd "$@" && PS1="\${ORACLE_SID}:$(hostname):$PWD> "
<br>   alias cd=CD
<br>}
<br>
alias cd=CD

alias snoot='find . ! -path "*/.svn*" -print0 | xargs -0 egrep '

As a scheduled task, to copy any modified/new files from entire drive d: to backup drive g:, and to log the files copied. It helps me keep track of what I did when, as well.

justdate is a small program to prints the date and time to the screen

g:

cd \drive_d

d:

cd \

type g:\backup_d.log >> g:\logs\backup_d.log

echo ========================================== > g:\backup_d.log

d:\mu\bmutil\justdate >> g:\backup_d.log

xcopy /s /d /y /c . g:\drive_d >> g:\backup_d.log


On Windows XP, I have set an AT job to run this command daily in C:\

dir /s /b * > dirlist.txt

This lists the full path of all files on the C drive. Then whenever I need to find a file, I can use findstr. This beats using Windows Explorer Search since it allows regular expression matching on the entire path. For example:

findstr ".jpg" dirlist.txt
findstr /i /r "windows.*system32.*png$" dirlist.txt

This is a very fast solution to set up, and great if you find yourself with a fresh Windows install and no internet connection.

If you need to search within certain file types for some pattern, first list all of the files you need to check, then search within them. For example, to find a Java or Python program that flips an image you could do this:

findstr "\.java \.py" dirlist.txt > narrowlist.txt
findstr /i /r /f:narrowlist.txt "flip.*image"

A Greasemonkey script to add a "press that button a lot" control box to an online game.


Called assignIisSite_ToAppPool.js

Really useful when you want to make sure that some resources are properly mapped.

:)

SetAppPool("W3SVC/1059997624/Root", "MyAppPool");



function SetAppPool(webId, appPoolName)
{
var providerObj=GetObject("winmgmts:/root/MicrosoftIISv2");
var vdirObj=providerObj.get("IIsWebVirtualDirSetting='" + webId + "'");
vdirObj.AppPoolId=appPoolName;
vdirObj.Put_();
}

[email protected] and [email protected] => modify the hosts file, to go through LAN or WAN addresses.


Mass file renaming via drag&drop.

Ages ago I've made a small VBScript that accepts a RegEx and replaces file names accordingly. You would simply drop a bunch of files or folders on it. I found that to be very useful throughout the years.

gist.github.com/15824 (Beware, the comments are in German)


Not every day, but I did use XSLT script to create my wedding invitations (a Pages file for the inserts to the invite cards, and an HTML file for the address labels).


[email protected] and [email protected] => modify the hosts file, to go through LAN or WAN addresses.


I wrote some lines of code to automatically tweak all things powertop suggests when I unplug my laptop and undo that if I plug the laptop back in. Maximum power, maximum efficiency, maximum convenience.


alias dir='ls -al' is my preferred favorite script.


I wrote a simple Ruby script to help my wife and I when we were considering names for our first child. It generated all name combinations and checked the initials against a blacklist of initials I wanted to avoid, excluding any that didn't match my criteria. It felt like an appropriate thing for a nerdy dad to do and actually proved to be quite worthwhile and useful.

Other than that I've written a couple of Python scripts that serve as IRC bots which I use every day. One saves URLs, via regular expression matching, to delicious. Another serves as a simple IRC Twitter interface, allowing me to check my feed and post updates.


I used to work at a technology summer camp, and we had to compose these write-ups for each of the kids in the group at the end of the week, which they would then receive and take home as a keepsake. Usually, these consisted of a bunch of generic sentences, and one to two personalized sentences. I wrote a python script which constructed one of these write-ups out of a bank of canned sentences, and allowed the user to add a couple of personalized sentences in the middle. This saved a huge amount of time for me and other counselors I let in on the secret. Even though so much of it was automated, our write-ups still looked better than many of the 'honest' ones, because we could put more time into the personalized parts.


I use procmail to sort my incoming email to different folders. Because I have trouble remembering the procmailrc syntax, I use m4 as a preprocessor. Here's how my procmailrc begins (this isn't the script yet):

divert(-1)
changequote(<<, >>)
define(mailinglistrule, 
<<:0:
* $2
Lists/$1
>>)
define(listdt, <<mailinglistrule($1,^Delivered-To:.*$2)>>)
define(listid, <<mailinglistrule($1,^List-Id:.*<$2>)>>)
divert# Generated from .procmailrc.m4 -- DO NOT EDIT

This defines two macros for mailing lists, so e.g. listdt(foo, [email protected]) expands to

:0:
* ^Delivered-To:.*[email protected]
Lists/foo

meaning that emails with a Delivered-To header containing [email protected] should be put in the Lists/foo folder. It also arranges the processed file to begin with a comment that warns me not to edit that file directly.

Now, frankly, m4 scares me: what if I accidentally redefine a macro and procmail starts discarding all my email, or something like that? That's why I have a script, which I call update-procmailrc, that shows me in diff format how my procmailrc is going to change. If the change is just a few lines and looks roughly like what I intended, I can happily approve it, but if there are huge changes to the file, I know to look at my edits more carefully.

#! /bin/sh

PROCMAILRC=.procmailrc
TMPNAM=.procmailrc.tmp.$$
cd $HOME
umask 077
trap "rm -f $TMPNAM" 0

m4 < .procmailrc.m4 > $TMPNAM
diff -u $PROCMAILRC $TMPNAM

echo -n 'Is this acceptable? (y/N) '
read accept

if [ -z "$accept" ]; then
    accept=n
fi

if [ $accept = 'y' -o $accept = 'Y' ]; then
    mv -f $TMPNAM $PROCMAILRC && \
    chmod 400 $PROCMAILRC && \
    echo "Created new $PROCMAILRC"
    if [ "$?" -ne 0 ]; then
        echo "*** FAILED creating $PROCMAILRC"
    fi
else
    echo "Didn't update $PROCMAILRC"
fi

The script hasn't yet prevented any email disasters, but it has made me less anxious about changing my procmailrc.


I wrote some lines of code to automatically tweak all things powertop suggests when I unplug my laptop and undo that if I plug the laptop back in. Maximum power, maximum efficiency, maximum convenience.


Sometimes I forget what are the most recent files I just created in a directory, but a ls command will just show every file in the directory, I just want a few most recent files so I put this in my .cshrc

 ls -l -t | awk 'NR<15{print $0}'

(Actually it is in a file called lt and in the .cshrc it is set with: alias lt '~/lt')

So now lt will show me only a few files.


I got a script which extracts id3 tags encodes the songs newly in a certain format, and then adds them according to the tags to my music library.

300 lines of python. Mostly because lame isn't able to deal with tags in a nice fashion.


Not every day, but I did use XSLT script to create my wedding invitations (a Pages file for the inserts to the invite cards, and an HTML file for the address labels).


A python script that does a filewalk and prints my directory tree sorted by disk usage.


The most useful? But there are so many...

  1. d.cmd contains: @dir /ad /on
  2. dd.cmd contains: @dir /a-d /on
  3. x.cmd contains: @exit
  4. s.cmd contains: @start .
  5. sx.cmd contains: @start . & exit
  6. ts.cmd contains the following, which allows me to properly connect to another machine's console session over RDP regardless of whether I'm on Vista SP1 or not.

    @echo off

    ver | find "6.0.6001"

    if ERRORLEVEL 0 if not errorlevel 1 (set TSCONS=admin) ELSE set TSCONS=console

    echo Issuing command: mstsc /%TSCONS% /v %1

    start mstsc /%TSCONS% /v %1

(Sorry for the weird formatting, apparently you can't have more than one code sample per answer?)

From a command prompt I'll navigate to where my VS solution file is, and then I'll want to open it, but I'm too lazy to type blah.sln and press enter. So I wrote sln.cmd:

@echo off
if not exist *.sln goto csproj
for %%f in (*.sln) do start /max %%f
goto end

:csproj
for %%f in (*.csproj) do start /max %%f
goto end

:end

So I just type sln and press enter and it opens the solution file, if any, in the current directory. I wrap things like pushd and popd in pd.cmd and pop.cmd.


Mass file renaming via drag&drop.

Ages ago I've made a small VBScript that accepts a RegEx and replaces file names accordingly. You would simply drop a bunch of files or folders on it. I found that to be very useful throughout the years.

gist.github.com/15824 (Beware, the comments are in German)


I use this as an autoloaded function. I can just type "mycd" and a list of directories appears which I frequently cd to. If I happen to know then number I can just say something like "mycd 2". To add a directory to the list you just type "mycd /tmp/foo/somedirectory".

function mycd {

MYCD=/tmp/mycd.txt
touch ${MYCD}

typeset -i x
typeset -i ITEM_NO
typeset -i i
x=0

if [[ -n "${1}" ]]; then
   if [[ -d "${1}" ]]; then
      print "${1}" >> ${MYCD}
      sort -u ${MYCD} > ${MYCD}.tmp
      mv ${MYCD}.tmp ${MYCD}
      FOLDER=${1}
   else
      i=${1}
      FOLDER=$(sed -n "${i}p" ${MYCD})
   fi
fi

if [[ -z "${1}" ]]; then
   print ""
   cat ${MYCD} | while read f; do
      x=$(expr ${x} + 1)
      print "${x}. ${f}"
   done
   print "\nSelect #"
   read ITEM_NO
   FOLDER=$(sed -n "${ITEM_NO}p" ${MYCD})
fi

if [[ -d "${FOLDER}" ]]; then
   cd ${FOLDER}
fi

}

#! /bin/bash
# check to see if site is up
#   if it is, don't worry
#   if it's down, restart apache after get a process listing
#
# v.1 Warren M Myers - initial stab
#     31 Aug 06
#

ERRCOD='7'
WHEN=`date +%d%b%y`
REPT="~/psaux.$WHEN.txt"
STARS='********************'

curl -I http://www.shodor.org > /var/tmp/curlret.txt

if [ "$?" = "$ERRCOD" ]; then
    # return was unable to connect to host: save ps -aux; mail report
    ps -aux > $REPT
    echo $STARS
    echo 'curl return results'
    echo
    cat curlret.txt
    echo
    echo $STARS
    echo 'ps -aux results'
    cat $REPT
    echo
    echo $STARS
    echo 'restarting apache'
    /etc/init.d/apache2 restart
    echo 'apache restarted'
    echo
    echo "ps -aux results saved in $REPT"
fi

rm -f /var/tmp/curlret.txt


For those of us who don't remember where we are on unix, or which SID we are using.
Pop this in your .profile.

<br>function CD
<br>{
<br>   unalias cd
<br>   command cd "$@" && PS1="\${ORACLE_SID}:$(hostname):$PWD> "
<br>   alias cd=CD
<br>}
<br>
alias cd=CD

A bash script called up so that if I'm in /a/very/deeply/nested/path/somewhere and I want to go "up" N directories, I can type up N:

#!/bin/bash
LIMIT=$1
P=$PWD
for ((i=1; i <= LIMIT; i++))
do
    P=$P/..
done
cd $P

For example:

/a/very/deeply/nested/path/somewhere> up 4
/a/very> 

NB by gmatt:

Working off the great work above, it can be extended to a back function by placing the following into your bashrc:

function up( )
{
LIMIT=$1
P=$PWD
for ((i=1; i <= LIMIT; i++))
do
    P=$P/..
done
cd $P
export MPWD=$P
}

function back( )
{
LIMIT=$1
P=$MPWD
for ((i=1; i <= LIMIT; i++))
do
    P=${P%/..}
done
cd $P
export MPWD=$P
}

I had a version control script that would take a directory as an argument, and recursively copy all files to ../dirname/DATE/TIME/

Obviously it was a crappy way to do things, but it was handy before installing a real version control package.


I wrote a Python script that would go to all the web comics I read, and download any new comics. I just run that once a day, and there is no need to visit each site individually, just visit the /Comics/ Folder. ;)


A Quick and Dirty Python script that looked up the DNS for google.com every 5 seconds and beeped once if it succeeded and twice if it failed.

I wrote this during a time when I had to live with a highly flaky home network. It allowed me to instantly know the state of the network even while I was head first under the desk across the room with both hands full of network cable and a flashlight in my mouth.


At my previous place of work office hours were ridiculous. It was a software company and my boss was sucked. He would give us work right around 5:30PM (right when it was time to go home) and made us finish the job until past 11:00PM (way past our ideal productive hours). Or he would find annoying problems in code that was still in progress.

So I made a batch file and a script that would turn my computer OFF at a random time between 7:00PM and 8:00PM. It had a 1 minute timer just in case I would stay after hours and needed to abort the shutdown process.

But I would leave my desk before 5:00PM so he couldn't find me to keep me if he wanted to dump crap around checkout time. If he came to my desk and see my computer on, he would think I was still around the pantry area or at the nearby minimart to grab some chips or something. But if it was off around that time, he would call my cell phone and tell me to get back to the office.

I also scheduled the BIOS on my machine to turn my machine ON around 8:00AM or 9:00AM in case I felt lazy and wanted to stroll in around 10:00AM or 11:00AM. If I got caught walking to my desk he would ask "where have you been all morning?" And I would say "I was at a meeting with the marketing team." or "I was out getting breakfast."

dumb dog


I'm a private pilot. I wrote a couple of scripts that obtain weather information for local airports from aviationweather.gov. They were useful for a quick answer to the question "Is today a good day to fly?"


At my previous place of work office hours were ridiculous. It was a software company and my boss was sucked. He would give us work right around 5:30PM (right when it was time to go home) and made us finish the job until past 11:00PM (way past our ideal productive hours). Or he would find annoying problems in code that was still in progress.

So I made a batch file and a script that would turn my computer OFF at a random time between 7:00PM and 8:00PM. It had a 1 minute timer just in case I would stay after hours and needed to abort the shutdown process.

But I would leave my desk before 5:00PM so he couldn't find me to keep me if he wanted to dump crap around checkout time. If he came to my desk and see my computer on, he would think I was still around the pantry area or at the nearby minimart to grab some chips or something. But if it was off around that time, he would call my cell phone and tell me to get back to the office.

I also scheduled the BIOS on my machine to turn my machine ON around 8:00AM or 9:00AM in case I felt lazy and wanted to stroll in around 10:00AM or 11:00AM. If I got caught walking to my desk he would ask "where have you been all morning?" And I would say "I was at a meeting with the marketing team." or "I was out getting breakfast."

dumb dog



For those of us who don't remember where we are on unix, or which SID we are using.
Pop this in your .profile.

<br>function CD
<br>{
<br>   unalias cd
<br>   command cd "$@" && PS1="\${ORACLE_SID}:$(hostname):$PWD> "
<br>   alias cd=CD
<br>}
<br>
alias cd=CD

Not every day, but I did use XSLT script to create my wedding invitations (a Pages file for the inserts to the invite cards, and an HTML file for the address labels).


Running WinXP and I never seem to have time to kick off a defrag and wait for it to finish. So I wrote my own script to fire off XP's builtin defrag.exe and scheduled it to run nitely. The results are saved to a log file in C:\Temp for later review.

@echo off

GOTO :MAIN
###########################################################
#
#  Reason: 
#     This script runs the defrag utility.
#
#  Suggestion:
#     Schedule this script to run daily (via schtasks)
#
#     Example:
#        SCHTASKS /Create /SC DAILY /ST 03:00:00 
#                 /TR \"C:\path\to\DAILY_DEFRAG.BAT" /TN "Daily Defrag of C Drive\"
#
#     Example:
#        AT 03:00 /every:Su,M,T,W,Th,F,Sa C:\path\to\DAILY_DEFRAG.BAT
#
#  Required OS: 
#     Windows XP or Windows Server 2003
#
#  Required files:
#     DEFRAG.EXE
#
#
###########################################################

:MAIN

   :: Output a listing of scheduled tasks
   SCHTASKS /QUERY /V > C:\temp\schtasks.out



   :: *****************************************************
   :: * SITE SPECIFIC Program Parameters                  *
   :: *****************************************************
   :: * Drive to defrag
        SET TARGET=C:

   :: * Log file
        SET LOGFILE=C:\temp\defrag.log


   :: *****************************************************
   :: * No editable parameters below this line            *
   :: *****************************************************


   SETLOCAL


   :: Announce intentions
   echo.
   echo Beginning defragmentation of disk %TARGET%
   echo ----------------------------------------------

   echo.
   for /f "tokens=1 delims=_" %%a in ('date /t') do set NOW=%%a
   for /f "tokens=1 delims=_" %%a in ('time /t') do set NOW=%NOW% %%a
   echo  Start time: %NOW%

   :: Run the defrag utility
   C:\WINNT\SYSTEM32\defrag.exe %TARGET% -f -v > %LOGFILE%

   echo.
   for /f "tokens=1 delims=_" %%a in ('date /t') do set NOW=%%a
   for /f "tokens=1 delims=_" %%a in ('time /t') do set NOW=%NOW% %%a
   echo    End time: %NOW%

   echo.
   echo ----------------------------------------------
   echo Defrag complete. 
   echo.


:END

I'm a private pilot. I wrote a couple of scripts that obtain weather information for local airports from aviationweather.gov. They were useful for a quick answer to the question "Is today a good day to fly?"


Anime CRC32 checksum:

#!/usr/bin/python                                                                                                                                                                                  

import sys, re, zlib

c_null="^[[00;00m"
c_red="^[[31;01m"
c_green="^[[32;01m"

def crc_checksum(filename):
    filedata = open(filename, "rb").read()
    sum = zlib.crc32(filedata)
    if sum < 0:
        sum &= 16**8-1
    return "%.8X" %(sum)

for file in sys.argv[1:]:
    sum = crc_checksum(file)
    try:
        dest_sum = re.split('[\[\]]', file)[-2]
        if sum == dest_sum:
            c_in = c_green
        else:
            c_in = c_red
        sfile = file.split(dest_sum)
        print "%s%s%s   %s%s%s%s%s" % (c_in, sum, c_null, sfile[0], c_in, dest_sum, c_null, sfile[1])
    except IndexError:
        print "%s   %s" %(sum, file)

A simple all around shell function. Just when I get too lazy to think about what I am trying to do.

Really useful when I am just browsing around some random directory, and I have to switch from ls, to cd, to less constantly.

en() {
if [[ -z $1 ]] ; then
ls '.'

elif [[ -d $1 ]] ; then
cd $1

elif [[ -f $1 ]] ; then
less <$1
fi
}

Well back in 2005 I used Gentoo Linux and I used a lot a small program called genlop to show me the history of what I've emerged (installed) on my gentoo box. Well to simplify my work I've written not a small python script but a large one, but at that time I just started using python:

    #!/usr/bin/python
##############################################
# Gentoo emerge status              #   
# This script requires genlop,           #   
# you can install it using `emerge genlop`.  #
# Milot Shala <[email protected]>        #
##############################################

import sys
import os
import time

#colors
color={}
color["r"]="\x1b[31;01m"
color["g"]="\x1b[32;01m"
color["b"]="\x1b[34;01m"
color["0"]="\x1b[0m"


def r(txt):
   return color["r"]+txt+color["0"]
def g(txt):
   return color["g"]+txt+color["0"]
def b(txt):
   return color["b"]+txt+color["0"]

# View Options
def view_opt():   

   print
   print
   print g("full-info - View full information for emerged package")
   print g("cur - View current emerge")
   print g("hist - View history of emerged packages by day")
   print g("hist-all - View full list of history of emerged packages")
   print g("rsync - View rsync history")
   print g("time - View time for compiling a package")
   print g("time-unmerged - View time of unmerged packages")
   print
   command = raw_input(r("Press Enter to return to main "))
   if command == '':
      c()
      program()
   else:
      c()
      program()

# system command 'clear'
def c():
   os.system('clear')


# Base program
def program():
   c()
   print g("Gentoo emerge status script")
   print ("---------------------------")
   print

   print ("1]") + g(" Enter options")
   print ("2]") + g(" View options")
   print ("3]") + g(" Exit")
   print
   command = input("[]> ")


   if command == 1:   
      print
      print r("""First of all  you must view options to know what to use, you can enter option name ( if you know any ) or type `view-opt` to view options.""")
      print
      time.sleep(2)
      command = raw_input(b("Option name: "))
      if (command == 'view-opt' or command == 'VIEW-OPT'):
         view_opt()


      elif command == 'full-info':
         c()
         print g("Full information for a single package")
         print ("-------------------------------------")
         print
         print b("Enter package name")
         command=raw_input("> ")
         c()
         print g("Full information for package"), b(command)
         print ("-----------------------------------")
         print
         pack=['genlop -i '+command]
         pack_=" ".join(pack)
         os.system(pack_)
         print
         print r("Press Enter to return to main.")
         command=raw_input()
         if command == '':
            c()
            program()

         else:
            c()
            program()


      elif command == 'cur':
         if command == 'cur':
            c()
            print g("Current emerge session(s)")
            print ("-------------------------")
            print
            print b("Listing current emerge session(s)")
            print
            time.sleep(1)
            os.system('genlop -c')
            print
            print r("Press Enter to return to main.")
            command = raw_input()
            if (command == ''):
               c()
               program()

            else:
               c()
               program()


      elif command == 'hist':
         if command == 'hist':
            c()
            print g("History of merged packages")
            print ("---------------------------")
            print
            time.sleep(1)
            print b("Enter number of how many days ago you want to see the packages")
            command = raw_input("> ")
            c()
            print g("Packages merged "+b(command)+ g(" day(s) before"))
            print ("------------------------------------")
            pkg=['genlop --list --date '+command+' days ago']
            pkg_=" ".join(pkg)
            os.system(pkg_)
            print
            print r("Press Enter to return to main.")
            command = raw_input()
            if command == '':
               c()
               program()

            else:
               c()
               program()


      elif command == 'hist-all':
            c()
            print g("Full history of merged individual packages")
            print ("--------------------------------------")
            print
            print b("Do you want to view individual package?")
            print r("YES/NO?")
            command = raw_input("> ")
            print
            if (command == 'yes' or command == 'YES'):
               print g("Enter package name")
               command = raw_input("> ")
               print
               pkg=['genlop -l | grep '+command+ ' | less']
               pkg_=" ".join(pkg)
               os.system(pkg_)
               print
               print r("Press Enter to return to main")
               command = raw_input()
               if command == '':
                  c()
                  program()
               else:
                  c()
                  program()

            elif (command == 'no' or command == 'NO'):
               pkg=['genlop -l | less']
               pkg_=" ".join(pkg)
               os.system(pkg_)
               print
               print r("Press Enter to return to main")
               command = raw_input()
               if command == '':
                  c()
                  program()

               else:
                  c()
                  program()

            else:
               c()
               program()


      elif command == 'rsync':
         print g("RSYNC updates")
         print
         print
         print
         print b("You can view rsynced time by year!")
         print r("Do you want this script to do it for you? (yes/no)")
         command = raw_input("> ")
         if (command == 'yes' or command == 'YES'):
            print
            print g("Enter year i.e"), b("2005")
            print
            command = raw_input("> ")
            rsync=['genlop -r | grep '+command+' | less']
            rsync_=" ".join(rsync)
            os.system(rsync_)
            print
            print r("Press Enter to return to main.")
            c()
            program()
         elif (command == 'no' or command == 'NO'):
            os.system('genlop -r | less')
            print
            print r("Press Enter to return to main.")
            command = raw_input()
            if command == '':
               c()
               program()

            else:
               c()
               program()

      elif command == 'time':
         c()
         print g("Time of package compilation")
         print ("---------------------------")
         print
         print

         print b("Enter package name")
         pkg_name = raw_input("> ")
         pkg=['emerge '+pkg_name+' -p | genlop -p | less']
         pkg_=" ".join(pkg)
         os.system(pkg_)
         print
         print r("Press Enter to return to main")
         time.sleep(2)
         command = raw_input()
         if command == '':
            c()
            program()

         else:
            c()
            program()


      elif command == 'time-unmerged':
         c()
         print g("Show when package(s) is/when is unmerged")
         print ("----------------------------------------")
         print

         print b("Enter package name: ")
         name = raw_input("> ")
         pkg=['genlop -u '+name]
         pkg_=" ".join(pkg)
         os.system(pkg_)
         print
         print r("Press Enter to return to main")
         time.sleep(2)
         command = raw_input()
         if command == '':
            c()
            program()

         else:
            c()
            program()

      else:
         print
         print r("Wrong Selection!")
         time.sleep(2)
         c()
         program()


   elif command == 2:
      view_opt()
      command = raw_input(r("Press Enter to return to main "))
      if command == '':
         c()
         program()
      else:
         c()
         program()


   elif command == 3:
      print
      print b("Thank you for using this script")
      print
      time.sleep(1)
      sys.exit()

   else:
      print
      print r("Wrong Selection!")
      time.sleep(2)
      c()
      program()
      command = ("")


program()

I wrote a python program to calculate my apartment's shared spending and output a neat little grid, with roommate as the columns and expense category as the row, along with how much money each roommate owed for rent, after adjusting for his contribution toward shared expenses. We'd been sharing this way for a while, but just adding up raw totals at the end of the month. I needed more granular control. With a maximum of eight keystrokes per line-item, this is way better than excel. I was sort of on a desperate quest to stop the monthly trend of one roommate spending 25% of our budget on beverages...


I use this as an autoloaded function. I can just type "mycd" and a list of directories appears which I frequently cd to. If I happen to know then number I can just say something like "mycd 2". To add a directory to the list you just type "mycd /tmp/foo/somedirectory".

function mycd {

MYCD=/tmp/mycd.txt
touch ${MYCD}

typeset -i x
typeset -i ITEM_NO
typeset -i i
x=0

if [[ -n "${1}" ]]; then
   if [[ -d "${1}" ]]; then
      print "${1}" >> ${MYCD}
      sort -u ${MYCD} > ${MYCD}.tmp
      mv ${MYCD}.tmp ${MYCD}
      FOLDER=${1}
   else
      i=${1}
      FOLDER=$(sed -n "${i}p" ${MYCD})
   fi
fi

if [[ -z "${1}" ]]; then
   print ""
   cat ${MYCD} | while read f; do
      x=$(expr ${x} + 1)
      print "${x}. ${f}"
   done
   print "\nSelect #"
   read ITEM_NO
   FOLDER=$(sed -n "${ITEM_NO}p" ${MYCD})
fi

if [[ -d "${FOLDER}" ]]; then
   cd ${FOLDER}
fi

}

A perl script that scrapes my local Craigslist, by selected categories, in to a SQL DB which I can then query against.

V2 of this updates the DB with a timer and alerts me if I have a match on any of the queries, basically providing me with a background agent for CL.


I don't have the code any more, but possibly the most useful script I wrote was, believe it or not, in VBA. I had an annoying colleague who had such a short fuse that I referred to him as Cherry Bomb. He would often get mad when customers would call and then stand up and start ranting at me over the cubicle wall, killing my productivity and morale.

I always had Microsoft Excel open. When he would do this, I would alt-tab to Excel and there, on the toolbar, was a new icon with an image of a cherry bomb. I would discreetly click that ... and nothing would happen.

However, shortly after that I would get a phone call and would say something like "yeah, yeah, that sounds bad. I had better take a look." And then I would get up, apologize to the Cherry Bomb and walk away.

What happened is that we used NetWare and it had a primitive messaging system built in. When I clicked the button, a small VBA script would send out a NetWare message to my friends, telling them that the Cherry Bomb was at it again and would they please call me. He never figured it out :)


A similar backup.sh for each project, that tars and gzips just the source, moves it into a snapshot directory and labels it with timestamp: project-mmddyy-hhmmss. Useful for coding between commits.


I wrote a script that ended up being used every day in my team. When I used to work for Intel we had an app that talked to an access database to grab a dump of register information (I worked on validating chipsets). It would take this information (from a SQL query) and dump it into a CSV file, HTML file, and an Excel file. The whole process took almost 2 hours. No joke. No idea why it took so long. We would start it up an hour before lunch, go to lunch, and then come back.

I thought that there had to be a better way of doing this. I talked to the team that maintained the registry database and got the SQL code from them. I then wrote a perl script that grabbed the data and outputted it into CSV, HTML, and Excel formats. Runtime? Around 1-2 seconds. A great speed improvement.

I also wrote a few scripts while I was on deployment in Iraq in 2006 (I served in the National Guard for 9 years - got out in December). We used this old app called ULLS-G (Unit Level Logistics System - Ground) that was written in ADA and originally ran on DOS. They hacked it enough to where it would run on Windows XP in a command shell. This system didn't have a mouse interface. Everything was via keyboard and it had NO batch functionality. So let's say you wanted to print out licenses for all vehicle operators? Well... we had 150 soldiers in our unit so it took a LONG time. Let's say everyone got qualified on a new vehicle and you wanted to add it to everyone's operator qualifications? You had to do it one by one.

I was able to find an ODBC driver for the SAGE database (what ULLS-G used) and so I wrote perl scripts that were able to talk to the SAGE database. So things that took over an hour, now took only a few seconds. I also used my scripts and the driver for reporting. We had to report all information up to battalion every morning. Other units would write the information in by hand every morning. I whipped up an Excel macro that talked used the same driver and talked to the SAGE database and updated the Excel spreadsheet that way. It's the most complicated and only Excel macro I've ever written. It paid off because they awarded me the Army Commendation Medal. So yeah, I got a medal in the military for writing perl scripts :) How many can say that? ;)


I use this as an autoloaded function. I can just type "mycd" and a list of directories appears which I frequently cd to. If I happen to know then number I can just say something like "mycd 2". To add a directory to the list you just type "mycd /tmp/foo/somedirectory".

function mycd {

MYCD=/tmp/mycd.txt
touch ${MYCD}

typeset -i x
typeset -i ITEM_NO
typeset -i i
x=0

if [[ -n "${1}" ]]; then
   if [[ -d "${1}" ]]; then
      print "${1}" >> ${MYCD}
      sort -u ${MYCD} > ${MYCD}.tmp
      mv ${MYCD}.tmp ${MYCD}
      FOLDER=${1}
   else
      i=${1}
      FOLDER=$(sed -n "${i}p" ${MYCD})
   fi
fi

if [[ -z "${1}" ]]; then
   print ""
   cat ${MYCD} | while read f; do
      x=$(expr ${x} + 1)
      print "${x}. ${f}"
   done
   print "\nSelect #"
   read ITEM_NO
   FOLDER=$(sed -n "${ITEM_NO}p" ${MYCD})
fi

if [[ -d "${FOLDER}" ]]; then
   cd ${FOLDER}
fi

}

Wrote a script to click my start button, then click it again in half a second, and repeat every 30 seconds.

Keeps me marked Online while at work, and I can get the real work done on my personal laptop right next to it. Not bogged down by work software.

Don't tell the boss :)


This keeps 20 days of diff backups without using a bunch of space. Uses links to copy and rsync copies as necessary


#!/bin/bash                                                                                                                                                                                                                            


BACKUPDIR=/media/proxy/store/backups/                                                                                                                                                                                                  

[ ! -d $BACKUPDIR ] && { echo "BACKUP DIRECTORY NOT AVAILABLE!"; exit; }                                                                                                                                                               

dobackup() {                                                                                                                                                                                                                           
        SDIR=$2                                                                                                                                                                                                                        
        PARENTDIR=$1                                                                                                                                                                                                                   
        echo "BACKING UP $PARENTDIR/$SDIR to $BACKUPDIR"                                                                                                                                                                               
        bnum=20
        count=$bnum
        [ -d ${BACKUPDIR}${SDIR}.$bnum ] && {  mv ${BACKUPDIR}${SDIR}.$bnum ${BACKUPDIR}${SDIR}.tmp; }
        until [ $count -eq 1 ]; do
                let lastdir=$count-1
                [ -d ${BACKUPDIR}${SDIR}.$lastdir ] && { mv ${BACKUPDIR}${SDIR}.$lastdir ${BACKUPDIR}${SDIR}.$count; }
                let count-=1
        done
        cp -al  ${BACKUPDIR}${SDIR}.0  ${BACKUPDIR}${SDIR}.1
        rsync -a --delete --bwlimit=2000  $PARENTDIR/$SDIR ${BACKUPDIR}${SDIR}.0
}

for backup in $(cat /sbin/backup.directories); do
        PDIR=$(echo $backup | awk -F '::' {'print$1'})
        DIR=$(echo $backup | awk -F '::' {'print$2'})
        dobackup $PDIR $DIR
done

exit;


cat /sbin/backup.directories
/media/warehouse::Archive
/media/warehouse::concept


I have a batch file which establishes a VPN connection and then enters an infinite loop, pinging a machine on the other side of the connection every five minutes so that the VPN server doesn't drop the connection due to inactivity if I don't generate any traffic over that connection for a while.


I used to work at a technology summer camp, and we had to compose these write-ups for each of the kids in the group at the end of the week, which they would then receive and take home as a keepsake. Usually, these consisted of a bunch of generic sentences, and one to two personalized sentences. I wrote a python script which constructed one of these write-ups out of a bank of canned sentences, and allowed the user to add a couple of personalized sentences in the middle. This saved a huge amount of time for me and other counselors I let in on the secret. Even though so much of it was automated, our write-ups still looked better than many of the 'honest' ones, because we could put more time into the personalized parts.


Running WinXP and I never seem to have time to kick off a defrag and wait for it to finish. So I wrote my own script to fire off XP's builtin defrag.exe and scheduled it to run nitely. The results are saved to a log file in C:\Temp for later review.

@echo off

GOTO :MAIN
###########################################################
#
#  Reason: 
#     This script runs the defrag utility.
#
#  Suggestion:
#     Schedule this script to run daily (via schtasks)
#
#     Example:
#        SCHTASKS /Create /SC DAILY /ST 03:00:00 
#                 /TR \"C:\path\to\DAILY_DEFRAG.BAT" /TN "Daily Defrag of C Drive\"
#
#     Example:
#        AT 03:00 /every:Su,M,T,W,Th,F,Sa C:\path\to\DAILY_DEFRAG.BAT
#
#  Required OS: 
#     Windows XP or Windows Server 2003
#
#  Required files:
#     DEFRAG.EXE
#
#
###########################################################

:MAIN

   :: Output a listing of scheduled tasks
   SCHTASKS /QUERY /V > C:\temp\schtasks.out



   :: *****************************************************
   :: * SITE SPECIFIC Program Parameters                  *
   :: *****************************************************
   :: * Drive to defrag
        SET TARGET=C:

   :: * Log file
        SET LOGFILE=C:\temp\defrag.log


   :: *****************************************************
   :: * No editable parameters below this line            *
   :: *****************************************************


   SETLOCAL


   :: Announce intentions
   echo.
   echo Beginning defragmentation of disk %TARGET%
   echo ----------------------------------------------

   echo.
   for /f "tokens=1 delims=_" %%a in ('date /t') do set NOW=%%a
   for /f "tokens=1 delims=_" %%a in ('time /t') do set NOW=%NOW% %%a
   echo  Start time: %NOW%

   :: Run the defrag utility
   C:\WINNT\SYSTEM32\defrag.exe %TARGET% -f -v > %LOGFILE%

   echo.
   for /f "tokens=1 delims=_" %%a in ('date /t') do set NOW=%%a
   for /f "tokens=1 delims=_" %%a in ('time /t') do set NOW=%NOW% %%a
   echo    End time: %NOW%

   echo.
   echo ----------------------------------------------
   echo Defrag complete. 
   echo.


:END

I've written a small shell script, tapt, for Debian based system. esp. Ubuntu. What it basically does is to post all your "apt-get" activities to your twitter account. It helps me to keep the track of what and when I've installed/remove programs in my Ubuntu system. I created a new Twitter account just for this and kept it private. Really useful. More information here: http://www.quicktweaks.com/tapt/


#!/usr/bin/perl
use strict;
use utf8;
use Encode;
use File::Find;
binmode STDOUT, ':utf8';
sub orderly {
    my ($x, $y) = @_{$a, $b};
    if (my $z = $x <=> $y) {return $z}
    $x = length $a;
    $y = length $b;
    my $z = $x < $y ? $x : $y;
    if (substr($a, 0, $z) eq substr($b, 0, $z)) {
        return $y <=> $x;
    }
    else {
        return $a cmp $b;
    }
}
my %conf = map +($_ => 0), split //, 'acsxL';
sub Stat {$conf{L} ? lstat : stat}
my @dirs = ();
while (defined ($_ = shift)) {
    if ($_ eq "--") {push @dirs, @ARGV; last}
    elsif (/^-(.*)$/s) {
        for (split //, $1) {
            if (!exists $conf{$_} or $conf{$_} = 1 and $conf{a} and $conf{s}) {
                print STDERR "$0 [-a] [-c] [-s] [-x] [-L] [--] ...\n";
                exit 1;
            }
        }
    }
    else {push @dirs, $_}
}
s/\/*$//s for @dirs;  # */ SO has crappy syntax highlighting
@dirs = qw(.) unless @dirs;
my %spec = (follow => $conf{L}, no_chdir => 1);
if ($conf{a}) {
    $spec{wanted} = sub {
        Stat;
        my $s = -f _ ? -s _ : 0;
        decode(utf8 => $File::Find::name) =~ /^\Q$dirs[0]\E\/?(.*)$/s;
        my @a = split /\//, $1;
        for (unshift @a, $dirs[0]; @a; pop @a) {
            $_{join "/", @a} += $s;
        }
    };
}
elsif ($conf{s}) {
    $spec{wanted} = sub {
        Stat;
        $_{$dirs[0]} += -f _ ? -s _ : 0;
    };
}
else {
    $spec{wanted} = sub {
        Stat;
        my $s = -f _ ? -s _ : 0;
        decode(utf8 => $File::Find::name) =~ /^\Q$dirs[0]\E\/?(.*)$/s;
        my @a = split /\//, $1;
        ! -d _ and pop @a;
        for (unshift @a, $dirs[0]; @a; pop @a) {
            $_{join "/", @a} += $s;
        }
    };
}
if ($conf{x}) {
    $spec{preprocess} = sub {
        my $dev = (Stat $File::Find::dir)[0];
        grep {$dev == (Stat "$File::Find::dir/$_")[0]} @_;
    };
}
while (@dirs) {
    find(\%spec, $dirs[0] eq "" ? "/" : $dirs[0]);
    $_{""} += $_{$dirs[0]} if $conf{c};
    shift @dirs;
}
$_{$_} < 1024 ** 1 ? printf "%s «%-6.6sB» %s\n", $_{$_}, sprintf("%6.6f", "$_{$_}" / 1024 ** 0), $_ :
$_{$_} < 1024 ** 2 ? printf "%s «%-6.6sK» %s\n", $_{$_}, sprintf("%6.6f", "$_{$_}" / 1024 ** 1), $_ :
$_{$_} < 1024 ** 3 ? printf "%s «%-6.6sM» %s\n", $_{$_}, sprintf("%6.6f", "$_{$_}" / 1024 ** 2), $_ :
$_{$_} < 1024 ** 4 ? printf "%s «%-6.6sG» %s\n", $_{$_}, sprintf("%6.6f", "$_{$_}" / 1024 ** 3), $_ :
$_{$_} < 1024 ** 5 ? printf "%s «%-6.6sT» %s\n", $_{$_}, sprintf("%6.6f", "$_{$_}" / 1024 ** 4), $_ :
$_{$_} < 1024 ** 6 ? printf "%s «%-6.6sP» %s\n", $_{$_}, sprintf("%6.6f", "$_{$_}" / 1024 ** 5), $_ :
$_{$_} < 1024 ** 7 ? printf "%s «%-6.6sE» %s\n", $_{$_}, sprintf("%6.6f", "$_{$_}" / 1024 ** 6), $_ :
$_{$_} < 1024 ** 8 ? printf "%s «%-6.6sZ» %s\n", $_{$_}, sprintf("%6.6f", "$_{$_}" / 1024 ** 7), $_ :
                     printf "%s «%-6.6sY» %s\n", $_{$_}, sprintf("%6.6f", "$_{$_}" / 1024 ** 8), $_
    for grep {$_{$_} > 0} sort orderly keys %_;

I save it in ~/bin/dush, it acts as a sort of du -h/du | sort -n hybrid: sorts and gives human-readable sizes all at once. Very useful for finding what's taking up disk space.

In a similar vein,

#!/usr/bin/perl
$t = 1;
%p = map {$_ => ($t *= 1024)} qw(K M G T P E Z Y);
$t = 4707319808;
if (@ARGV) {
    if (($_ = shift) =~ /^-*dvd/i) {$t = 4707319808}
    elsif (/^-*cd[^w]*$/i) {$t = 737280000}
    elsif (/^-*cd/i) {$t = 681984000}
    elsif (/^-*([\d.]+)([kmgtpezy])/i) {$t = $1 * ($p{"\U$2"} || 1)}
    elsif (/^-*([\d.]+)/) {$t = $1}
    else {unshift @ARGV, $_}
}
($q, $r, $s) = (0, ($ENV{COLUMNS} || 80) - 13, $t);
while (<>) {
    chomp, stat;
    unless (-e _) {
        print STDERR "$_ does not exist\n";
        next;
    }
    if (($s += -s _) > $t) {
        $s && $s < $t && printf "-%7s %s\n",
            sprintf("%2.3f%%", 100 * ($t - $s) / $t), $t - $s;
        printf "-----------%d%*s\n", ++$q, $r, "-" x $r;
        $s = -s _;
    }
    printf "%8s %s\n",
        sprintf("%3.3f%%", $s * 100 / $t),
        /.{4}(.{$r})$/s ? "...$1" : $_;
}
$s && $s < $t && printf "-%7s %s\n",
    sprintf("%2.3f%%", 100 * ($t - $s) / $t), $t - $s;

I save this as ~/bin/fit. When I'm archiving a bunch of files, I run ls | fit or ls | fit -cdrw to help determine if it'll fit on a DVD/CD/CDRW, and where to split them if they don't.


I wrote a simple VB app that tracked which game numbers of Freecell I had played and successfully completed, and always launched it with a different seed.

....starting from 1....

Max game number is 65k. Rather sadly after more than 5 years I am still in only the hundreds. But at least I know I've never played the same hand twice!

** Postscript - it's the only VB app I've ever written. I ran screaming back to C++....


Well back in 2005 I used Gentoo Linux and I used a lot a small program called genlop to show me the history of what I've emerged (installed) on my gentoo box. Well to simplify my work I've written not a small python script but a large one, but at that time I just started using python:

    #!/usr/bin/python
##############################################
# Gentoo emerge status              #   
# This script requires genlop,           #   
# you can install it using `emerge genlop`.  #
# Milot Shala <[email protected]>        #
##############################################

import sys
import os
import time

#colors
color={}
color["r"]="\x1b[31;01m"
color["g"]="\x1b[32;01m"
color["b"]="\x1b[34;01m"
color["0"]="\x1b[0m"


def r(txt):
   return color["r"]+txt+color["0"]
def g(txt):
   return color["g"]+txt+color["0"]
def b(txt):
   return color["b"]+txt+color["0"]

# View Options
def view_opt():   

   print
   print
   print g("full-info - View full information for emerged package")
   print g("cur - View current emerge")
   print g("hist - View history of emerged packages by day")
   print g("hist-all - View full list of history of emerged packages")
   print g("rsync - View rsync history")
   print g("time - View time for compiling a package")
   print g("time-unmerged - View time of unmerged packages")
   print
   command = raw_input(r("Press Enter to return to main "))
   if command == '':
      c()
      program()
   else:
      c()
      program()

# system command 'clear'
def c():
   os.system('clear')


# Base program
def program():
   c()
   print g("Gentoo emerge status script")
   print ("---------------------------")
   print

   print ("1]") + g(" Enter options")
   print ("2]") + g(" View options")
   print ("3]") + g(" Exit")
   print
   command = input("[]> ")


   if command == 1:   
      print
      print r("""First of all  you must view options to know what to use, you can enter option name ( if you know any ) or type `view-opt` to view options.""")
      print
      time.sleep(2)
      command = raw_input(b("Option name: "))
      if (command == 'view-opt' or command == 'VIEW-OPT'):
         view_opt()


      elif command == 'full-info':
         c()
         print g("Full information for a single package")
         print ("-------------------------------------")
         print
         print b("Enter package name")
         command=raw_input("> ")
         c()
         print g("Full information for package"), b(command)
         print ("-----------------------------------")
         print
         pack=['genlop -i '+command]
         pack_=" ".join(pack)
         os.system(pack_)
         print
         print r("Press Enter to return to main.")
         command=raw_input()
         if command == '':
            c()
            program()

         else:
            c()
            program()


      elif command == 'cur':
         if command == 'cur':
            c()
            print g("Current emerge session(s)")
            print ("-------------------------")
            print
            print b("Listing current emerge session(s)")
            print
            time.sleep(1)
            os.system('genlop -c')
            print
            print r("Press Enter to return to main.")
            command = raw_input()
            if (command == ''):
               c()
               program()

            else:
               c()
               program()


      elif command == 'hist':
         if command == 'hist':
            c()
            print g("History of merged packages")
            print ("---------------------------")
            print
            time.sleep(1)
            print b("Enter number of how many days ago you want to see the packages")
            command = raw_input("> ")
            c()
            print g("Packages merged "+b(command)+ g(" day(s) before"))
            print ("------------------------------------")
            pkg=['genlop --list --date '+command+' days ago']
            pkg_=" ".join(pkg)
            os.system(pkg_)
            print
            print r("Press Enter to return to main.")
            command = raw_input()
            if command == '':
               c()
               program()

            else:
               c()
               program()


      elif command == 'hist-all':
            c()
            print g("Full history of merged individual packages")
            print ("--------------------------------------")
            print
            print b("Do you want to view individual package?")
            print r("YES/NO?")
            command = raw_input("> ")
            print
            if (command == 'yes' or command == 'YES'):
               print g("Enter package name")
               command = raw_input("> ")
               print
               pkg=['genlop -l | grep '+command+ ' | less']
               pkg_=" ".join(pkg)
               os.system(pkg_)
               print
               print r("Press Enter to return to main")
               command = raw_input()
               if command == '':
                  c()
                  program()
               else:
                  c()
                  program()

            elif (command == 'no' or command == 'NO'):
               pkg=['genlop -l | less']
               pkg_=" ".join(pkg)
               os.system(pkg_)
               print
               print r("Press Enter to return to main")
               command = raw_input()
               if command == '':
                  c()
                  program()

               else:
                  c()
                  program()

            else:
               c()
               program()


      elif command == 'rsync':
         print g("RSYNC updates")
         print
         print
         print
         print b("You can view rsynced time by year!")
         print r("Do you want this script to do it for you? (yes/no)")
         command = raw_input("> ")
         if (command == 'yes' or command == 'YES'):
            print
            print g("Enter year i.e"), b("2005")
            print
            command = raw_input("> ")
            rsync=['genlop -r | grep '+command+' | less']
            rsync_=" ".join(rsync)
            os.system(rsync_)
            print
            print r("Press Enter to return to main.")
            c()
            program()
         elif (command == 'no' or command == 'NO'):
            os.system('genlop -r | less')
            print
            print r("Press Enter to return to main.")
            command = raw_input()
            if command == '':
               c()
               program()

            else:
               c()
               program()

      elif command == 'time':
         c()
         print g("Time of package compilation")
         print ("---------------------------")
         print
         print

         print b("Enter package name")
         pkg_name = raw_input("> ")
         pkg=['emerge '+pkg_name+' -p | genlop -p | less']
         pkg_=" ".join(pkg)
         os.system(pkg_)
         print
         print r("Press Enter to return to main")
         time.sleep(2)
         command = raw_input()
         if command == '':
            c()
            program()

         else:
            c()
            program()


      elif command == 'time-unmerged':
         c()
         print g("Show when package(s) is/when is unmerged")
         print ("----------------------------------------")
         print

         print b("Enter package name: ")
         name = raw_input("> ")
         pkg=['genlop -u '+name]
         pkg_=" ".join(pkg)
         os.system(pkg_)
         print
         print r("Press Enter to return to main")
         time.sleep(2)
         command = raw_input()
         if command == '':
            c()
            program()

         else:
            c()
            program()

      else:
         print
         print r("Wrong Selection!")
         time.sleep(2)
         c()
         program()


   elif command == 2:
      view_opt()
      command = raw_input(r("Press Enter to return to main "))
      if command == '':
         c()
         program()
      else:
         c()
         program()


   elif command == 3:
      print
      print b("Thank you for using this script")
      print
      time.sleep(1)
      sys.exit()

   else:
      print
      print r("Wrong Selection!")
      time.sleep(2)
      c()
      program()
      command = ("")


program()

I wrote some lines of code to automatically tweak all things powertop suggests when I unplug my laptop and undo that if I plug the laptop back in. Maximum power, maximum efficiency, maximum convenience.


Well back in 2005 I used Gentoo Linux and I used a lot a small program called genlop to show me the history of what I've emerged (installed) on my gentoo box. Well to simplify my work I've written not a small python script but a large one, but at that time I just started using python:

    #!/usr/bin/python
##############################################
# Gentoo emerge status              #   
# This script requires genlop,           #   
# you can install it using `emerge genlop`.  #
# Milot Shala <[email protected]>        #
##############################################

import sys
import os
import time

#colors
color={}
color["r"]="\x1b[31;01m"
color["g"]="\x1b[32;01m"
color["b"]="\x1b[34;01m"
color["0"]="\x1b[0m"


def r(txt):
   return color["r"]+txt+color["0"]
def g(txt):
   return color["g"]+txt+color["0"]
def b(txt):
   return color["b"]+txt+color["0"]

# View Options
def view_opt():   

   print
   print
   print g("full-info - View full information for emerged package")
   print g("cur - View current emerge")
   print g("hist - View history of emerged packages by day")
   print g("hist-all - View full list of history of emerged packages")
   print g("rsync - View rsync history")
   print g("time - View time for compiling a package")
   print g("time-unmerged - View time of unmerged packages")
   print
   command = raw_input(r("Press Enter to return to main "))
   if command == '':
      c()
      program()
   else:
      c()
      program()

# system command 'clear'
def c():
   os.system('clear')


# Base program
def program():
   c()
   print g("Gentoo emerge status script")
   print ("---------------------------")
   print

   print ("1]") + g(" Enter options")
   print ("2]") + g(" View options")
   print ("3]") + g(" Exit")
   print
   command = input("[]> ")


   if command == 1:   
      print
      print r("""First of all  you must view options to know what to use, you can enter option name ( if you know any ) or type `view-opt` to view options.""")
      print
      time.sleep(2)
      command = raw_input(b("Option name: "))
      if (command == 'view-opt' or command == 'VIEW-OPT'):
         view_opt()


      elif command == 'full-info':
         c()
         print g("Full information for a single package")
         print ("-------------------------------------")
         print
         print b("Enter package name")
         command=raw_input("> ")
         c()
         print g("Full information for package"), b(command)
         print ("-----------------------------------")
         print
         pack=['genlop -i '+command]
         pack_=" ".join(pack)
         os.system(pack_)
         print
         print r("Press Enter to return to main.")
         command=raw_input()
         if command == '':
            c()
            program()

         else:
            c()
            program()


      elif command == 'cur':
         if command == 'cur':
            c()
            print g("Current emerge session(s)")
            print ("-------------------------")
            print
            print b("Listing current emerge session(s)")
            print
            time.sleep(1)
            os.system('genlop -c')
            print
            print r("Press Enter to return to main.")
            command = raw_input()
            if (command == ''):
               c()
               program()

            else:
               c()
               program()


      elif command == 'hist':
         if command == 'hist':
            c()
            print g("History of merged packages")
            print ("---------------------------")
            print
            time.sleep(1)
            print b("Enter number of how many days ago you want to see the packages")
            command = raw_input("> ")
            c()
            print g("Packages merged "+b(command)+ g(" day(s) before"))
            print ("------------------------------------")
            pkg=['genlop --list --date '+command+' days ago']
            pkg_=" ".join(pkg)
            os.system(pkg_)
            print
            print r("Press Enter to return to main.")
            command = raw_input()
            if command == '':
               c()
               program()

            else:
               c()
               program()


      elif command == 'hist-all':
            c()
            print g("Full history of merged individual packages")
            print ("--------------------------------------")
            print
            print b("Do you want to view individual package?")
            print r("YES/NO?")
            command = raw_input("> ")
            print
            if (command == 'yes' or command == 'YES'):
               print g("Enter package name")
               command = raw_input("> ")
               print
               pkg=['genlop -l | grep '+command+ ' | less']
               pkg_=" ".join(pkg)
               os.system(pkg_)
               print
               print r("Press Enter to return to main")
               command = raw_input()
               if command == '':
                  c()
                  program()
               else:
                  c()
                  program()

            elif (command == 'no' or command == 'NO'):
               pkg=['genlop -l | less']
               pkg_=" ".join(pkg)
               os.system(pkg_)
               print
               print r("Press Enter to return to main")
               command = raw_input()
               if command == '':
                  c()
                  program()

               else:
                  c()
                  program()

            else:
               c()
               program()


      elif command == 'rsync':
         print g("RSYNC updates")
         print
         print
         print
         print b("You can view rsynced time by year!")
         print r("Do you want this script to do it for you? (yes/no)")
         command = raw_input("> ")
         if (command == 'yes' or command == 'YES'):
            print
            print g("Enter year i.e"), b("2005")
            print
            command = raw_input("> ")
            rsync=['genlop -r | grep '+command+' | less']
            rsync_=" ".join(rsync)
            os.system(rsync_)
            print
            print r("Press Enter to return to main.")
            c()
            program()
         elif (command == 'no' or command == 'NO'):
            os.system('genlop -r | less')
            print
            print r("Press Enter to return to main.")
            command = raw_input()
            if command == '':
               c()
               program()

            else:
               c()
               program()

      elif command == 'time':
         c()
         print g("Time of package compilation")
         print ("---------------------------")
         print
         print

         print b("Enter package name")
         pkg_name = raw_input("> ")
         pkg=['emerge '+pkg_name+' -p | genlop -p | less']
         pkg_=" ".join(pkg)
         os.system(pkg_)
         print
         print r("Press Enter to return to main")
         time.sleep(2)
         command = raw_input()
         if command == '':
            c()
            program()

         else:
            c()
            program()


      elif command == 'time-unmerged':
         c()
         print g("Show when package(s) is/when is unmerged")
         print ("----------------------------------------")
         print

         print b("Enter package name: ")
         name = raw_input("> ")
         pkg=['genlop -u '+name]
         pkg_=" ".join(pkg)
         os.system(pkg_)
         print
         print r("Press Enter to return to main")
         time.sleep(2)
         command = raw_input()
         if command == '':
            c()
            program()

         else:
            c()
            program()

      else:
         print
         print r("Wrong Selection!")
         time.sleep(2)
         c()
         program()


   elif command == 2:
      view_opt()
      command = raw_input(r("Press Enter to return to main "))
      if command == '':
         c()
         program()
      else:
         c()
         program()


   elif command == 3:
      print
      print b("Thank you for using this script")
      print
      time.sleep(1)
      sys.exit()

   else:
      print
      print r("Wrong Selection!")
      time.sleep(2)
      c()
      program()
      command = ("")


program()

My .cmd backup script. It runs on my server every night, and names the backup files according the week day. A full week of backups has saved me (and my family) many times:

:: Backup args:
::   /V Verify? (yes/no)
::   /R Restrict access to owner? (yes/no)
::   /RS Removable storage? (yes/no)
::   /HC Hardware compression (on/off)
::   /M Backup type (normal/copy/differential/incremental/daily)
::   /L Log file type (f/s/n)
::   /D "Description"
::   /J "Job-name"
::   /F "File-name"

SETLOCAL

:: ensure that network drives are mounted
CALL C:\bat\configs\MapShares-home.cmd
echo on

set today=%DATE:~0,3%
if %today%==Mon set yesterday=0Sunday
if %today%==Tue set yesterday=1Monday
if %today%==Wed set yesterday=2Tuesday
if %today%==Thu set yesterday=3Wednesday
if %today%==Fri set yesterday=4Thursday
if %today%==Sat set yesterday=5Friday
if %today%==Sun set yesterday=6Saturday

set configsDir=%~dp0
set storePath=C:\mybackups

:: (eg: Monday C files)
set title=%yesterday% backup set


echo %DATE% %TIME% %title% > "%storePath%\%yesterday%_backup.log"

CALL BackupConfigs.bat

:: Create new BKF file
call C:\WINDOWS\system32\ntbackup.exe backup ^
    "@%configsDir%\daily.bks" ^
    /V:yes /R:no /RS:no /HC:off /M normal /L:s ^
    /D "%title%" ^
    /J "%title%.job" ^
    /F "%storePath%\%yesterday%.bkf" ^
    >> "%storePath%\%yesterday%_backup.log"

echo %DATE% %TIME% Completed >> "%storePath%\%yesterday%_backup.log"

copy "%storePath%\%yesterday%.bkf" "V:\Backups\NEPTUNE"

CALL C:\bat\clean-temps.bat

defrag -v C: > "%storePath%\%yesterday%_defrag.log"

:: display backup directories
start /D"C:\bat\Backups\" checkbkf.bat

ENDLOCAL

::pause


A threaded HTML scraper to download all available subtitles for series/movies from a site which is a pain to use (you have to click like 4 times after a search to get to the download page, just to display more ads). Now I just put the search criteria and press download.


I don't have the code any more, but possibly the most useful script I wrote was, believe it or not, in VBA. I had an annoying colleague who had such a short fuse that I referred to him as Cherry Bomb. He would often get mad when customers would call and then stand up and start ranting at me over the cubicle wall, killing my productivity and morale.

I always had Microsoft Excel open. When he would do this, I would alt-tab to Excel and there, on the toolbar, was a new icon with an image of a cherry bomb. I would discreetly click that ... and nothing would happen.

However, shortly after that I would get a phone call and would say something like "yeah, yeah, that sounds bad. I had better take a look." And then I would get up, apologize to the Cherry Bomb and walk away.

What happened is that we used NetWare and it had a primitive messaging system built in. When I clicked the button, a small VBA script would send out a NetWare message to my friends, telling them that the Cherry Bomb was at it again and would they please call me. He never figured it out :)


A few years ago I wrote a winforms app with the help of a few win32 api's to completely lock myself out of my computer for an hour so that it would force me to go and exercise. Because I was lazy? No... because I had a personal fitness goal. Sometimes you just need a little kick to get started :)


I wrote a simple VB app that tracked which game numbers of Freecell I had played and successfully completed, and always launched it with a different seed.

....starting from 1....

Max game number is 65k. Rather sadly after more than 5 years I am still in only the hundreds. But at least I know I've never played the same hand twice!

** Postscript - it's the only VB app I've ever written. I ran screaming back to C++....


I wrote a cron job to grab the ip address of my dads router and ftp it to a secure location so when he needed help I could remote desktop in and fix his comp.


Called assignIisSite_ToAppPool.js

Really useful when you want to make sure that some resources are properly mapped.

:)

SetAppPool("W3SVC/1059997624/Root", "MyAppPool");



function SetAppPool(webId, appPoolName)
{
var providerObj=GetObject("winmgmts:/root/MicrosoftIISv2");
var vdirObj=providerObj.get("IIsWebVirtualDirSetting='" + webId + "'");
vdirObj.AppPoolId=appPoolName;
vdirObj.Put_();
}

As a scheduled task, to copy any modified/new files from entire drive d: to backup drive g:, and to log the files copied. It helps me keep track of what I did when, as well.

justdate is a small program to prints the date and time to the screen

g:

cd \drive_d

d:

cd \

type g:\backup_d.log >> g:\logs\backup_d.log

echo ========================================== > g:\backup_d.log

d:\mu\bmutil\justdate >> g:\backup_d.log

xcopy /s /d /y /c . g:\drive_d >> g:\backup_d.log


A perl script that scrapes my local Craigslist, by selected categories, in to a SQL DB which I can then query against.

V2 of this updates the DB with a timer and alerts me if I have a match on any of the queries, basically providing me with a background agent for CL.


A Rakefile in my downloads directory containing tasks that copy files from said directory to their respective media archives on external drives. Given my internet speed and storage capacity, it would take me hours out of every week to just copy across and re-name appropriately every piece of media that is downloaded (completely legally, I might add) by hellanzb.

Another very useful task in the same file logs into and scrapes IMDB for episode lists / discographies of all the media I have, and then checks NewzBin for reports that would fill any holes I have.

Combined, this means I have to do absolutely nothing, and in exchange, I wake up every morning with more media than I could possibly consume in that day sitting on my external hard drives.

Did I mention that this is all entirely above-board and legal? d-:

I'll probably merge this all into a sort of command-line media manager/player (farming things out to mplayer), and publish it on GitHub when I have the time.


I use this as an autoloaded function. I can just type "mycd" and a list of directories appears which I frequently cd to. If I happen to know then number I can just say something like "mycd 2". To add a directory to the list you just type "mycd /tmp/foo/somedirectory".

function mycd {

MYCD=/tmp/mycd.txt
touch ${MYCD}

typeset -i x
typeset -i ITEM_NO
typeset -i i
x=0

if [[ -n "${1}" ]]; then
   if [[ -d "${1}" ]]; then
      print "${1}" >> ${MYCD}
      sort -u ${MYCD} > ${MYCD}.tmp
      mv ${MYCD}.tmp ${MYCD}
      FOLDER=${1}
   else
      i=${1}
      FOLDER=$(sed -n "${i}p" ${MYCD})
   fi
fi

if [[ -z "${1}" ]]; then
   print ""
   cat ${MYCD} | while read f; do
      x=$(expr ${x} + 1)
      print "${x}. ${f}"
   done
   print "\nSelect #"
   read ITEM_NO
   FOLDER=$(sed -n "${ITEM_NO}p" ${MYCD})
fi

if [[ -d "${FOLDER}" ]]; then
   cd ${FOLDER}
fi

}

"backup.sh" that tars up the contents of a directory and sends it to my gmail account.


A Quick and Dirty Python script that looked up the DNS for google.com every 5 seconds and beeped once if it succeeded and twice if it failed.

I wrote this during a time when I had to live with a highly flaky home network. It allowed me to instantly know the state of the network even while I was head first under the desk across the room with both hands full of network cable and a flashlight in my mouth.


I like how git figures out when to use less, subversion doesn't have that feature so I want to easily get colored output in a pager. the cgrep alias lets me choose quickly. without it there are times I get raw color output.

I also, when grepping through code, don't like to see certain results, like .svn ctags binary files

grep -R sourcecodetext sourcedir | nosvn

Below is what I have in my config files

cat .bash_profile

alias nosvn="grep -v \"\.svn\|tags\|cscope\|Binary\""
alias less="less -R"
alias diff="colordiff -u"
alias cgrep="grep --color=always"

export GREP_OPTIONS='--color=auto'

cat bin/gitdiffwrapper

#!/bin/bash

old_file=$1
tmp_file=$2
old_hex=$3
old_mode=$4
new_file=$5
new_mode=$6

colordiff -u $old_file $tmp_file

cat .gitconfig

[diff]
    external = $HOME/bin/gitdiffwrapper

cat .subversion_config | grep ^diff-cmd

diff-cmd = /usr/bin/colordiff

Sometimes I forget what are the most recent files I just created in a directory, but a ls command will just show every file in the directory, I just want a few most recent files so I put this in my .cshrc

 ls -l -t | awk 'NR<15{print $0}'

(Actually it is in a file called lt and in the .cshrc it is set with: alias lt '~/lt')

So now lt will show me only a few files.


alias dir='ls -al' is my preferred favorite script.


A threaded HTML scraper to download all available subtitles for series/movies from a site which is a pain to use (you have to click like 4 times after a search to get to the download page, just to display more ads). Now I just put the search criteria and press download.


I had a version control script that would take a directory as an argument, and recursively copy all files to ../dirname/DATE/TIME/

Obviously it was a crappy way to do things, but it was handy before installing a real version control package.


A few years ago I wrote a winforms app with the help of a few win32 api's to completely lock myself out of my computer for an hour so that it would force me to go and exercise. Because I was lazy? No... because I had a personal fitness goal. Sometimes you just need a little kick to get started :)


I have a batch file which establishes a VPN connection and then enters an infinite loop, pinging a machine on the other side of the connection every five minutes so that the VPN server doesn't drop the connection due to inactivity if I don't generate any traffic over that connection for a while.


A Greasemonkey script which removes obviously stupid[*] comments from gaming site Kotaku.com.

[*] As identified by common spelling mistakes, all-caps writing, excessive use of "LOL" and similar heuristics.


A little script that monitors some popular websites for ads that match my skills and email me an email.


Various Shortcuts to "net start" and "net stop" commands so I can start and stop services without having to go into the Services MMC


I use procmail to sort my incoming email to different folders. Because I have trouble remembering the procmailrc syntax, I use m4 as a preprocessor. Here's how my procmailrc begins (this isn't the script yet):

divert(-1)
changequote(<<, >>)
define(mailinglistrule, 
<<:0:
* $2
Lists/$1
>>)
define(listdt, <<mailinglistrule($1,^Delivered-To:.*$2)>>)
define(listid, <<mailinglistrule($1,^List-Id:.*<$2>)>>)
divert# Generated from .procmailrc.m4 -- DO NOT EDIT

This defines two macros for mailing lists, so e.g. listdt(foo, [email protected]) expands to

:0:
* ^Delivered-To:.*[email protected]
Lists/foo

meaning that emails with a Delivered-To header containing [email protected] should be put in the Lists/foo folder. It also arranges the processed file to begin with a comment that warns me not to edit that file directly.

Now, frankly, m4 scares me: what if I accidentally redefine a macro and procmail starts discarding all my email, or something like that? That's why I have a script, which I call update-procmailrc, that shows me in diff format how my procmailrc is going to change. If the change is just a few lines and looks roughly like what I intended, I can happily approve it, but if there are huge changes to the file, I know to look at my edits more carefully.

#! /bin/sh

PROCMAILRC=.procmailrc
TMPNAM=.procmailrc.tmp.$$
cd $HOME
umask 077
trap "rm -f $TMPNAM" 0

m4 < .procmailrc.m4 > $TMPNAM
diff -u $PROCMAILRC $TMPNAM

echo -n 'Is this acceptable? (y/N) '
read accept

if [ -z "$accept" ]; then
    accept=n
fi

if [ $accept = 'y' -o $accept = 'Y' ]; then
    mv -f $TMPNAM $PROCMAILRC && \
    chmod 400 $PROCMAILRC && \
    echo "Created new $PROCMAILRC"
    if [ "$?" -ne 0 ]; then
        echo "*** FAILED creating $PROCMAILRC"
    fi
else
    echo "Didn't update $PROCMAILRC"
fi

The script hasn't yet prevented any email disasters, but it has made me less anxious about changing my procmailrc.


Not every day, but I did use XSLT script to create my wedding invitations (a Pages file for the inserts to the invite cards, and an HTML file for the address labels).


An alert box, on a random timer, guaranteed to pop-up at least once an hour to remind me to do some pushups.

I used it when I was in the military.

I also wrote architecture rules (http://architecturerules.org) for me and anyone else.


As a scheduled task, to copy any modified/new files from entire drive d: to backup drive g:, and to log the files copied. It helps me keep track of what I did when, as well.

justdate is a small program to prints the date and time to the screen

g:

cd \drive_d

d:

cd \

type g:\backup_d.log >> g:\logs\backup_d.log

echo ========================================== > g:\backup_d.log

d:\mu\bmutil\justdate >> g:\backup_d.log

xcopy /s /d /y /c . g:\drive_d >> g:\backup_d.log


I suppose this depends on how you define useful, but my favorite little script is a variant on the *nix fortune program. See below, and you'll get the idea of what it does:

telemachus ~ $ haiku 

   January--
in other provinces,
   plums blooming.
    Issa

It doesn't really get anything done, but a nice haiku goes a long way. (I like how the colorizer decided to interpret the poem.) (Edit: If I really have to be useful, I'd say a script that allows a user to enter a US zipcode and get current weather and 0-3 days of forecast from Google.)


#! /bin/bash
# check to see if site is up
#   if it is, don't worry
#   if it's down, restart apache after get a process listing
#
# v.1 Warren M Myers - initial stab
#     31 Aug 06
#

ERRCOD='7'
WHEN=`date +%d%b%y`
REPT="~/psaux.$WHEN.txt"
STARS='********************'

curl -I http://www.shodor.org > /var/tmp/curlret.txt

if [ "$?" = "$ERRCOD" ]; then
    # return was unable to connect to host: save ps -aux; mail report
    ps -aux > $REPT
    echo $STARS
    echo 'curl return results'
    echo
    cat curlret.txt
    echo
    echo $STARS
    echo 'ps -aux results'
    cat $REPT
    echo
    echo $STARS
    echo 'restarting apache'
    /etc/init.d/apache2 restart
    echo 'apache restarted'
    echo
    echo "ps -aux results saved in $REPT"
fi

rm -f /var/tmp/curlret.txt

A bash script called up so that if I'm in /a/very/deeply/nested/path/somewhere and I want to go "up" N directories, I can type up N:

#!/bin/bash
LIMIT=$1
P=$PWD
for ((i=1; i <= LIMIT; i++))
do
    P=$P/..
done
cd $P

For example:

/a/very/deeply/nested/path/somewhere> up 4
/a/very> 

NB by gmatt:

Working off the great work above, it can be extended to a back function by placing the following into your bashrc:

function up( )
{
LIMIT=$1
P=$PWD
for ((i=1; i <= LIMIT; i++))
do
    P=$P/..
done
cd $P
export MPWD=$P
}

function back( )
{
LIMIT=$1
P=$MPWD
for ((i=1; i <= LIMIT; i++))
do
    P=${P%/..}
done
cd $P
export MPWD=$P
}

I like how git figures out when to use less, subversion doesn't have that feature so I want to easily get colored output in a pager. the cgrep alias lets me choose quickly. without it there are times I get raw color output.

I also, when grepping through code, don't like to see certain results, like .svn ctags binary files

grep -R sourcecodetext sourcedir | nosvn

Below is what I have in my config files

cat .bash_profile

alias nosvn="grep -v \"\.svn\|tags\|cscope\|Binary\""
alias less="less -R"
alias diff="colordiff -u"
alias cgrep="grep --color=always"

export GREP_OPTIONS='--color=auto'

cat bin/gitdiffwrapper

#!/bin/bash

old_file=$1
tmp_file=$2
old_hex=$3
old_mode=$4
new_file=$5
new_mode=$6

colordiff -u $old_file $tmp_file

cat .gitconfig

[diff]
    external = $HOME/bin/gitdiffwrapper

cat .subversion_config | grep ^diff-cmd

diff-cmd = /usr/bin/colordiff

I wrote some lines of code to automatically tweak all things powertop suggests when I unplug my laptop and undo that if I plug the laptop back in. Maximum power, maximum efficiency, maximum convenience.


I have a batch file which runs every morning, which launches a browser with the tabs loaded to all the sites I want to check each day (Woot, Dilbert, Doonesbury, UserFriendly; seasonally, NY Mets scores and electoral-vote.com, plus a few websites that need to be visited regularly to keep membership active)


#!/usr/bin/perl
use strict;
use utf8;
use Encode;
use File::Find;
binmode STDOUT, ':utf8';
sub orderly {
    my ($x, $y) = @_{$a, $b};
    if (my $z = $x <=> $y) {return $z}
    $x = length $a;
    $y = length $b;
    my $z = $x < $y ? $x : $y;
    if (substr($a, 0, $z) eq substr($b, 0, $z)) {
        return $y <=> $x;
    }
    else {
        return $a cmp $b;
    }
}
my %conf = map +($_ => 0), split //, 'acsxL';
sub Stat {$conf{L} ? lstat : stat}
my @dirs = ();
while (defined ($_ = shift)) {
    if ($_ eq "--") {push @dirs, @ARGV; last}
    elsif (/^-(.*)$/s) {
        for (split //, $1) {
            if (!exists $conf{$_} or $conf{$_} = 1 and $conf{a} and $conf{s}) {
                print STDERR "$0 [-a] [-c] [-s] [-x] [-L] [--] ...\n";
                exit 1;
            }
        }
    }
    else {push @dirs, $_}
}
s/\/*$//s for @dirs;  # */ SO has crappy syntax highlighting
@dirs = qw(.) unless @dirs;
my %spec = (follow => $conf{L}, no_chdir => 1);
if ($conf{a}) {
    $spec{wanted} = sub {
        Stat;
        my $s = -f _ ? -s _ : 0;
        decode(utf8 => $File::Find::name) =~ /^\Q$dirs[0]\E\/?(.*)$/s;
        my @a = split /\//, $1;
        for (unshift @a, $dirs[0]; @a; pop @a) {
            $_{join "/", @a} += $s;
        }
    };
}
elsif ($conf{s}) {
    $spec{wanted} = sub {
        Stat;
        $_{$dirs[0]} += -f _ ? -s _ : 0;
    };
}
else {
    $spec{wanted} = sub {
        Stat;
        my $s = -f _ ? -s _ : 0;
        decode(utf8 => $File::Find::name) =~ /^\Q$dirs[0]\E\/?(.*)$/s;
        my @a = split /\//, $1;
        ! -d _ and pop @a;
        for (unshift @a, $dirs[0]; @a; pop @a) {
            $_{join "/", @a} += $s;
        }
    };
}
if ($conf{x}) {
    $spec{preprocess} = sub {
        my $dev = (Stat $File::Find::dir)[0];
        grep {$dev == (Stat "$File::Find::dir/$_")[0]} @_;
    };
}
while (@dirs) {
    find(\%spec, $dirs[0] eq "" ? "/" : $dirs[0]);
    $_{""} += $_{$dirs[0]} if $conf{c};
    shift @dirs;
}
$_{$_} < 1024 ** 1 ? printf "%s «%-6.6sB» %s\n", $_{$_}, sprintf("%6.6f", "$_{$_}" / 1024 ** 0), $_ :
$_{$_} < 1024 ** 2 ? printf "%s «%-6.6sK» %s\n", $_{$_}, sprintf("%6.6f", "$_{$_}" / 1024 ** 1), $_ :
$_{$_} < 1024 ** 3 ? printf "%s «%-6.6sM» %s\n", $_{$_}, sprintf("%6.6f", "$_{$_}" / 1024 ** 2), $_ :
$_{$_} < 1024 ** 4 ? printf "%s «%-6.6sG» %s\n", $_{$_}, sprintf("%6.6f", "$_{$_}" / 1024 ** 3), $_ :
$_{$_} < 1024 ** 5 ? printf "%s «%-6.6sT» %s\n", $_{$_}, sprintf("%6.6f", "$_{$_}" / 1024 ** 4), $_ :
$_{$_} < 1024 ** 6 ? printf "%s «%-6.6sP» %s\n", $_{$_}, sprintf("%6.6f", "$_{$_}" / 1024 ** 5), $_ :
$_{$_} < 1024 ** 7 ? printf "%s «%-6.6sE» %s\n", $_{$_}, sprintf("%6.6f", "$_{$_}" / 1024 ** 6), $_ :
$_{$_} < 1024 ** 8 ? printf "%s «%-6.6sZ» %s\n", $_{$_}, sprintf("%6.6f", "$_{$_}" / 1024 ** 7), $_ :
                     printf "%s «%-6.6sY» %s\n", $_{$_}, sprintf("%6.6f", "$_{$_}" / 1024 ** 8), $_
    for grep {$_{$_} > 0} sort orderly keys %_;

I save it in ~/bin/dush, it acts as a sort of du -h/du | sort -n hybrid: sorts and gives human-readable sizes all at once. Very useful for finding what's taking up disk space.

In a similar vein,

#!/usr/bin/perl
$t = 1;
%p = map {$_ => ($t *= 1024)} qw(K M G T P E Z Y);
$t = 4707319808;
if (@ARGV) {
    if (($_ = shift) =~ /^-*dvd/i) {$t = 4707319808}
    elsif (/^-*cd[^w]*$/i) {$t = 737280000}
    elsif (/^-*cd/i) {$t = 681984000}
    elsif (/^-*([\d.]+)([kmgtpezy])/i) {$t = $1 * ($p{"\U$2"} || 1)}
    elsif (/^-*([\d.]+)/) {$t = $1}
    else {unshift @ARGV, $_}
}
($q, $r, $s) = (0, ($ENV{COLUMNS} || 80) - 13, $t);
while (<>) {
    chomp, stat;
    unless (-e _) {
        print STDERR "$_ does not exist\n";
        next;
    }
    if (($s += -s _) > $t) {
        $s && $s < $t && printf "-%7s %s\n",
            sprintf("%2.3f%%", 100 * ($t - $s) / $t), $t - $s;
        printf "-----------%d%*s\n", ++$q, $r, "-" x $r;
        $s = -s _;
    }
    printf "%8s %s\n",
        sprintf("%3.3f%%", $s * 100 / $t),
        /.{4}(.{$r})$/s ? "...$1" : $_;
}
$s && $s < $t && printf "-%7s %s\n",
    sprintf("%2.3f%%", 100 * ($t - $s) / $t), $t - $s;

I save this as ~/bin/fit. When I'm archiving a bunch of files, I run ls | fit or ls | fit -cdrw to help determine if it'll fit on a DVD/CD/CDRW, and where to split them if they don't.


alias snoot='find . ! -path "*/.svn*" -print0 | xargs -0 egrep '

A Quick and Dirty Python script that looked up the DNS for google.com every 5 seconds and beeped once if it succeeded and twice if it failed.

I wrote this during a time when I had to live with a highly flaky home network. It allowed me to instantly know the state of the network even while I was head first under the desk across the room with both hands full of network cable and a flashlight in my mouth.


I have a batch file which runs every morning, which launches a browser with the tabs loaded to all the sites I want to check each day (Woot, Dilbert, Doonesbury, UserFriendly; seasonally, NY Mets scores and electoral-vote.com, plus a few websites that need to be visited regularly to keep membership active)


A small application that left click (or double-click) every "X" ms for "Y" amount of time. No more need for that drinking bird to work at the nuclear power plant! ;)


A Greasemonkey script to add a "press that button a lot" control box to an online game.


A script that reads a config file in the current dir, logs into an FTP account, and uploads all files that have changed since the last time it was run. Really handy for clients who use shared hosting, and FTP is my only option for file access.

http://lucasoman.com/code/updater


Anime CRC32 checksum:

#!/usr/bin/python                                                                                                                                                                                  

import sys, re, zlib

c_null="^[[00;00m"
c_red="^[[31;01m"
c_green="^[[32;01m"

def crc_checksum(filename):
    filedata = open(filename, "rb").read()
    sum = zlib.crc32(filedata)
    if sum < 0:
        sum &= 16**8-1
    return "%.8X" %(sum)

for file in sys.argv[1:]:
    sum = crc_checksum(file)
    try:
        dest_sum = re.split('[\[\]]', file)[-2]
        if sum == dest_sum:
            c_in = c_green
        else:
            c_in = c_red
        sfile = file.split(dest_sum)
        print "%s%s%s   %s%s%s%s%s" % (c_in, sum, c_null, sfile[0], c_in, dest_sum, c_null, sfile[1])
    except IndexError:
        print "%s   %s" %(sum, file)

A similar backup.sh for each project, that tars and gzips just the source, moves it into a snapshot directory and labels it with timestamp: project-mmddyy-hhmmss. Useful for coding between commits.


I often use a MS Word macro that takes a source-code file, formats it in two columns of monospaced type on a landscape page, numbers the lines, and adds company header and footer info such as filename, print date, page number, and confidentiality statement.

Printing both sides of the page uses about 1/4 the paper as the equivalent lpr command. (Does anyone use lpr anymore???)


I wrote a python program to calculate my apartment's shared spending and output a neat little grid, with roommate as the columns and expense category as the row, along with how much money each roommate owed for rent, after adjusting for his contribution toward shared expenses. We'd been sharing this way for a while, but just adding up raw totals at the end of the month. I needed more granular control. With a maximum of eight keystrokes per line-item, this is way better than excel. I was sort of on a desperate quest to stop the monthly trend of one roommate spending 25% of our budget on beverages...


I have a batch file which establishes a VPN connection and then enters an infinite loop, pinging a machine on the other side of the connection every five minutes so that the VPN server doesn't drop the connection due to inactivity if I don't generate any traffic over that connection for a while.


A script that runs hourly to retrain my spam filters based two IMAP folder where span and ham are put.

#!/bin/sh
FNDIR="train-as-spam"
FPDIR="train-as-ham"

for dir in /home/*/.maildir
do
    cd "${dir}"
    USER=`stat -c %U .`

    SRCDIR="${dir}/.${FNDIR}"
    if [ ! -d ${SRCDIR} ]; then
        echo no "${SRCDIR}" directory
    else
        cd "${SRCDIR}/cur"
        ls -tr | while read file
        do
            if grep -q "^X-DSPAM" "${file}"; then
                SOURCE="error"
            else
                SOURCE="corpus"
            fi

            dspam --user "${USER}" --class=spam --source="${SOURCE}" --deliver=innocent,spam --stdout < "${file}" > "../tmp/${file}"
            mv "../tmp/${file}" "${dir}/new/${file%%:*}" && rm "${file}"
        done
    fi

    SRCDIR="${dir}/.${FPDIR}"
    if [ ! -d ${SRCDIR} ]; then
        echo no "${SRCDIR}" directory
    else
        cd "${SRCDIR}/cur"
        ls -tr | while read file
        do
            if grep -q "^X-DSPAM" "${file}"; then
                SOURCE="error"
            else
                SOURCE="corpus"
            fi

            dspam --user "${USER}" --class=innocent --source="${SOURCE}" --deliver=innocent,spam --stdout < "${file}" > "../tmp/${file}"
            mv "../tmp/${file}" "${dir}/new/${file%%:*}" && rm "${file}"
        done
    fi

done

I have a Python script that automatically runs when I plug my digital camera in.

It copies all of the pictures off the card on the camera, backs them up, and then uploads them to Flickr.


The upload-to-Flickr piece comes from uploadr.py (which I can't take credit for).

Here's the Python code for unloading the camera. It recurses through SRCDIR and names each image with the date & time before copying the images to DESTDIR.

#!/usr/bin/python

import os
import string
import time
import shutil

###################################################
__SRCDIR__ = "/mnt/camera"
__DESTDIR__ = "/home/pictures/recent"
###################################################
def cbwalk(arg, dirname, names):
    sdatetime = time.strftime("%y%m%d%H%M")
    for name in names:
        if string.lower(name[-3:]) in ("jpg", "mov"):
            srcfile = "%s/%s" % (dirname, name)
            destfile = "%s/%s_%s" % (__DESTDIR__, sdatetime, name)
                    print destfile
            shutil.copyfile( srcfile, destfile)
###################################################
if __name__ == "__main__":
    os.path.walk(__SRCDIR__, cbwalk, None)

I have a batch file which runs every morning, which launches a browser with the tabs loaded to all the sites I want to check each day (Woot, Dilbert, Doonesbury, UserFriendly; seasonally, NY Mets scores and electoral-vote.com, plus a few websites that need to be visited regularly to keep membership active)


#! /bin/bash
# check to see if site is up
#   if it is, don't worry
#   if it's down, restart apache after get a process listing
#
# v.1 Warren M Myers - initial stab
#     31 Aug 06
#

ERRCOD='7'
WHEN=`date +%d%b%y`
REPT="~/psaux.$WHEN.txt"
STARS='********************'

curl -I http://www.shodor.org > /var/tmp/curlret.txt

if [ "$?" = "$ERRCOD" ]; then
    # return was unable to connect to host: save ps -aux; mail report
    ps -aux > $REPT
    echo $STARS
    echo 'curl return results'
    echo
    cat curlret.txt
    echo
    echo $STARS
    echo 'ps -aux results'
    cat $REPT
    echo
    echo $STARS
    echo 'restarting apache'
    /etc/init.d/apache2 restart
    echo 'apache restarted'
    echo
    echo "ps -aux results saved in $REPT"
fi

rm -f /var/tmp/curlret.txt

I have a Python script that automatically runs when I plug my digital camera in.

It copies all of the pictures off the card on the camera, backs them up, and then uploads them to Flickr.


The upload-to-Flickr piece comes from uploadr.py (which I can't take credit for).

Here's the Python code for unloading the camera. It recurses through SRCDIR and names each image with the date & time before copying the images to DESTDIR.

#!/usr/bin/python

import os
import string
import time
import shutil

###################################################
__SRCDIR__ = "/mnt/camera"
__DESTDIR__ = "/home/pictures/recent"
###################################################
def cbwalk(arg, dirname, names):
    sdatetime = time.strftime("%y%m%d%H%M")
    for name in names:
        if string.lower(name[-3:]) in ("jpg", "mov"):
            srcfile = "%s/%s" % (dirname, name)
            destfile = "%s/%s_%s" % (__DESTDIR__, sdatetime, name)
                    print destfile
            shutil.copyfile( srcfile, destfile)
###################################################
if __name__ == "__main__":
    os.path.walk(__SRCDIR__, cbwalk, None)

The most useful? But there are so many...

  1. d.cmd contains: @dir /ad /on
  2. dd.cmd contains: @dir /a-d /on
  3. x.cmd contains: @exit
  4. s.cmd contains: @start .
  5. sx.cmd contains: @start . & exit
  6. ts.cmd contains the following, which allows me to properly connect to another machine's console session over RDP regardless of whether I'm on Vista SP1 or not.

    @echo off

    ver | find "6.0.6001"

    if ERRORLEVEL 0 if not errorlevel 1 (set TSCONS=admin) ELSE set TSCONS=console

    echo Issuing command: mstsc /%TSCONS% /v %1

    start mstsc /%TSCONS% /v %1

(Sorry for the weird formatting, apparently you can't have more than one code sample per answer?)

From a command prompt I'll navigate to where my VS solution file is, and then I'll want to open it, but I'm too lazy to type blah.sln and press enter. So I wrote sln.cmd:

@echo off
if not exist *.sln goto csproj
for %%f in (*.sln) do start /max %%f
goto end

:csproj
for %%f in (*.csproj) do start /max %%f
goto end

:end

So I just type sln and press enter and it opens the solution file, if any, in the current directory. I wrap things like pushd and popd in pd.cmd and pop.cmd.


I wrote a file extraction tool to be used in Linux, that can extract about 20 different file formats and uses the file content, not the file name.

This tool got quite popular, I have a regular stream of people who download it from my blog. Get it here:


I wrote a simple VB app that tracked which game numbers of Freecell I had played and successfully completed, and always launched it with a different seed.

....starting from 1....

Max game number is 65k. Rather sadly after more than 5 years I am still in only the hundreds. But at least I know I've never played the same hand twice!

** Postscript - it's the only VB app I've ever written. I ran screaming back to C++....


I have a batch file which establishes a VPN connection and then enters an infinite loop, pinging a machine on the other side of the connection every five minutes so that the VPN server doesn't drop the connection due to inactivity if I don't generate any traffic over that connection for a while.


I've written a small shell script, tapt, for Debian based system. esp. Ubuntu. What it basically does is to post all your "apt-get" activities to your twitter account. It helps me to keep the track of what and when I've installed/remove programs in my Ubuntu system. I created a new Twitter account just for this and kept it private. Really useful. More information here: http://www.quicktweaks.com/tapt/


A little script that monitors some popular websites for ads that match my skills and email me an email.