The other day I came across Solaris’s implementation of /bin/true. I use “true” for infinite while loops quite often. However, I began to wonder what legitimate uses existed for “false”. Example: disabling shell access for user accounts. Do you know of another legitimate use of false? Submit an example by commenting on this post and win Chris F.A. Johnson’s Shell Scripting Recipes.

Rules:

  1. “Disabling shell access for user accounts” is not a valid submission.
  2. You cannot submit someone else’s submission.
  3. One submission per person.
  4. I will take submissions until March 9, 2008 10PM CST.
  5. One winner will be randomly chosen from the valid submissions.

 

Win a copy of Chris F.A. Johnson’s Shell Scripting Recipes by telling me why the script below does not work.

UPDATE: Quite a few people responded correctly (see comments).  I will sort it out tonight and decide who wins.

#/bin/bash
doRead()
{
  local retVal=1
  ps -e -o user | grep apache | \
  while read user
  do
  echo $user
  retVal=0
  done
  return $retVal
}
doRead
echo "doRead exited with retVal = $?"

When the script runs, assuming the host has a user named apache and is running something, you should see “apache” a few times and then “doRead exited with retVal = 0″. However, the actual output is below:

# ./readReturnValExample.sh
apache
apache
apache
apache
apache
apache
apache
apache
doRead exited with retVal = 1

I ran into this problem when writing the monitor cpu usage shell script.  I could have put echo statement’s all over the place in order to find out what the value of retVal is at various points in the script.  However, I used the shell’s builtin debug option instead. I just added “set -x” to the top of the script, like so:

#/bin/bash
set -x
doRead()

This prints out exactly what the shell is doing.

# ./readReturnValExample.sh
++ doRead
++ local retVal=1
++ ps -e -o user
++ grep apache
++ read user
++ echo apache
apache
++ retVal=0
++ read user
++ echo apache
apache
++ retVal=0
++ read user
++ echo apache
apache
++ retVal=0
++ read user
++ echo apache
apache
++ retVal=0
++ read user
++ echo apache
apache
++ retVal=0
++ read user
++ echo apache
apache
++ retVal=0
++ read user
++ echo apache
apache
++ retVal=0
++ read user
++ echo apache
apache
++ retVal=0
++ read user
++ return 1
++ echo 'doRead exited with retVal = 1'
doRead exited with retVal = 1

As you can see, the value of $retVal is valued at 1, directly after the last and unsuccesful read statement. The $25.99 dollar question is, why? I remember reading something about this a long, long time ago but its quite hazy and a cursory google search did not resolve my question. The person that answers that question (except Chris as he won my last contest) gets the book. If multiple people answer, the best single answer gets the book.

After publishing 10 Linux commands you’ve never used I was the benefactor of much feedback as to how I could have used the commands better. Which is great, I certainly do not have all the answers and I love to see the way others do things.

To that end I offering $100USD to the best improvement to this script, httpClient.sh. I have always wanted a pure BASH http client and thus I put this together last night. Its not perfect, some things are screaming to be fixed. In addition some other features should be added.

The rules:

  1. Your work MUST be original.
  2. Your work MUST be derived from the script above.
  3. It CAN NOT use netcat (nc).
  4. It MUST be submitted to me via email to this address: SpamDefeator.
  5. The script must contain, as comments, your name, email, and website (if you have one).
  6. It MUST be submitted by March 7th at midnight CST.
  7. Also NOTE that BASH must be compiled with networking support to use the script. I believe most distributions do this, Debian apparently does not.

I will judge based on the following in no particular order. There is no need to fix them all, the winner may just fix one, in a really cool way:

  • I would like to see it exit with an error status when the server responds with an error.
    Meaningful exit status?
  • Handling white space better.
  • Storing the headers in an array.
  • Following the HTTP 1.1 RFC more. (HEAD with meaningful exit codes?)
  • Adding an option to save headers to a file.
  • Sometimes 0 is printed after the HTML, not sure why.
  • f19 is printed before the HTML when grabbing google, not sure why.
  • User agent
  • I had to institute a timeout on read otherwise it took 3-5 seconds to timeout. I’d like this handled better.
  • I am not sure what happens when you download binary data or large files.
  • Better ways of handling things, novel practices, clean code, and of course my opinion.

Chris FA Johnson won with this version of httpClient.sh! Congratulations Chris!