Saturday, June 21

Neato .bashrc functions

Saw these three functions in this thread on the Arch Linux forum and thought they were neat. Since I'm on the parents' computer I thought I'd stick them here for the next time I'm on my eee (which is happily running Arch) or for when someone is googling about for .bashrc stuff :).


# from user semperfiguy's .bashrc
extract () {
if [ -f $1 ] ; then
case $1 in
*.tar.bz2) tar xjf $1 ;;
*.tar.gz) tar xzf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) rar x $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xf $1 ;;
*.tbz2) tar xjf $1 ;;
*.tgz) tar xzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "'$1' cannot be extracted via extract()" ;;
esac
else
echo "'$1' is not a valid file"
fi
}

# from user kaKTuZ's .bashrc
function remindme()
{
sleep $1 && zenity --info --text "$2" &
}

# from uer Daenyth's .bashrc
function weather ()
{
links -dump "http://google.com/search?q=weather+${1:-02135}"
| grep -A 5 -m 1 '^ *Weather for' | grep -v 'Add to'
}

Labels: , , , , ,

Thursday, June 19

it's the little things

I always roughly equate Perl and Python in my head. I recognize that they are good for different things but I forget that there's such a peculiarly "Perl" way of doing stuff (or, at least, 'my' Perl way - I don't profess to have tapped into some kind of universal Perl zeitgeist).

Here's some ultra simple code I wrote because I didn't feel like messing with getopt stuff, in both Python and Perl.

Python
commands = ['init','rebuild','destroy']
if len(sys.argv) != 2 or sys.argv[1] not in commands:
print "Error: Specify one of (%s)" % ' '.join(commands)
sys.exit(2)

Perl
my @commands = ('init', 'rebuild', 'destroy');
die "Error: specify one of (", join(', ',@commands), ")\n"
unless $#ARGV == 0 and index( join(' ', @commands), $ARGV[0] ) >= 0;

It's just...........different.

Labels: , , ,