133

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.

Ben Brocka
  • 2,006
  • 4
  • 34
  • 53
Kyle Walsh
  • 2,774
  • 4
  • 27
  • 25
  • I've seen some really cool work related ones, but not as many "everyday life" related scripts. Then again, if something at work is ruining your life and you fixed it with a neat script then who am I to judge? – Kyle Walsh Oct 09 '08 at 18:30
  • 11
    One of those gem questions that makes stack overflow worth while. Any guideline that suggests it be closed shouldn't be there, any mod who voted to close it shouldn't be one. – Bill K Nov 22 '16 at 07:28
  • This is one one for me: https://github.com/codeforester/base. This framework has increased my productivity as an SRE/DevOps person by leaps and bounds, while increasing team cohesiveness and collaboration. – codeforester Jun 07 '20 at 15:57

78 Answers78

117

My o key fell off on my laptop; so I wrote a program that replaces two 0 keystrokes within 200 MS of each other as an o, two 0 keystrokes within 700 MS of each other as a 0 and ignore the rest; so I could use my laptop before I get around to replacing the keyboard.

Wow; I didn't know this would be so popular :p

As for how - Microsoft exposes a nice little API feature called Hooks.

Using that hook; I was able to write a "filter" that did what I needed it to do (hint: if you return 1 with your callback windows will not process the keystroke).

The reason I know about this actually is not because I was writing a keylogger - but because I wrote a program smiler to Synergy a while ago.

And yes. I did write another program that swapped alpha-numeric keys with a random alpha-numeric key and yes; it was really funny :D

Carolus
  • 477
  • 4
  • 16
  • 1
    I might have thought of just typing on the exposed key-rest, but that's certainly an interesting way around it :) – warren Oct 09 '08 at 17:02
  • I think this answer is pretty cool. Not that the others aren't, but this one is just...different. :) – Kyle Walsh Oct 09 '08 at 17:09
  • 2
    I wnder why I didn't think f that... I'm having the same prblem... :-( – asterite Oct 09 '08 at 17:30
  • The enter key on my laptop fell off (due to my video game rage,) and there's no numpad Enter key. Can you help me? – Haoest Oct 09 '08 at 17:39
  • @haoest: see those USB ports? Seen any USB keyboards? – Adriano Varoli Piazza Oct 09 '08 at 17:57
  • 1
    This reminds me of this XKCD comic: http://xkcd.com/196/ – JesperE Oct 09 '08 at 18:17
  • 59
    HA! Y00u will n00t be able t00 type 00n a standard keyb00ard again with00ut retraining y00ur fingers! G0000d Luck! – Doug L. Oct 29 '08 at 04:00
  • yeah it dose take some adjusting. Man; I need a new laptop now. I figure I might just as well get a new one seeing as this is a three year piece of junk. Some remoting software (logmein) does not work with my app. So I have to copy+paste o's when I need them if I'm logged into a friends computer :p –  Oct 29 '08 at 05:19
  • 107
    The big question is how did you manage to write that program without using the letter 'o'? – e.James Jan 19 '09 at 07:38
  • 1
    eJames: Probably by using asc(111) or similar. – Peter Boughton Mar 17 '09 at 21:04
114

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 :)

baudtack
  • 29,062
  • 9
  • 53
  • 61
Ovid
  • 11,580
  • 9
  • 46
  • 76
94

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
}
ldog
  • 11,707
  • 10
  • 54
  • 70
