Tips and Tricks About Computers, Web Development, Linux, the Internet and the Like
Programming
New Python eBook, Much Better, Down and Dirty
Jul 1st
Some friends recommended a book that’s a quicker pace with some better programming practices. Learn Python The Hard way (or quick way
). It reads more like a walk-through tutorial / quick reference and gives you easily repeatable programming practices that will get you a stronger feel for the language. It’s probably best used in combination with the Python Documentation. Also, a fair warning, Dive Into Python has war declared against it.
Random cow(ish) animals preaching quotes on Ubuntu 9.10
Feb 3rd
Looking for something interesting when I login to one of my servers, I decided to whip up the following script I appended to my ~/.bashrc file.
# fortune and cowsay are needed for the snippet to work, I had to install these first sudo apt-get install fortune cowsay
COWDIR=/usr/share/cowsay/cows/; COWNUM=$(($RANDOM%$(ls $COWDIR | wc -l))); COWFILE=$(ls $COWDIR | sed -n ''$COWNUM'p'); fortune | cowsay -f $COWFILE
UPDATE:
Suggested by MrBougo, a shorter but perhaps more process intensive method:
fortune | cowsay -f $(ls /usr/share/cowsay/cows/ | shuf | head -n1)
Breaking down the script, the first 3 parts create variables and the last command executes the cowsay and quote.
# defines the directory of the cow files COWDIR=/usr/share/cowsay/cows/; # Get a random number limited to the number of files in the directory, making clever use of % (mod) and adding 1 to make sure it doesn't return 0 COWNUM=$(($RANDOM%$(ls $COWDIR | wc -l))+1); # list the contents of the cow dir again, pipe to sed and use the number as a random line to get the name of a file COWFILE=$(ls $COWDIR | sed -n ''$COWNUM'p'); # use fortune to get a quote, pipe to cowsay and use the file as defined above fortune | cowsay -f $COWFILE;
Using Nautilus Scripting Abilities to Integrate Right Click File Enqueues with mocp
Nov 8th
Using moc player can prove to be both beneficial and challenging. I’ve found myself going back to exaile for a few hours on random days for the simplicity in file management via a GUI. Since I prefer to use a single media player and mocp is light weight and helpful in so many other ways to me… I knew I needed a solution. It dawned on me just today how simple that solution could be with nautilus scripts.
#!/bin/bash # Enqueue with mocp # by Tyler "-z-" Mulligan # # This is a nautilus script. When placed in ~/.gnome2/nautilus-scripts # and chmod +x you will have the ability to right click >> enqueue files # or directories in mocp. # mocp -a "$@"
Some other tips… [ and ] silently skip back and forward respectively at a rate of 5sec per second held… this beats the left and arrows which work interactively at 1sec per sec.
? and h bring up the help, don’t forget this. Use this, learn the commands that work for you and happy listening.
Thanks to MrBougo again for helping me simplify the script further… I was originally using a for loop which is unnecessary as the quotes will help the variable expansion and mocp -a can accept multiple files/folders.
Reducing pageweight by compressing production css and js files
Sep 4th
I’ve been a little obsessed with improving the speed of web pages via minified javascript and css files. YUI’s team not only agrees with this, they recommend gzipping your minified js and css files. For a while I’ve been calling YUI Compressor inside my push to production scripts to do the deed. However, with this new mention of gzipping, I think might be exploring other options such as the method mentioned on the page which originally linked me to that awesome YUI writeup; minifying and gzipping javascript and css on the fly using php.
Finding the difference in time between the first and last file in a folder using bash
Aug 22nd
I was working on running some statistics on log files and it required me to figure out the difference to increase the accuracy. I came up with the following bash script:
#!/bin/bash # get the dates start_date=$(date --utc --date "$(ls -Rt --full-time | tail -n1 | awk '{ print $6 }')" +%s) end_date=$(date --utc --date "$(ls -Rt --full-time | head -n2 | tail -n1 | awk '{ print $6 }')" +%s) # find the difference difference=$((end_date-start_date)) # echo results echo $end_date - $start_date = $difference seconds echo $((difference/86400)) days
Which I originally wrote as a one liner:
start_date=$(date --utc --date "$(ls -Rt --full-time | tail -n1 | awk '{ print $6 }')" +%s); end_date=$(date --utc --date "$(ls -Rt --full-time | head -n2 | tail -n1 | awk '{ print $6 }')" +%s); difference=$((end_date-start_date)); echo $end_date - $start_date = $difference seconds; echo $((difference/86400)) days;
I got a little carried away and created this beast, which still isn’t as accurate as I need it to be but it did give me some information:
map_1=nordiccastle;map_2=dance;start_date=$(date --utc --date "$(ls -Rt --full-time | tail -n1 | awk '{ print $6 }')" +%s); end_date=$(date --utc --date "$(ls -Rt --full-time | head -n2 | tail -n1 | awk '{ print $6 }')" +%s); difference=$((end_date-start_date)); echo $... Read Moreend_date - $start_date = $difference seconds; echo logs for $((difference/86400)) days; map_1_ended=$(find -name *00*.log | xargs egrep -A 4 "endmatch|timelimit -1" |grep $map_1 |wc -l); map_1_played=$(find -name *00*.log | xargs egrep "gamestart" |grep $map_1 |wc -l); echo $map_1 endmatched $map_1_ended out of $map_1_played times played; map_2_ended=$(find -name *00*.log | xargs egrep -A 4 "endmatch|timelimit -1" |grep $map_2 |wc -l); map_2_played=$(find -name *00*.log | xargs egrep "gamestart" |grep $map_2 |wc -l); echo $map_2 endmatched $map_2_ended out of $map_2_played times played
It was used to see how many times a map was played and how many times it was voted to end the match.
It should really be a separate script to allow for more organization
Generating sequences of numbers or characters with bash
Aug 15th
If you ever needed to generate a sequence of characters or numbers, the terminal (using bash) is a quick and easy way to do it. Lets explore some examples bash’s brace expansion:
$ echo {a..z} a b c d e f g h i j k l m n o p q r s t u v w x y z
by defining a start and end character with the ‘..’ in between, we tell bash to fill in the rest and echo a list for us. Those are all lowercase, what if you wanted uppercase? simple:
$ echo {A..Z} A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Or both, with a few extra characters in the mix:
$ echo {A..z} A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z
It doesn’t always have to be a-z though,
$ echo {A..G} A B C D E F G
This also works with numbers:
$ echo {0..9} 0 1 2 3 4 5 6 7 8 9 echo {0..100} 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
Descending as well as ascending
$ echo {9..0} 9 8 7 6 5 4 3 2 1 0
There is another method to generate a sequence of numbers from the command line, rightfully called ‘seq’
$ seq 1 5 1 2 3 4 5
The difference here is that it’s delimited by a new line, however, we can override that with the -s (seperator) flag
$ seq -s " " 1 10 1 2 3 4 5 6 7 8 9 10



