bashlib

bashlib is a shell script that makes CGI programming in the bash shell easier, or at least more tolerable. It contains a few functions that get called automatically and place form elements (from POSTs and GETs) and cookies in your environment. It also contains complete documentation on how to use these variables and how to set cookies manually.

Recent bashlib News

The most recent version of bashlib will always be available from http://bashlib.sourceforge.net/src/bashlib-current.tar.gz and will be browsable at http://bashlib.sourceforge.net/src/bashlib-current/.

Version 0.4 (download or browse) of bashlib, released March 12, 2002, is fully autoconfiscated. To install it, run ./configure as usual. If you check out the CVS version, you’ll need autoconf installed to create the configure script from configure.in.

I released version 0.3 (download or browse)of bashlib on February 21, 2001. The only difference between 0.3 and 0.2 is that the tarball works. I’ve been testing it for a while, and it definitely seems stable enough for real use. The interface has changed a little; the sample below reflects these changes.

How this came about, and what it is useful for

Things such as this are born of necessity. My ISP, while being incredibly useful in most ways, doesn’t have a recent version of Perl available for their casual users (e.g., dial-up accounts, with web pages in http://www.isp.net/~mylogin/) — Perl is a “value-added” service. This was a great source of frustration for me, since I am a Perl programmer by day. So, I began rooting (not literally) around for other methods of writing CGI scripts, and realized that the shell, which I spent most of my time using, was not only a great generaly purpose scripting environment, but also a good one for CGI scripts. This naturally led me to forms and cookies, and here we are today.

No, I really don’t have this much free time. The current release is the result of about a days worth of work (on and off throughout the day), with some help. Well, that’s not quite true — 9 years of using Unix went into it as well.

Using bashlib

Using bashlib is pretty straight-forward. More important, however, is knowing what to do with the variables once they come into your script and knowing how to write CGI scripts. (This script is not running here, for obvious reasons.)

#!/bin/bash

# this sources bashlib into your current environment
. /usr/local/lib/bashlib

echo "Content-type: text/html"
echo ""

# OK, so we've sent the header... now send some content
echo "<html><title>Crack This Server</title><body>"

# print a "hello" if the username is filled out
username=`param username`
if [ -n "x$username" != "x" ] ; then
    echo "<h1>Hello, $username</h1>"

fi

echo "<h2>Users on `/bin/hostname`</h2>"
echo "<ul>"

# for each user in the passwd file, print their login and full name
# bold them if they are the current user
for user in $(cat /etc/passwd | awk -F: '{print $1 "t" $5}') ; do
    echo "<li>"
    if [ "$username" = "$user" ] ; then
        echo "<strong>$user</strong>"
    else
        echo "$user"
    fi
    echo "</li>"
done
echo "</ul>"
echo "</body></html>"

Other Resoruces

  • I recommend checking out bashish if you use bash often; it is dedicated to the configuration of bash. Although it isn’t helpful for CGI programming in bash, it makes day to day usage of bash quite nice. To quote:
  • Bashish is a theme engine for the console.

    It lets you customize title, prompt, background, foreground, colors, font and a lot of other things.
    Bashish is also very configurable, you can turn on and off nearly all features.

  • ^txt2regex$ is a Regular Expression “wizard”, all written with bash2 builtins, that converts human sentences to RegExs. with a simple interface, you just answer to questions and build your own RegEx for a large variety of programs, like awk, ed, emacs, grep, perl, php, procmail, python, sed and vim. there are more than 20 supported programs. it’s bash so download and run, no compilation needed.
  • For folks who do a lot of command-line-based web work, surfaw might be useful:
  • Surfraw (Shell Users’ Revolutionary Front Rage Against the Web) provides a Unix command line interface to a variety of popular Web search engines and sites, including Google, Altavista, Babelfish, Raging, DejaNews, Research Index, Yahoo!, WeatherNews, Slashdot, freshmeat, and many others.

  • The BASH Programming – Introduction HOW-TO and Advanced Bash-Scripting HOWTO: A guide to shell scripting, using Bash are very, very useful.
  • I just came across Prentice Hall’s 1996 book Portable Shell Programming, and it is wonderful. All the examples are pure Bourne shell, so they are (naturally) portable, and also very informative.
  • And, finally, while it’s not free, David Tansley’s Linux & Unix Shell Programming is a wonderful resource (get it from Amazon).

Something you don’t like?

OK, so there’s probably something in here you think should be different, could be better, etc. Well, drop me a line (email me at dlc at users dot sourceforge dot net) and let me know. You can write it out in words (“bashlib should uuencode GIFs on the fly”), provide a patch (via diff -u), or rewrite the whole thing (try to keep it in shell, though). All reasonable emails will be read, and probably answered as well.

Getting bashlib

bashlib is a pretty short script (as libraries go), and all versions can be browsed here. It is available for download from Sourceforge at http://bashlib.sourceforge.net/src/bashlib-0.4.tar.gz.

bashlib is also available from anonymous CVS. The CVS repository can be checked out through anonymous (pserver) CVS. When prompted for a password for anonymous, simply press the Enter key.

cvs -d:pserver:anonymous@cvs.bashlib.sourceforge.net:/cvsroot/bashlib login
cvs -z3 -d:pserver:anonymous@cvs.bashlib.sourceforge.net:/cvsroot/bashlib co bashlib
Advertisements
%d bloggers like this: