Double Tall Iced Mocha, Lite on the Chocolate

August 1, 2007

Learn something new every day

Filed under: /StupidShellTricks, /geek — Ken @ 11:53 pm

Usually in a bash script when I want to parse a file line by line I do something like this:
exec < foo.txt
while read LINE;
do
SOMEVAR=$SOMEVAR,$LINE
done

This evening I was writing a bash wrapper for debootstrap to automate the building of chroot environments and for some reason I chose to stray away from my norm and did the following:
cat foo.txt | while read LINE;
do
SOMEVAR=$SOMEVAR,$LINE
done

After a few test runs I noticed $SOMEVAR did not have what I expected after exiting the while loop. After some debugging (bash -x) I discovered the variable was magically empty after the last read LINE of the while loop. I immediately went back to old ways of parsing and sure enough it worked as I expected. Hmm… Well after finding the explanation it makes perfect sense. I am just surprised I hadn’t run into it before.
From the Bash FAQ

E4) If I pipe the output of a command into `read variable’, why doesn’t
the output show up in $variable when the read command finishes?

This has to do with the parent-child relationship between Unix
processes. It affects all commands run in pipelines, not just
simple calls to `read’. For example, piping a command’s output
into a `while’ loop that repeatedly calls `read’ will result in
the same behavior.

Each element of a pipeline runs in a separate process, a child of
the shell running the pipeline. A subprocess cannot affect its
parent’s environment. When the `read’ command sets the variable
to the input, that variable is set only in the subshell, not the
parent shell. When the subshell exits, the value of the variable
is lost.

October 30, 2004

geo-cam (explaining the previous post)

Filed under: /StupidShellTricks — @ 6:06 pm

A while back when I was gearing up for the STP, I thought it would be cool to update my blog with a picture from my camera. That way I could send pictures during the ride. Then I thought it would be cool if I could send a phone cam picture with an address and have it show a map of where I was. So ended up writing geo-cam to do this.

The funny thing is that I spent a bunch of time writing it the day before the STP and didnt even bother to use it. I had a conversation today that reminded me of it. So I decided to clean it up a bit and post it.

It works like this, I send an email from my phone with a picture, and the address in the message body. I have rule on my mailserver that takes all emails to this email address and feeds them to the shell script which then does the following:

  • Makes a thumbnail of the picture
  • Takes the address and submits to Geocoder.us
  • Parses the returned xml with the lat. and long. co-ordiantes
  • Takes the lat. and long. submits to the Tiger Map Server
  • Downloads the returned map image
  • Constructs a blosxom post on my blog

    Code is here.
    I took the image conversion/thumbnail code from Matt’s phonecam.sh shell script.

    This is really just a proof of concept, and I wrote it because I really wanted to use Geocoder.us for something.

    BTW, thanks Schuyler for writing geocoder and Matt for phonecam.sh

  • October 26, 2004

    Spam Subject Lines

    Filed under: /StupidShellTricks — @ 12:52 am

    If you look at the left sidebar of my blog, you will notice a new feature. The random spam subject line.

    Tonight I decided to whip out a little bash cgi script that would pick a message at random from my spam trap and display the subject line. So every time you look at my site, you get a different spam subject line to mull over. So go ahead, hit refresh, try for yourself.

    You can look at my Inbox vs SpamTrap Graphs (just started graphing this week, so not much historical data) and see that there are currently over 12,000 pieces of spam to select subject lines from. Definitely enough to keep you entertained for hours.

    I am not checking for null subjects so it might come up blank every now and again.

    The source for the shell script is here.

    September 20, 2004

    whore.cgi

    Filed under: /StupidShellTricks — @ 5:18 am

    In my spare time I help sysadmin the box this site is on.
    This generally means getting vhosts setup for people and showing them how to setup their mail client etc… Recently Jason moved his site and mail over so I have been setting up the webserver for him and what not. He sends me an email asking to make sure that his cgi-bin has execute permissions. So I go ahead and make sure the permissions are set properly and then as a good sysdamin, I decide to whip out a little one line bash cgi to test his cgi-bin and make sure my work there is done.

    So it basically started out as:

    #!/bin/bash

    echo Content-type: text/plain

    echo “”

    echo “<html&gt<head><title>whore</title></head><body>whore</body></html>”

    A simple little cgi that produces a lame web page that says “whore”. Whore is a term of endearment for us, we call each other whores all the time, so it I figured it would be kind of funny.

    Then I thought, a cgi that produces a static page is really no fun. I need something dynamic about the page, I know I will change the format of the word “whore” every time the cgi gets executed. By this time I have invested about 15 minutes of time into this and I can see it progressively getting worse.

    I decide that I will construct about 5 different formats of the word whore, and randomly select one each time the page is loaded. So then I think to myself, “Oh crap, how do you generate a random number in bash?”. Well that led to my next time suck:

    Articles about random number generation in Bash

  • 1
  • 2
  • 3

    So know I am into this about 40 minutes, I have read some different ways to do what I want to do. I need to get this done so I can move on with my life.

    Here is the result: whore.cgi

    And the resulting code, becuase you really need to be able to do this.

    So to answer your question Jason, yes it looks your cgi-bin working… like a whore!

  •