Never been to TextSnippets before?

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

« Newer Snippets
Older Snippets »
5 total  XML / RSS feed 

backup files

// create backup copy

#!/bin/bash

filename=$1
date=`date +%Y%m%d`

usage () {
        echo "Usage: `basename $0` filename"
}

if [ -z "$filename" -a ! -f "$filename" ]; then
        usage
        exit 1
fi

rev=0
backup="$filename.$date.$rev"

while [ -f $backup ]; do
        let rev+=1
        backup="$filename.$date.$rev"
done

cp $filename $backup
exit $?

Rewrite SPAM subject line with procmail

// Remove textdrive's ***SPAM*** addition
// by phearlez
// http://discuss.joyent.com/viewtopic.php?pid=154695#p154695

# Remove the word ***SPAM*** from SpamAssassin's subject line re-write

 :0 fHw
 * ^Subject: \*\*\*SPAM\*\*\*
 | sed -e 's/\*\*\*SPAM\*\*\*//g'

JS Regex's for sourcecode

Extracted from SyntaxHilighter

        MultiLineCComments : new RegExp('/\\*[\\s\\S]*?\\*/', 'gm'),
        SingleLineCComments : new RegExp('//.*$', 'gm'),
        SingleLinePerlComments : new RegExp('#.*$', 'gm'),
        DoubleQuotedString : new RegExp('"(?:\\.|(\\\\\\")|[^\\""\\n])*"','g'),
        SingleQuotedString : new RegExp("'(?:\\.|(\\\\\\')|[^\\''\\n])*'", 'g')

Substrings in Ruby

The other day I was wondering why some really simple Ruby code of mine wasn’t working. The code under test looked something like this:

    def find_directories
        directories = []
        entries = `ls -l`.split/\n/  # get long-format directory listing
        entries.shift   # toss away the totals line
        entries.each do |entry|
            is_directory = (entry[0] == 'd')  # is it a directory?
            directories << dir if is_directory
        end
        directories
    end

I figured that my ls -l.split/\n/ trickery wasn’t working. So I fired up irb and tried it manually. It worked fine.
Then I sprinkled a few puts statements throughout. entries was fine, and each entry was perfect, looking something like this:
drwx------ 24 jcohen jcohen 816 Aug 19 11:04 Documents

I couldn’t figure out what was wrong. All I had to do was look at the first character of entry; if it was a ‘d’, then I knew I had a directory. But for some reason, is_directory was always false. I googled, checked the RDocs for the String class, thumbed through the PickAxe, and something I read triggered one of those ah-ha moments.
Rats: My C# brain is still alive and kicking.

To confirm my fears, I fired up irb again:

irb> s = "Jeff" 
=> "Jeff" 
irb> s[0]
=> 74

That’s right, folks: on a string, the bracket syntax – when given only one parameter – will return the ASCII value of the character inside. Not the actual character, as it will in C#.
To correctly grab a one-character substring from a string, you have to supply two parameters:

irb> s[0,1]
=> "J" 

Publishing raw code

Here is how to add code to a blog post in a way that you can see the code...so you can show the actual raw code without it actually working.

<strong>hello</strong>


Just incase the head and tail in the code tags.

This is handy, I can keep this code here, as I was always searching for where I kept this handy info...now I have a place where I can quickly find all my code, and better still I'm sharing it by default.
« Newer Snippets
Older Snippets »
5 total  XML / RSS feed