foxdonut
  • 7,779
  • 7
  • 34
  • 33
  • 4
    +1 Absolutely *f*'ing brilliant! I will be using this a *lot*! – wzzrd Jan 30 '09 at 22:00
  • replace with this one-liner: p=$(printf "%${1}s" " "|sed 's/ /..\//g'); cd $p; – gnud Feb 26 '09 at 23:56
  • 12
    Why would you replace easy to read code with difficult to read code? Does your computer run more quickly if you have less less lines of source code? – Beska Mar 17 '09 at 21:00
  • I am on Mac and I cannot get the code working. Are you 100% sure that there should not be some quotes somewhere? – Léo Léopold Hertz 준영 Mar 24 '09 at 15:32
  • for mac see this: http://stackoverflow.com/questions/1328750/shell-script-that-goes-up-n-folders-in-the-file-system – André Hoffmann Aug 25 '09 at 15:30
  • @Masi: yes. Many, many quotes. "$1", "$P", "$PWD" all the way through and you will get on much better... – ijw Sep 02 '09 at 12:26
  • 3
    gnud: replace my clean and clear code with garbage? No thanks. – foxdonut Nov 14 '09 at 13:32
  • 3
    brilliant! I extended this to include a `back` method, so whenever you use up you can use back to easily move back and forth. To use it put this in your bashrc (sorry about shit formatting): 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 } – ldog Jun 17 '10 at 23:55
  • Great idea, but a minor point: wouldn't it be better located in .bash_profile since it's only applicable to interactive shells? – Lyle Jun 18 '10 at 15:59
  • hmm I won't lie to you my scripting ability is dubious at best, if .bash_profile is more appropriate then go for it. Should I change that in the note I wrote above? – ldog Jun 18 '10 at 16:47
  • You could change it. Generally the difference is that .bash_profile is read once when you log in, while .bashrc is read every time you start a shell (which includes shell scripts which have no use for interactive aliases and functions). Most peoples' .bash_profile source their .bashrc as well, as noted here: http://stackoverflow.com/questions/415403/whats-the-difference-between-bashrc-bash-profile-and-environment – Lyle Jun 22 '10 at 02:47
  • 2
    am i crazy? how the hell does this work? cd in a script will cd there but when the script exists, you're back in the same/original directory! does my bash work different than all of yours? – johnnyB Dec 03 '13 at 18:06
  • 2
    @johnnyB I was surprised Fred didn't mention it in his script. When script is invoked, it creates subshell by default and change of directory happens in subshell so you do not need to effect in shell itself since it exits at the end. You need to use ``source`` command to make it work like expected: ``source up.sh 4`` or ``. up.sh 4``. I have added following to ~/.bash_aliases for easy usage ``alias up='. up.sh $1'``. After you re-start shell you can just type ``up 4`` and it will work as expected. – Rafal Feb 07 '14 at 08:03
  • The version I use allows for an empty argument list to go back 1 level, and I have it set to `..`. alias ..='cdparentdirs' function cdparentdirs(){ path="../" for ((i=0; i < $[$1-1]; i++)) do path="$path../" done cd $path } – Floegipoky Sep 23 '14 at 18:18
  • in ubuntu with zsh you can do `$ cd ....` and it will go up 3 lvls :) – Shining Love Star Apr 16 '17 at 17:49
  • On Mac, when you want to go back, you need to specify the same parameter that you passed to `up N`. To make the `back` parameterless append `export MLIMIT=$LIMIT` to `up` and replace `LIMIT=$1` with `LIMIT=$MLIMIT` in `back`. `` – user3579815 Aug 14 '19 at 23:33
  • I still use this a lot, and I also use: `upto() { cd ${PWD%$1/*}$1; }` with which you can go from `/a/very/deeply/nested/path/somewhere` to `/a/very` by typing `upto very` – foxdonut Aug 16 '19 at 01:40
67

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.

Shahbaz
  • 46,337
  • 19
  • 116
  • 182
Martin Beckett
  • 94,801
  • 28
  • 188
  • 263
63

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"
Liam
  • 19,819
  • 24
  • 83
  • 123
38

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)
Motti
  • 110,860
  • 49
  • 189
  • 262
Mark Biek
  • 146,731
  • 54
  • 156
  • 201
  • Share the code, and I'll share an up vote :) – Even Mien Oct 10 '08 at 05:37
  • 41
    This will work great until the day you forget those special pics you took the night before... – Ed Guiness Nov 07 '08 at 12:23
  • The full thing is actually a two-step process. The first step dumps things off the camera & renames them. The second step has a prompt asking if you want to upload to Flickr. That's more because uploading kills my bandwidth than because I'm worried about what I'm uploading. – Mark Biek Dec 30 '08 at 01:48
  • 5
    How do you trigger the 'run on plug in' bit? – ijw Sep 02 '09 at 12:27
  • @ijw For Unix, see here: http://unix.stackexchange.com/questions/28548/how-to-run-custom-scripts-upon-usb-device-plug-in - windows: http://superuser.com/questions/219401/starting-scheduled-task-by-detecting-connection-of-usb-drive – Chris May 14 '13 at 03:57
32

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 :)

16

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. ;)

Onion-Knight
  • 3,477
  • 7
  • 31
  • 34
  • hmmm. If these are ad supported comics I would worry that you are not "paying" by taking the risk of seeing the ads :-) – Matthew Scouten May 13 '10 at 19:27
  • 2
    @Matthew - I see your point, but I don't see why not looking at an advertisement is a moral/ethical issue. If that is the case, the authors of No-Script and pop-up blockers have a lot of explaining to do. ;) – Onion-Knight May 13 '10 at 21:34
  • 1
    I don't have any problem with pop-up blockers, because popups are an abusive way of displaying ads. It's not exactly a moral issue, more of a fairness issue. I use an adblocker on most sites, but turn it off for certain sites that I visit frequently and wish to support. Fetching the comics with a python script (and never seeing the site at all) feels over the line to me, but I could not tell you exactly where the line is. – Matthew Scouten May 19 '10 at 15:24
