Home
Tony Perrie [entries|archive|friends|userinfo]
Tony Perrie

[ website | My Website ]
[ userinfo | livejournal userinfo ]
[ archive | journal archive ]

Randoword.sh [Oct. 30th, 2006|12:33 pm]
[Tags|, ]

I've had variations of this script kicking around in my .bash_history for quite some time now. The first version of it merely printed a random word from my dictionary file at a rate of one per second. This served as a source of inspiration/entropy for irc conversations and Flickr titles. Today, I decided to extend this one-liner to generate eight-words-at-a-time at a variable rate and jigger the output into an irssi process to amuse the good netizens. Here is the implementation for randoword.sh.


#!/bin/sh
i=0 
while true; do 
  let i=$i+1
  dictionary="/usr/share/dict/linux.words"
  dl=($(wc -l $dictionary))
  echo -n "randowords #$i: "
  for j in $( seq 1 8 ); do 
    head -n$(($(head -c4 /dev/urandom | od -An -tu4) % $dl))\
    $dictionary| tail -n1
  done | xargs
  sleep $((RANDOM % 3600))
done

Update: I never really understood how to use random numbers within a bash script without invoking perl (or equivalent). In light of [info]_fool's comment, I rewrote my script using only bash and POSIX commands. I had to break-out /dev/urandom and od because bash's $RANDOM variable only produces random values up to 32767. Technically, it'd be a whole lot faster for a C program to use a pointer to jump around randomly in the dictionary, but that seems too much like work.

Update 2: The seq command probably isn't POSIX.

Update 3: In Ruby: ruby -e ' $w=STDIN.readlines; 8.times{print "#{$w[rand(483523)].chomp} "}' < /usr/share/dict/linux.words

link13 comments|post comment

Breaking the Law (of Hostname Canonicalization) [Jun. 9th, 2006|06:40 pm]
[Tags|, , , ]

Mailman is a great little program written in Python that lets you manage mailing lists. I was tasked with installing it on a new machine last week, and I got to learn about canonicalization the hard way. All of the emails sent by our server out to the mailing lists were using the host name instead of the DNS A record for the Mailman VirtualHost. I tried for days to fix this and eventually got the feeling that it was DNS related. However, I still couldn't see why Sendmail would force the To-header back to the canonical name when Mailman was setting it correctly. Eventually, I happened across this email on Mailman's dev list that solved the problem by removing one of sendmail.cf's canonicalization rules. I'm sure this violates some RFC. Although, the hack does indeed solve the problem. The actual wizardry was to comment out the following line in sendmail.cf.

# pass to name server to make hostname canonical
# R$* $| $* < @ $* > $*         $: $2 < @ $[ $3 $] > $4

It's absolutely wrong to do this because if you edit sendmail.mc, and restart sendmail, the cf file gets regenerated automatically which obliterates the change. Is there a better way to fix this without violating RFCs and changing sendmail rulesets? Note: I don't have access to the DNS server.

linkpost comment

Pathmunge [May. 11th, 2006|06:12 pm]
[Tags|]

This one came from reading RHEL4U3's default /etc/profile today. Before you call me a nerd and go back to your so called life, hear me out on this. So, the good folks in Raleigh decided to add a function called pathmunge to their default profile. This function just allows you to type stuff like pathmunge ~/bin after to add a directory to your search path. Most self-respecting nerds probably sorted this out in 1987, however, I'm an idiot and continue to do things the old fashioned way until I'm bludgeoned with the technology stick. Unfortunately, pathmunge doesn't get added to the default shell environment for all users. So, I just added it to /etc/bashrc on my machines today. Here is how to implement pathmunge in all of its glory...


pathmunge () {
  if ! echo $PATH | /bin/egrep -q '(^|:)$1($|:)' ; then
    if [ "$2" = "after" ] ; then
      PATH=$PATH:$1
    else
      PATH=$1:$PATH
    fi
  fi
}
Feel free to throw this into your ~/.bashrc if you haven't implemented it already.

linkpost comment

rm -blah [Apr. 21st, 2006|02:06 pm]
[Tags|]

Being the resident Unix nerd in my group, about once a year, someone asks me how to remove file named -blah. The problem occurs because the hyphen character is the option delimiter for shell commands and utilities. So, typing ''rm -blah'' or even ''rm \-blah'' usually results in ''rm: Not a recognized flag: b'' on most systems. The solution to this problem is so amazingly stupid that the asker usually walks away with his or her pride obliterated. The answer? rm ./-blah. [info]ivo told me about the alternate solution of using rm -- -blah to do it. I tend to like the ./ way better though because it's so obvious once you know it.
link8 comments|post comment

navigation
[ viewing | most recent entries ]

Advertisement