How to find multiple patterns with GNU findutils

November 30th, 2011 No comments

Actually, searching for multiple patterns should be a trivial task. Find provides a -o operator (and many others) that lets you combine multiple expressions.

A simple Example: You want to find all files in the current directory whose filename extension are either .c or .h

$ find . \( -name "*.c" -o -name "*.h" \) -print

This is not limited to the -name test but can be combined with any other test (like -perm, -size, -type, etc.)

But Careful! You need to quote patterns that contain metacharacters (such as * in the example above). Singe quotes work as well as double quotes. The braces surrounding the expression have to be escaped, too. And watch those spaces right before and after the braces, they’re essential.

q.v. find manpage, GNU find documentation

Categories: Uncategorized Tags:

Hack: Howto disable recently used items list

October 30th, 2011 2 comments

Quite a few applications use ~/.local/share/recently-used.xbel to keep track of a user’s most recent files. Unfortunately, not every application offers customization options to disable this list.

One possible solution, granted, a quite hacky one, is to clear recently-used.xbel and revoke a user’s permission to edit it again.

First, remove and re-create the file to clear it

$ rm -f ~/.local/share/recently-used.xbel
$ touch ~/.local/share/recently-used.xbel

You can then edit the permission so that the user can’t edit the file any more.

$ chmod -w ~/.local/share/recently-used.xbel

For KDE, there is quite a similar mechanism. While there is no single file that stores the recently used items, a .desktop file is created in ~/.kde/share/apps/RecentDocuments/ for every item.

If if you revoke a user’s writing permission to that folder, KDE won’t add any item to the ‘recently used’ list.

$ chmod -w ~/.kde/share/apps/RecentDocuments/
Categories: Uncategorized Tags: ,

Split flac file into tracks using a cuesheet

August 25th, 2011 No comments

shnsplit (part of the “multi-purpose WAVE data processing and reporting utility” shntool package) provides a simple method to split flac files into individual tracks specified in a cuesheet.

$ shnsplit -o flac -f CUESHEET.cue -t %n.%t FLACFILE.flac

With the custom output format module, you can even transcode the tracks directly to another format, e.g. mp3, if your mobile music player doesn’t support flac.

snippets.dzone.com provides an exemplary script for this.

#!/bin/sh
set -e
ENCODE="cust ext=mp3 lame -b 192 - %f"
FORMAT="%n.%t"

FLACFILE=$1
CUEFILE=$2
echo $FLACFILE - $CUEFILE
if [ -z "$FLACFILE" ]; then
    echo "usage: flac2mp3 FLAC_FILE [CUE_FILE]"
    exit 1
elif [ -z "$CUEFILE" ]; then
    DIRECTORY=$(dirname "$FLACFILE")
    BASENAME=$(basename "$FLACFILE" ".flac")
    CUEFILE="$DIRECTORY/$BASENAME.cue"
fi

shnsplit -O always -o "$ENCODE" -f "$CUEFILE" -t "$FORMAT" "$FLACFILE"
Categories: Uncategorized Tags:

Convert subversion repository to GIT

August 17th, 2011 No comments

First, you need to create a file that maps the subversion authors to GIT users (let’s say /tmp/svnusers). The syntax is pretty easy:

kmartin = Kirk Martin <marty@localhost.com>
mattaway = Marshal Attaway <marshal@localhost.com>

To get a list of all your SVN authors, run

$ svn log --xml | grep author | sort -u | perl -pe 's/.>(.?)<./$1 = /'

on you subversion working copy.

Next, you have to create a temp directory (which will be cloned later to get rid of all the SVN stuff).

$ mkdir /tmp/MyProject_tmp
$ cd /tmp/MyProject_tmp

Now, you can fetch the SVN files from you subversion server

$ git-svn init svn+ssh://user@SVNHost/MyProject/trunk/ --no-metadata
$ git config svn.authorsfile /tmp/svnusers
$ git-svn fetch

Please note, that you may need to adjust the protocol (svn+ssh, http, https, ftp, etc.), user, host, path to the project files etc.
To get rid of all the SVN remains, simply clone the newly created GIT repo

$ git clone MyProject_tmp MyProject

Resources and further reading:
http://progit.org/book/ch8-2.html
http://www.jonmaddox.com/2008/03/05/cleanly-migrate-your-subversion-repository-to-a-git-repository/

Categories: Uncategorized Tags:

Specifying file encoding when writing dom Documents

August 10th, 2011 No comments

Assumed, we got a fully parsed org.w3c.dom.Document:

Document doc;
//parse doc etc...

Just using LSSerializer‘s writeToString method without specifying any encoding will result in (rather impractical) UTF-16 encoded xml file per default

DOMImplementation impl = doc.getImplementation();
DOMImplementationLS implLS = (DOMImplementationLS) impl.getFeature("LS", "3.0");
LSSerializer lsSerializer = implLS.createLSSerializer();
lsSerializer.getDomConfig().setParameter("format-pretty-print", true);
String result = ser.writeToString(doc);

will output

<?xml version="1.0" encoding="UTF-16"?>
...

Unfortunately, specifying an encoding isn’t trivial. Here are two solutions that don’t require any third party libraries:

1. Using org.w3c.dom.ls.LSOutput

DOMImplementation impl = doc.getImplementation();
DOMImplementationLS implLS = (DOMImplementationLS) impl.getFeature("LS", "3.0");
LSSerializer lsSerializer = implLS.createLSSerializer();
lsSerializer.getDomConfig().setParameter("format-pretty-print", true);

LSOutput lsOutput = implLS.createLSOutput();
lsOutput.setEncoding("UTF-8");
Writer stringWriter = new StringWriter();
lsOutput.setCharacterStream(stringWriter);
lsSerializer.write(doc, lsOutput);