14

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

Chris Noe
  • 36,411
  • 22
  • 71
  • 92
  • 2
    Some nice .cmd file tricks there. I knew you could use '^' to escape redirection and pipes, but I didn't know you could also use it as for line continuation. Cool :) – Patrick Cuff Oct 09 '08 at 17:57
  • The world needs less .bat scripts, not more! :-) – JesperE Oct 09 '08 at 18:07
  • 2
    And the corollary is that the world needs less Windows. Until then, this is the least common denominator. I share this script because zillions of people can use it, as is. – Chris Noe Oct 09 '08 at 21:07
14

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

JayG
  • 4,339
  • 3
  • 23
  • 19
14

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? ;)

Vivin Paliath
  • 94,126
  • 40
  • 223
  • 295
13

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?"

Kristopher Johnson
  • 81,409
  • 55
  • 245
  • 302
12

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.

LKM
  • 4,351
  • 3
  • 27
  • 22
  • 6
    Cool. I feel like extracting the regexp's, and generate random statements consisting only of expressions matching one or more of them. – gnud Feb 26 '09 at 23:41
11

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

Paul Nathan
  • 39,638
  • 28
  • 112
  • 212
11

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.

Vinko Vrsalovic
  • 330,807
  • 53
  • 334
  • 373
11

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.

Justin R.
  • 23,435
  • 23
  • 108
  • 157
8

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)

Tomalak
  • 332,285
  • 67
  • 532
  • 628
8

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.

Matthew Scouten
  • 15,303
  • 9
  • 33
  • 50
8

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.

Sniggerfardimungus
  • 11,583
  • 10
  • 52
  • 97
8

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

Ludwi
  • 437
  • 4
  • 8
7

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

PotatoEngineer
  • 1,572
  • 3
  • 20
  • 26
7

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.

Mongoose
  • 4,555
  • 1
  • 16
  • 7
6
#! /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
warren
  • 32,620
  • 21
  • 85
  • 124
6

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

Eli
  • 97,462
  • 20
  • 76
  • 81
6

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

}
Ethan Post
  • 3,020
  • 3
  • 27
  • 27
5

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 :)

tsilb
  • 7,977
  • 13
  • 71
  • 98
5

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

Wayne
  • 38,646
  • 4
  • 37
  • 49
5

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.

Valerion
  • 823
  • 8
  • 15
4

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

dongola7
  • 269
  • 1
  • 5
4

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! ;)

4

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.

Tom
  • 6,991
  • 13
  • 60
  • 78
3

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++....

Greg Whitfield
  • 5,649
  • 2
  • 30
  • 32
3

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.

Ronny Brendel
  • 4,777
  • 5
  • 35
  • 55
3

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.

Benjol
  • 63,995
  • 54
  • 186
  • 268
2

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.

James Schek
  • 17,844
  • 7
  • 51
  • 64
  • You do know about http://java.sun.com/javase/6/docs/api/constant-values.html, right? – Michael Myers Mar 17 '09 at 20:39
  • Yes I am. Try searching this one for an error code that is reported using the HEX value. http://edndoc.esri.com/arcobjects/9.2/java/api/arcobjects/constant-values.html – James Schek Mar 18 '09 at 14:52
  • Oh, and there are 8 different java doc libraries for this API... all of them with their own constant-values.html file. – James Schek Mar 18 '09 at 14:54
2

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
John Nilsson
  • 17,001
  • 8
  • 32
  • 42
2

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, foo@example.com) expands to

:0:
* ^Delivered-To:.*foo@example.com
Lists/foo

meaning that emails with a Delivered-To header containing foo@example.com 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.

Jouni K. Seppänen
  • 43,139
  • 5
  • 71
  • 100
2

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

Lucas Oman
  • 15,597
  • 2
  • 44
  • 45
2

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.

Simon Howard
  • 8,999
  • 5
  • 28
  • 21
2

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.

Mark Allen
  • 1,230
  • 1
  • 15
  • 17
  • Mark, try indenting the code in the numbered enumeration by another 4 spaces, it gets displayed as code then. And use backticks (`…`) to mark inline code. – Konrad Rudolph Oct 13 '08 at 12:28
  • I used to do that a lot too. Other programmers would sometimes make fun of my "two letter commands to do everything." – Ferruccio Oct 24 '08 at 00:30
2

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).

Pete Kirkham
  • 48,893
  • 5
  • 92
  • 171
  • I've found a few uses for XSLT lately, and it feels like dabbling in black magic (in a good way). – benzado Feb 26 '09 at 23:34
