A Django site.
November 4, 2008

Jason Hall
jayce^
Jayce
» seq on osx

I was trying to mirror down a set of zip’s (CD3WD a pretty amazing resource) and I didnt’ want to just mirror, because of other data.  So I was trying to do a simple loop, but was thinking too Perlish and trying to sprintf somehow the numerical formatting (files are cd3wd401 to 444, so needed the 0).  Perlhoser corrected my attempt, by pointing me to the seq command, with the -w flag, which pads the width to the largest amount, with leading zeros, perfect.

Until I tried to run it on my mac, which had the drive I wanted it on, instead of a linux box.  Seq wasn’t installed, and I couldnt’ find it with macports.  However I did find via google that the ‘coreutils’ port has a ‘gseq’ tool. Drop in replacement for my script.

June 27, 2008

Aaron Toponce
atoponce
Aaron Toponce
» DASH on Ubuntu

A couple recent posts have started on the Utah Open Source Planet regarding popd and pushd not being available on a default Ubuntu install. As discovered, popd and pushd are shell built-ins for the BASH shell, and not provided by the Debian Almquist Shell (DASH). Why has Ubuntu made the change from BASH to DASH as the default shell? Well, there some reasons for it, as identified by the Ubuntu Wiki.

BASH is full-featured bloat. Yes, bloat. If System V Init scripts are relying on BASH to start their service, your boot process will be slower. DASH, in comparison is light and snappy, thus greatly improving the time it takes your computer to boot. If your scripts are adhering to POSIX standards, and are using /bin/sh rather than /bin/bash, you shouldn’t notice any problems. However, if your scripts are relying on /bin/bash features, such as popd or pushd, and you change the interpreter at the top of your script to /bin/sh, you’ll have some breakage.

For what it’s worth, this isn’t anything new with 8.04. DASH became the default in Ubuntu with 6.10, so we’ve had it in this manner for some time. If you would like to change it, then point the symbolic link from /bin/sh to /bin/bash rather than /bin/dash, and you’re done. However, as you may have noticed, it could cause some breakage if you Bourne-compatible scripts contain “Bashisms”.

Personally, I recommend the Z-shell (ZSH) to anyone looking for an alternative to DASH or BASH. Much more capable, flexible and configurable shell.

March 29, 2008

Aaron Toponce
atoponce
Aaron Toponce
» My ZSH Prompt Improved

I’ve been meaning to get to this for some time, but haven’t gotten around to it until today. In a previous post, I shared with the world my zsh PS1 variable. Well, I extended it a bit this morning making it more informative. First, I need to setup a scenario:

I’m running screen locally on my laptop (we’ll refer to it as SCREEN_L), and remotely on my server (we’ll refer to this one as SCREEN_R). For SCREEN_L, there is a ~/.screenrc that I use to show what screen window buffer I’m using via the “hardstatus” directive. My ~/.screenrc is thus:

aaron@kratos:~ 2609 % cat .screenrc
screen -t python 0
screen -t irssi 1
screen -t shell 2
screen -t notify 3
hardstatus alwayslastline
hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{= kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B} %d/%m %{W}%c %{g}]‘ 

That puts a nice status line at the bottom of my terminal, letting me know what screen buffer I’m in, as well as it’s title. However, I don’t want to run a ~/.screenrc under SCREEN_R. This just means yet another hardstatus line that I really don’t want (I value my pixels). Rather, I would like to be notified what screen window I’m under, if any, in my prompt. After a bit of digging, I came up with the solution.

First, the environment variable $WINDOW is keeping track of this automatically for me. For example:

aaron@kratos:~ 2610 % echo "\"$WINDOW\""
""
aaron@kratos:~ 2611 % screen
aaron@kratos:~ 2612 % echo "\"$WINDOW\""
"0"

Perfect! Now, to get it into my prompt. This is easy with adding a little if logic to our ~/.zshrc:

if [ x"$WINDOW" = x ]; then
    SCREEN="($WINDOW)"
else
    SCREEN=""
fi
PS1="%n@%m$SCREEN:%~%(?..[%?]) %h %# "

What am I doing here? Well, first off I’m checking to see if the $WINDOW variable is blank. I’m using x”$WINDOW” != x for a simple reason. Zsh is expecting a valid condition after ‘!=’. Unfortunately, an empty string isn’t satisfying it. So, I prepend the letter x to the $WINDOW variable, and test to make sure it doesn’t equal just ‘x’. Second, if $WINDOW equals anything other than ‘x’, a SCREEN variable is defined with the $WINDOW variable contents wrapped in parenthesis. This $SCREEN variable is then added to the prompt.

Now, I add this to my ~/.zshrc for my SCREEN_R, and I can see where I’m at in my screen session remotely without the need for a remote ~/.screenrc. Here’s the results, also showing off exit code:

aaron@kratos:~ 2614 % screen
aaron@kratos(0):~ 2615 % ping foo
ping: unknown host foo
aaron@kratos(0):~[2] 2616 %

Cheers!

January 4, 2008

Aaron Toponce
atoponce
Aaron Toponce
» Testing AlphaNumeric Arguments In Bash

Spending the evening working on my shell scripting, I thought I would jump into “Wicked Cool Shell Scripts” by Dave Taylor. In his script validalnum.sh, he has a test case to check if a user entered in valid alphabetic or numeric characters. His result is elegant and clean. I’ve changed up the script a bit for clarity:

#!/bin/bash

echo -n "Enter alphanumeric input: "
read input
 
compressed="$(echo $input | sed -e 's/[^[:alnum:]]//g')"
 
if [ "$compressed" != "$input" ] ; then
    echo "Input not valid."
else
    echo "Input valid."
fi

In this example, the user is asked to enter input that can be any combination of letters and numbers, regardless of case. If the user enters punctuation, the test case fails, and the user is notified of such. Otherwise, the test case passes, and everyone is happy.

I want to call to your attention the cornerstone of this script, however:

compressed="$(echo $input | sed -e 's/[^[:alnum:]]//g')"

The variable $compressed is holding only alphanumeric characters. This is done by taking the user input, and piping it to the stream editor sed. With sed, we are searching for any character in the string that is not a number or a letter. If such a character exists, we remove the character altogether. Thus, if $compressed removes any characters, then it does not match what the user entered, and our test will fail. If no characters were removed, then no punctuation exists in the input, and our test case will pass.

I thought this was most clever, and just had to share, hoping others benefit from this simple example. I also hope that Dave is not mad at me for taking an example, changing it up a bit, and presenting it on this blog. Thanks Dave.