Wednesday, April 27, 2011

perl chomp backticks

When you use backticks in perl to get output from the system, you may end up with trailing newlines. I usually get rid of them with chomp.

Here is an example of getting the (base)name of the script being executed, from within the script.

chomp(my $SCRIPTNAME=`basename $0`);
I usually use the script name in the usage/help text of the script.

It is interesting to note that the parentheses are required, because if you did this:
chomp $SCRIPTNAME=`basename $0`

It would be interpreted as:
(chomp $SCRIPTNAME) =`basename $0`

which is not what you want.

Wednesday, April 20, 2011

new line in echo

By default, echo will not interpret backslash escapes. If you want it to do that, you need to use the -e flag:

echo -e "--\nThis message is composed of 100% recycled ascii"

Tuesday, April 19, 2011

bash (shell) code to verify email address

I used the regex from here, and modified it to accept lower case letters.

if [[ "$email" =~ "^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$" ]]
then
    echo "Email address $email is valid."
else
    echo "Email address $email is invalid."
fi

get adium to always trust a certificate

Turns out, it is not enough to check the dialog box that says: "Always trust "example.com" when connecting to..."

You also have to go to Preferences and double click the account to edit it. Click Options, and uncheck the option "Do strict certificate checks". That should do the trick. If that doesn't work, you might be hitting this bug.

Monday, April 18, 2011

bash printf to variable

If you want to use bash's printf formatting and store the value into a variable, you can use bash's print's -v flag as follows:

calc_elaspsed_time()
{
    local SECONDS=$1
    ((h=SECONDS/3600))
    ((m=SECONDS%3600/60))
    ((s=SECONDS%60))
    printf -v ELAPSED_TIME "%02d:%02d:%02d" $h $m $s
} 

jump to percent of file in vim

here is a little known tip, that can be useful when rummaging through gigabytes of logs: if you want to jump to a percentage position within a file, just type out the number followed by the percent sign.

For example, if you'd like to jump half way down into a file, you would type the number 5, followed by the number 0, followed by the % sign - thats it.

Thursday, April 7, 2011

concatenate output of two commands

Quick followup to my previous post, on a *nix command prompt, if you want to concatenate the output of two commands, here is one way:


$ cat <(command1 arg1 arg2) <(command2 arg)