Random cow(ish) animals preaching quotes on Ubuntu 9.10

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)
random cowsay fortune

random cowsay fortune

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

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

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

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

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

The “easy” way to listen to internet radio in Ubuntu

I started with rhythmbox like most new Ubuntu users. It seemed nice enough but not in the area I was concerned with, internet radio. I tried out many players but was disappointed with different areas of different players. From Ubuntu 8.04 to 8.10 I was using the “good” Amarok (for KDE 3.5). Disappointed by the exclusion of that version in Ubuntu Jaunty 9.04 and unimpressed with workarounds like the PPA’s, I decided to play the field. I came across Exaile, which I’ve blogged about in the past. It’s a good enough player… most of the time. It crashed too often for my likings and I’m getting sick of pkilling it.

It struck me today that I needed a simplier more streamlined solution for my simple needs. I needed a console application. Through a little research, I found moc, which happens to play shoutcast streams as I’ve become accustom to.

I then proceeded to set myself the following way:

sudo apt-get install moc
mkdir -p ~/Music/internet_radio && cd ~/Music/internet_radio
wget -r -l2 -nd -Nc -A.pls http://www.di.fm/index.php
for file in *.pls; do mocp -a $file; done
mocp

1) Installed moc
2) created a directory to download all the playlists from di.fm (since this is the station I listen to most often)
3) wget all the playlists
4) add them all to moc
5) start moc and [tab] to the play list side, enter to play

screenshot-detrateshobo-music-internet_radio

  enter  -- starts playing
  s      -- stops playing
  n      -- plays next item from the playlist
  b      -- plays previous item from the playlist
  space  -- pause
  p      -- pause

  S      -- plays at random
  R      -- repeats the same song in a loop,
	    Next (X button below) must be OFF
  X      -- switches to play sequentially
  o      -- plays a file from the Internet
  u      -- moves playlist item up
  j      -- moves playlist item down
  Ctrl+u -- adds the URL to the playlist
  g      -- searches marked string in file names
  /      -- searches marked string in file names

  r      -- rereads the directory
  T      -- switches to the theme selection menu
  f      -- toggles display mode of song titles
  TAB    -- switches marker bar between the playlist
	    and the file manager panels
  l      -- switches between displaying the playlist
            or the file manager panel
  P      -- switches full path in the playlist
  H      -- toggles hidden files view
  Ctrl-t -- toggles song duration time
  Ctrl-f -- toggles format file view
  m      -- moves to directory entered in config file
  G      -- moves to directory with currently played file
  i      -- moves to marked directory
  U      -- moves to upper directory
  a      -- adds a file to the playlist
  A      -- adds a directory recursively to the playlist
  C      -- clears the playlist
  V      -- saves the playlist
  d      -- removes marked item from the playlist
  Y      -- removes all empty items from the playlist

  < -- decreases volume by 1%
  ,      -- decreases volume by 5%
  >      -- increases volume by 1%
  .      -- increases volume by 5%

  x      -- toggles the mixer channel
  ?      -- shows help

  !      -- goes to a fast dir 1 (set in config file)
  @      -- goes to a fast dir 2
  #      -- goes to a fast dir 3
  $      -- goes to a fast dir 4
  %      -- goes to a fast dir 5
  ^      -- goes to a fast dir 6
  &      -- goes to a fast dir 7
  *      -- goes to a fast dir 8
  (      -- goes to a fast dir 9
  )      -- goes to a fast dir 10

  F1     -- executes ExecCommand1 (set in config file)
  F2     -- executes ExecCommand2
  F3     -- executes ExecCommand3
  F4     -- executes ExecCommand4
  F5     -- executes ExecCommand5
  F6     -- executes ExecCommand6
  F7     -- executes ExecCommand7
  F8     -- executes ExecCommand8
  F9     -- executes ExecCommand9
  F10    -- executes ExecCommand10

Above commands from polish linux’s article on moc audo player, great resource.

codepad.org – an online compiler/interpreter, and a simple collaboration tool.

codepad.org is an online compiler/interpreter, and a simple collaboration tool.

Inspecting GTK dialogs with Parasite

