Welcome

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

What next?
1. Bookmark us with del.icio.us or Digg Us!
2. Subscribe to this site's RSS feed
3. Browse the site.
4. Post your own code snippets to the site!

Japanese Alphabets Quiz

This is my first Ruby script. It requires two files, hiragana.rb and katakana.rb (I'm pasting them here as well).

Its purpose is to test your knowledge of two Japanese alphabets, Hiragana and Katakana.

# Japanese Alphabets Quiz, version 1.3 :)

puts "Would you rather test your hiragana (1) or katakana (2) knowledge?"

choose = gets.chomp.to_i

if 
	choose == 1
	puts "OK, hiragana, then!"
	require 'hiragana.rb'
	lang_name = "hiragana"
else
	puts "OK, katakana, then!"
	require 'katakana.rb'
	lang_name = "katakana"
end

$score = 0 

10.times do 

	alphabet = $characters
	
	# Select a random pair of key and value from the given hash
	
	character = alphabet.sort_by{ rand }[0...1] 
	
	# Turn the result into an array
	
	pair = character.shift { |key, value| }
	
	# Get the first array value (was the hash key)
	
	question = pair.first
	
	# Ask the question
	
	puts "Which character is " + question + "?"
	
	# Gets the answer from the user, strip spaces and turn it lowercase to match the hash case anyway
	
	answer = gets.chomp.strip.downcase
	
	# Validate user input
	
	if 
		answer == pair.last
		$score += 1
		puts "Correct! " + question + " is " + pair.last + "."
	else
		puts "Wrong! " + question + " is " + pair.last + "."
	end

end

def grade (points)

	case points
	when (0..3)
		"Bad"
	when (4..6)
		"Average"
	when (7..9)
		"Excellent"
	when (10)
		"Perfect"
	end

end

puts "End of " + lang_name + " quiz. You scored " + $score.to_s + " out of 10."

puts "Your grade is " + grade( $score ) + "."


hiragana.rb
# In this hash we set the basic hiragana characters and their respective phonetic counterparts

$characters = 
	{
	'' => 'a',
	'' => 'i',
	'' => 'u',
	'' => 'e',
	'' => 'o',
	'' => 'ka',
	'' => 'ki',
	'' => 'ku',
	'' => 'ke',
	'' => 'ko',
	'' => 'sa',
	'' => 'si',
	'' => 'su',
	'' => 'se',
	'' => 'so',
	'' => 'ta',
	'' => 'ti',
	'' => 'tu',
	'' => 'te',
	'' => 'to',
	'' => 'na',
	'' => 'ni',
	'' => 'nu',
	'' => 'ne',
	'' => 'no',
	'' => 'ha',
	'' => 'hi',
	'' => 'hu',
	'' => 'he',
	'' => 'ho',
	'' => 'ma',
	'' => 'mi',
	'' => 'mu',
	'' => 'me',
	'' => 'mo',
	'' => 'ya',
	'' => 'yu',
	'' => 'yo',
	'' => 'ra',
	'' => 'ri',
	'' => 'ru',
	'' => 're',
	'' => 'ro',
	'' => 'wa',
	'' => 'wo',
	'' => 'n'
	}


# In this hash we set the basic katakana characters and their respective phonetic counterparts

$characters =
		{
		'' => 'a',
		'' => 'i',
		'' => 'u',
		'' => 'e',
		'' => 'o',
		'' => 'ka',
		'' => 'ki',
		'' => 'ku',
		'' => 'ke',
		'' => 'ko',
		'' => 'sa',
		'' => 'si',
		'' => 'su',
		'' => 'se',
		'' => 'so',
		'' => 'ta',
		'' => 'ti',
		'' => 'tu',
		'' => 'te',
		'' => 'to',
		'' => 'na',
		'' => 'ni',
		'' => 'nu',
		'' => 'ne',
		'' => 'no',
		'' => 'ha',
		'' => 'hi',
		'' => 'hu',
		'' => 'he',
		'' => 'ho',
		'' => 'ma',
		'' => 'mi',
		'' => 'mu',
		'' => 'me',
		'' => 'mo',
		'' => 'ya',
		'' => 'yi',
		'' => 'yu',
		'' => 'yo',
		'' => 'ra',
		'' => 'ri',
		'' => 'ru',
		'' => 're',
		'' => 'ro',
		'' => 'wa',
		'' => 'wy',
		'' => 'wo',
	}

Compile & install git on Mac OS X

Requires Xcode.

export PATH=/usr/local/bin:/usr/local/sbin:/usr/local/lib:/usr/local/include:/usr/bin:/bin:/usr/sbin:/sbin
export IFS=$' \t\n'


# rudix
# http://rudix.org

cd ~/Desktop
curl -L -O http://downloads.sourceforge.net/rudix/rudix-1.5.2-0.dmg
hdiutil mount ~/Desktop/rudix-1.5.2-0.dmg
ls -1 /Volumes/* | egrep -i rudix
open -a Installer /Volumes/rudix/rudix.pkg
hdiutil unmount /Volumes/rudix

which rudix
man rudix


# expat
# also available via http://rudix.org

cd ~/Desktop
curl -L -O http://downloads.sourceforge.net/expat/expat-2.0.1.tar.gz
tar -xvzf expat-2.0.1.tar.gz 
cd expat-2.0.1
./configure --prefix=/usr/local
make
make check
sudo make install

find /usr/local -iname "*expat*"


# Python 2.5.2

cd ~/Desktop
curl -L -O http://downloads.sourceforge.net/rudix/python-2.5.2-1.dmg
hdiutil mount ~/Desktop/python-2.5.2-1.dmg
sudo rudix -i /Volumes/python/python.pkg
hdiutil unmount /Volumes/python

which python
python -V     # Python 2.5.2


# python-docs

cd ~/Desktop
curl -L -O http://downloads.sourceforge.net/rudix/python-docs-2.5.2-1.dmg
hdiutil mount ~/Desktop/python-docs-2.5.2-1.dmg
sudo rudix -i /Volumes/python-docs/python-docs.pkg
hdiutil unmount /Volumes/python-docs

man /usr/local/share/man/man1/python.1
ls -1 /usr/local/share/doc/python/*
find /usr/local/share/doc/python -mindepth 1 -maxdepth 1
find /usr/local/share/doc/python/doc -mindepth 1 -maxdepth 1
find /usr/local/share/doc/python/mac -mindepth 1 -maxdepth 1


# asciidoc
# asciidoc 8.3.0 requires Python 2.4 or newer

cd ~/Desktop
curl -L -O http://www.methods.co.nz/asciidoc/asciidoc-8.3.0.tar.gz
tar -xvzf asciidoc-8.3.0.tar.gz
cd asciidoc-8.3.0
open -e ~/Desktop/asciidoc-8.3.0/INSTALL
sudo ./install.sh

which asciidoc
man asciidoc


# GPG

cd ~/Desktop
curl -L -O http://downloads.sourceforge.net/rudix/gnupg-1.4.9-2.dmg
hdiutil mount ~/Desktop/gnupg-1.4.9-2.dmg
sudo rudix -i /Volumes/gnupg/gnupg.pkg
hdiutil unmount /Volumes/gnupg

which gpg
gpg --version
man gpg



# GetText

cd ~/Desktop
curl -L -O http://downloads.sourceforge.net/rudix/gettext-0.17-1.dmg
hdiutil mount ~/Desktop/gettext-0.17-1.dmg
sudo rudix -i /Volumes/gettext/gettext.pkg
hdiutil unmount /Volumes/gettext

which gettext
gettext --version
man gettext
open /usr/local/share/locale




# git 
# http://rudix.org

cd ~/Desktop
curl -L -O http://downloads.sourceforge.net/rudix/git-1.5.6.2-1.dmg
hdiutil mount ~/Desktop/git-1.5.6.2-1.dmg
sudo rudix -i /Volumes/git/git.pkg
hdiutil unmount /Volumes/git

which git
git --version
man git

sudo rudix -r git.pkg



# git
# http://git.or.cz

cd ~/Desktop
curl -L -O http://kernel.org/pub/software/scm/git/git-1.6.0.4.tar.gz
tar -xzf git-1.6.0.4.tar.gz
cd git-1.6.0.4
./configure
make
sudo make install

which git
git --version


# git man pages

cd ~/Desktop
curl -L -O http://kernel.org/pub/software/scm/git/git-manpages-1.6.0.4.tar.gz
sudo tar -C /usr/local/share/man -xzf git-manpages-1.6.0.4.tar.gz


git --help

git help add
git help bisect
git help shell
git help git-svn
git help git-daemon


cd ~/Desktop
mkdir -p ~/Desktop/git-test
cd ~/Desktop/git-test
openport 9418   # cf. http://codesnippets.joyent.com/posts/show/1747
git clone git://git.kernel.org/pub/scm/git/git.git
closeport 9418
open ~/Desktop/git-test
find ~/Desktop/git-test/git -mindepth 1 -maxdepth 1 -ls


# To configure git see:
# - http://dysinger.net/2007/12/30/installing-git-on-mac-os-x-105-leopard/
# - http://git.or.cz/gitwiki/GitTips


References:

- Git - Fast Version Control System
- Wikipedia: Git
- Rudix
- Install git on Mac OS X 10.4 (expat, asciidoc)
- Installing GIT on Mac OS X 10.5 Leopard (GPG, GetText)
- Compiling Git and git-svn on OSX Tiger (git-svn, Perl)
- git-osx-installer
- GitTips
- Git User's Manual
- Tag: git
- Git Quick Reference
- Git Guide
- Git Internals: Source code control and beyond
- The Git Community Book
- Setting up Git on OSX
- Git and Binaries
- Git cheat sheet
- Setting up a new remote git repository
- Sharing git repositories via OS X's built-in web sharing
- GitHub Tips: Removing a Remote Branch
- Using git in the Finder
- GitX - a git GUI for Mac OS X

Copying JUST mp3 files

I wanted to copy just the mp3 files from my iTunes to a backup folder.
First I made a list of all mp3 files, including the full path, in a text file.
Then I went through that file, deleting any files I didn't want backed up.
Then I used rsync to make the backup.

I went through this process to ensure that I only backed up the files I wanted backed up, and no others.

Substitute your paths and file names as appropriate...this was done in the bash shell.

find [[yourpath]]/iTunes\ Music -name "*.mp3" -print >> [[yourpath]]/mp3List.txt

rsync -avu --files-from=[[yourpath]]/mp3List.txt [[yourpath]]/iTunes/iTunes\ Music [[yourbackuppath]]/mp3_files

prevent script execution upload directories php shtml

// description of your code here

<Directory "/Library/MediaWiki/web/images">
   # Ignore .htaccess files
   AllowOverride None
 
   # Serve HTML as plaintext, don't execute SHTML
   AddType text/plain .html .htm .shtml
 
   # Don't run arbitrary PHP code.
   php_admin_flag engine off
 
   # If you've other scripting languages, disable them too.
</Directory>

Ruby trim string by word count

def trim_by_words(string,wordcount)
    string.split[0..(wordcount-1)].join(" ") +(string.split.size > wordcount ? "..." : "")
end

md5 compute compare files

// description of your code here

md5sum <filename>

recursive find grep

// description of your code here

printf junk avoids problems with single quotes in results returned by find

find . -name "*.txt" -printf '"%p"\n' | xargs grep some_pattern


This example will show path+file but not matching lines.
find . -name "*.php" -printf '"%p"\n' | xargs grep "http://rst.void.ru" | cut -d: -f1


make a script of it and place it in .bashrc
deepfind () { echo "Searching for $1"; find . -type f -printf '"%p"\n' | xargs grep "$1" | cut -d: -f1;}

postfix mail queue watch monitor outgoing email

// description of your code here

postcat

The postcat command displays the content of a message in a mail queue.

To read a message in a mail queue, you need its queue ID. Run mailq for a list of queue IDs. For example, the queue ID of the following message is F2B9715C0B3:

After obtaining a queue ID, use it as an option to postcat to see the contents of the queue file:

mailq
F2B9715C0B3 2464 Mon Oct 13 15:29:39 markus.herrmann@example.com
            (connect to mail.example.com[217.6.113.151]: Connection timed out)
            torsten.hecke@example.net
-- 2 Kbytes in 1 Requests.

postcat -q F2B9715C0B3


Delete all mail in queue
postsuper -d ALL

ubuntu linux date time set update adjust

// description of your code here

date mmddHHMMYYyy

so
date 121520152005
Dec 15 8:15 2005

kill linux process

// description of your code here

find process id first via

ps -alx | grep <string>


then kill it with

kill <PID>


or kill stubborn processes with

kill -9 <PID>