2

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.

wzzrd
  • 610
  • 4
  • 13
2

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.)

Telemachus
  • 19,459
  • 7
  • 57
  • 79
  • Glad you like the poem, but I can't take any credit for it (or the translation). It's from The Essential Haiku, edited by Robert Haas. – Telemachus Mar 26 '09 at 12:19
1

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
1

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.

Watson
  • 564
  • 2
  • 10
1

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.

Chris Cudmore
  • 29,793
  • 12
  • 57
  • 94
1

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_();
}
Maxime Rouiller
  • 13,614
  • 9
  • 57
  • 107
1
#!/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.

ephemient
  • 198,619
  • 38
  • 280
  • 391
1

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.

AlbertEngelB
  • 16,016
  • 15
  • 66
  • 93
1

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.

databyss
  • 6,318
  • 1
  • 20
  • 24
1

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

1

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 <milot@mymyah.com>        #
##############################################

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()
milot
  • 1,060
  • 2
  • 8
  • 17
1

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

Brian
  • 25,523
  • 18
  • 82
  • 173
1

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)
  • Could you provide some details about what this does? – Beska Mar 17 '09 at 21:05
  • Basically anime tends to come with [23dsfa] as a checksum in the filename, this script I believe extract that value out of the filename and use it to checksum the file. – Pharaun Jul 13 '10 at 15:23
1
alias snoot='find . ! -path "*/.svn*" -print0 | xargs -0 egrep '
Ken
  • 77,016
  • 30
  • 84
  • 101
1

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.

Ferruccio
  • 98,941
  • 38
  • 226
  • 299
1

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!

Adam Liss
  • 47,594
  • 12
  • 108
  • 150
1

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???)

Adam Liss
  • 47,594
  • 12
  • 108
  • 150
1

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/

ashokgelal
  • 80,002
  • 26
  • 71
  • 84
1

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.

1

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.

ELLIOTTCABLE
  • 17,185
  • 12
  • 62
  • 78
1

Work@Home.ps1 and Work@Work.ps1 => modify the hosts file, to go through LAN or WAN addresses.

GvS
  • 52,015
  • 16
  • 101
  • 139
1

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)

James Curran
  • 101,701
  • 37
  • 181
  • 258
  • Does the script check the date and only open electoral-vote.com in election years? You'd earn my up-vote if so. – benzado Feb 26 '09 at 23:37
1

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:

martinus
  • 17,736
  • 15
  • 72
  • 92
1

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.

Alex
  • 8,521
  • 6
  • 31
  • 33
  • Of course I don't know what platform you're on, but is there a reason you're not just using head or tail for this? – wzzrd Feb 05 '09 at 20:33
  • Hmmmm good point... like ls -l -t > temp then do: head -15 temp. I guess I like awk allot and am not used to the more obvious unix commands other people use. How would you do it without creating a temporary file though? – Alex Feb 05 '09 at 23:40
  • Pipe the output of the ls command into the head command just like you did with awk. (BTW, in case you don't already know, ls will allow you to combine the parameters into 1, so you can type ls -lt.) – RobH Feb 21 '09 at 01:41
1

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
}
1

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
robmandu
  • 193
  • 1
  • 2
  • 11
  • Just make sure you have nothing running when your defrag starts. I haven't used it in XP, but I found that, in earlier versions of Windows, the defrag utility would restart the entire defrag process *any* time there's a disk access by something else. (VERY annoying! :-( ) – RobH Feb 21 '09 at 01:48
1

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.

polyglot
  • 9,945
  • 10
  • 49
  • 63
1

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.

Kyle Walsh
  • 2,774
  • 4
  • 27
  • 25
1

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.

benzado
  • 82,288
  • 22
  • 110
  • 138
1

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...

David Berger
  • 12,385
  • 6
  • 38
  • 51
  • Nice, this definitely is cooler than a script I wrote for a similar purpose. I'm out of upvotes for the day, so I'll hit you back next time I can. :) – Kyle Walsh Mar 17 '09 at 20:25
1

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.

1

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);

1

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.

SingleNegationElimination
  • 151,563
  • 33
  • 264
  • 304
1

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

Eddy
  • 1,862
  • 12
  • 12
1

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.

MikeNereson
  • 3,890
  • 6
  • 24
  • 28
0
copy con c.bat
c:
cd\
cls
^Z
tsilb
  • 7,977
  • 13
  • 71
  • 98
0


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
Krsna Kishore
  • 8,233
  • 4
  • 32
  • 48
EvilTeach
  • 28,120
  • 21
  • 85
  • 141