Parasite is a program much like Firebug, except this is for GTK based applications.

By opening a program with parasite enabled, you can literally click elements of the GUI to reveal their properties.

parasite

The installation is pretty simple, open a terminal, clone the git repo, compile install and run. I like to make a folder for software I checkout from SVN or Git in my home dir so I don’t get things all messy. You need git to clone, so if you don’t have it installed already:

$ sudo apt-get install git-core

Clone it:

$ mkdir ~/git_software && cd ~/git_software && git clone git://github.com/chipx86/gtkparasite

Compile and install it:

$ ./autogen.sh && make && sudo make install

I run 64bit, so I had to do the following command to get things working:

$ sudo cp /usr/local/lib/gtk-2.0 /usr/lib64/ -r

Try it out (prefix an application name with GTK_MODULES=gtkparasite):

$ GTK_MODULES=gtkparasite gedit

NautilusSvn – Finally an SVN GUI for Linux that doesn’t totally suck

Now I know that headline may upset some people but from when I originally switched from Windows as my primary desktop, to up until ~a few months ago, I was at a loss for a decent SVN GUI in Linux. The SVN clients in the [Ubuntu] repositories were unstable, quirky or just total garbage.

The first I tried was Rapid SVN, rated high in the ‘add/remove’ programs dialog. I found this very unstable. Next I tried eSvn which I also found unstable and quirky. Then unto KDE svn which seemed quite stable and solid actually… but unfortunately I use GNOME and this application is (obviously) for KDE, so naturally(?), there were some bugs. In particular the title bars and some other variables were not passing values properly, they would show up as literal %t or what have you. This was a deal breaker for me. Smart SVN came recommended and I agree, it has potential but it seemed to be too much for my simple needs and hell, if I’m going to run a Java application, I might as well switch my IDE to Eclipse and use the plugin for that (which is actually a good interface to SVN).

However, I love my current IDE, Geany and I don’t use SVN strictly for code projects. Some of the Ninjaz and I collaborate in mapping using SVN.

For a while I used svn in the terminal, which is really as minimal as you can get and to a degree, I really enjoy it. It also makes for good practice when I need to do things concerning SVN on a server. However, this is 2009 and I (usually) feel more efficient with a GUI.

In windows, my favorite SVN client was Tortoise SVN, the de facto standard in SVN for windows. After tireless research I finally came across the application I was dreaming for, Nautilus SVN, which is attempting to clone Tortoise SVN for Linux. Written in Python, it integrates itself nicely into Nautilus’ right click (context) menu. When I first started using it, the application was pretty barebones but with the latest release, it has come a long way. Bringing in nice emblems and icons to signify status and accompany menu options, it’s really starting to feel like a solid application.

Nautilus SVN in action

Nautilus SVN in action

At version 0.12 beta, it’s obviously got some minor bugs but nothing that stands out… too hard. There is currently an issue with the status checks responsible for the folder emblems that can cause nautilus to temporarily freeze while retrieving information from large repositories but the problem is being addressed, though they’ve hit a few snags along the way.

I talked with one of the developers, Bruce from the Netherlands on their IRC channel, #nautilussvn on irc.freenode.org and feel confident in direction of the project. Bruce is a really nice and informative guy which makes the project all the more interesting to me.

I wish the team the best in the future and I’ll definitely be sticking with this for a while, I suggest other GNOME users check it out as well.

Batch images conversion – color to transparency from the linux terminal

I’ve been re-theming and old forum and the themes that are available are a little less than perfect for our needs. I found a few suitable templates but it still results in me creating a mash-up. Some of the images (though gif) set the image background color to that of the site background. As I was using a different color, this obviously looked bad but I wasn’t about to manually edit all the files to give them transparency.

Being aware of the amazing command line tool for Linux, ImageMagick, I set out to find a tool to help me. I found a transparency flag, calculated the RGB values with the eyedropper in GIMP, then after a test run, I through it in a for loop.

screenshot2

So a directory down from my image source, I created a folder called “new” and ran the following color and like magic, they were all converted.

for image in *.gif; do convert -transparent 'RGB(48,71,94)' $image ../new/$image; done