String result = stringWriter.toString();

2. Using javax.xml.transform.Transformer

Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
DOMSource source = new DOMSource(doc);
Writer stringWriter = new StringWriter();
StreamResult streamResult = new StreamResult(stringWriter);
transformer.transform(source, streamResult);
String result = stringWriter.toString();
Categories: Uncategorized Tags: , ,

Non-interactive ssh with expect

August 2nd, 2011 No comments

If you need a programmatic ssh login, i.e. in a shell script, the best way to make ssh non-interactive usually is a public-key authentication. This requires the public key to be stored on the host machine, which (admitted, in very rare cases) can be hard or impossible. One of those rare cases might be a chrooted environment on a webserver without write access to the ssh config file or the per-user ssh directory.

Unfortunately, most ssh clients don’t allow piping a clear text password from a file or varibale. One solution is to use the SSH_ASKPASS environment variable and a wrapper script but most recommendations suggest the use of expect.

Expect is basically a scripting language to make interactive applications non-interactive. It is often used to automate reactions to certain outputs from programs like ftp, ssh, scp and others.

A simple expect script to rsync a backup from one host to another could look like this

#!/usr/bin/expect
set timeout 600
spawn rsync -avhS user@host:/backups/ /backups/
expect {
   "password" {send "secret\n";}
}
expect {
   eof {exit 0;}
}

Note that expect eof is used here, because the alternative interact command is not compatible with cron scripts.

For a list of commands have a look at the expect man page or expect’s sourceforge page.

Categories: Uncategorized Tags:

Fix veetle audio delay

July 23rd, 2011 No comments

When you try to output a sound stream from Veetle 0.9.17 through PulseAudio, you will most likely run into an annoying audio delay. Most of the fixes that can be found usually suggest changing the Veetle settings to use OSS for sound output.
However, there is a beta version of the latest Veetle plugin that enables you to continue using PulseAudio and get rid of the audio delay:

http://veetle.com/download.php/veetle-0.9.17plus-linux-install.sh
http://veetle.com/download.php/veetle-0.9.17plus.tgz

Please note that this version is still marked experimental. It seems to work fine on Fedora 15 though.

Resources:
http://www.lziegler.com/wordpress/?p=108

Categories: Uncategorized Tags: ,

Escaping WordPress shortcodes

July 15th, 2011 No comments

With WordPress 2.5 came the WordPress shortcodes, a simple set of functions to create macros to be used in WordPress posts. Quite a few plugins were developed subsequently to enable escaping of those shortcodes. This is necessary because the shortcode parser, as it tries to interprete everything between square brackets, makes it impossible to include a non-interpreted shortcode such as [gallery] into continuous text.

However, those plugins are actually obsolete. The issue has been fixed with the release of WordPress 2.8. Now there is an official syntax that allows you to escape shortcodes in your posts without the need for any additional pluging.
Simply double the square brackets to allow any shortcode in your post escape the parsing:

    [[shortcode]]

will be displayed as [shortcode] in your post.

Optional parameters to the shortcode are of course still possible, i.e.

    [[gallery link="file" columns="2"]]

will be displayed as [gallery link="file" columns="2"]

Resources:
http://www.wordpresspluginfu.com/2010/04/escaping-shortcodes-in-wordpress/

Categories: Uncategorized Tags:

Typesetting formuals with MathJax

July 6th, 2011 No comments

Most hosting providers don’t have LaTeX packages installed on their servers and installing LaTeX yourself is usually only possible on rootservers, which are quite a bit more expensive (and therefore much less common) than regular hosting plans.

However, there is a great project called MathJax, that enables you to display formulas directly inside your wordpress posts without the need to install any LaTeX packages on your server. While you can install the javascript that is used to typeset your formula from either LaTeX or MathML code locally, the easiest way to use MathJax is probably to their content delivery network. For even more convenience, there is even a wordpress plugin that makes typesetting formulas easy as pie.

First, enable the CDN option in MathJax-Latex Plugin Options:

MathJax-LatexPluginOptions

Enable "Use MathJax CDN Server" and save the changes

You can now use regular LaTeX code within your post:

[latex] S = \frac{1}{(1 - P) + \frac{P}{N}}\leq\frac{1}{1-P} [/latex]

will be typesettet as

\(S = \frac{1}{(1 – P) + \frac{P}{N}}\leq\frac{1}{1-P}\)

For a more detailed explanation, have a look at the plugin’s description.

Of course, there are quite a few other CDNs, that will typeset your LaTeX formulas. But usually, they just render an image that can be embedded in your post or use flash.
The main advantage of MathJax is that you get your formula as scalable instance that will fit nicely even if you resize your text. You can also access a formula’s source and copy and paste it into your math application (like Mathematica). Have you tried right-clicking the formula above?

Categories: Uncategorized Tags: ,

Auto-incremented fields in apache’s derby

June 27th, 2011 No comments

Unlike MySQL and other SQL dialects, apache derby doesn’t support the AUTO_INCREMENT keyword. To create an auto incremented filed in derby, you can use

CREATE TABLE customer
(
id INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
name VARCHAR(40) NOT NULL,
address VARCHAR(1024),
city VARCHAR(30),
CONSTRAINT primary_key PRIMARY KEY (id)
) ;

The value of id is now incremented automagically on every insert. You don’t have to specify it manually in the insert command

INSERT INTO customer(name , address, city)
   VALUES('Mr. Peanutbutter', '221 W 31st St', New York City);

For instruction on how to create auto incremented fields in other SQL dialects, have a look at the SQL Dialects Reference.

Categories: Uncategorized Tags: ,
This website uses a Hackadelic PlugIn, Hackadelic SEO Table Of Contents 1.7.3.