Tips and Tricks About Computers, Web Development, Linux, the Internet and the Like
Programming
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.
Resources for Learning About jQuery Functions and a Note About Callbacks
Sep 7th
Though it’s still using 1.2.6 (with current jQuery at 1.3.2), visualjquery.com has always been a favorite of mine for it’s simple and elegant layout. Some of the functions leave some documentation to be desired but after working with jQuery for a while, you begin to develop a taste for what is needed. Unfortunately, next to this issue and despite it’s strong layout, there is one other thing this application that takes this application down from strongest candidate. It cannot look into the future and as such, some functions are outright missing.
The jquery API is a somewhat familiar interface, featuring the latest version, the freshest functions and some decent documentation. Another in-depth (but outdated) resource is the old api-browser. Of course, there exists the Main Wiki Documentation, which also provides Alternative Resources. I think I’ll find myself using http://api.jquery.com/ most often despite it lacking the multi-panel style selection. It integrates it’s documentation with jsbin.com for testing / playing with code which is a nice touch.
Now a note about callbacks while we’re on the subject of jQuery. I find myself using them quite often, with ajax specifically. Understanding how to nest functions in jQuery allows you to do more things in terms of user or application response. When using ajax, you’ll often want to fade out content, do some processing, report the data back and have it fade back in nicely rather than a quick blink that can cause discomfort for a user. If you aren’t familiar with a callback, it’s a function that’s executed when the current function finishes.
Below I will break down this common scenario.
You have your HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
<script src="my_ajax.js" type="text/javascript"></script>
<style>
#ajax_loader {
display:block;
height:90px;
width:90px;
background:url('http://imgur.com/KW8ZD.gif') no-repeat 0 0;
}
</style>
</head>
<body>
<div id="ajax_loader"></div>
<div id="ajax_content"></div>
<a href="#" id="my_link">click me</a>
</body>
</html>
your php (or whatever language returning the data file) (my_page.php)
<? echo "hi, I was loaded with ajax!"; ?>
and your javascript (my_ajax.js) that glues them together
// $(function() is shorthand for $(document).ready(function () {
$(function() {
$("#ajax_loader").hide();
$("#my_link").click(function() {
$("#ajax_loader").fadeIn();
$("#ajax_content").fadeOut();
$("#content").load("my_page.php",function() {
$("#ajax_loader").fadeOut();
$("#ajax_content").fadeIn();
});
});
});
Here we can clearly see how all functions are nested into one root, the document ready function. We go two more deep here, using the click event and then the load callback. Many functions allow for callbacks, search the documentation for the best way you think you should be nesting them. I hope this helps to clarify some of the basics in AJAX using jQuery, good luck.
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)) daysWhich 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 playedIt 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 zby 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 ZOr 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 zIt doesn’t always have to be a-z though,
$ echo {A..G}
A B C D E F GThis 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 100Descending as well as ascending
$ echo {9..0}
9 8 7 6 5 4 3 2 1 0There 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
Jul 15th
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
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 ExecCommand10Above commands from polish linux’s article on moc audo player, great resource.
codepad.org – an online compiler/interpreter, and a simple collaboration tool.
May 27th
codepad.org is an online compiler/interpreter, and a simple collaboration tool.



