SHELLdorado Newsletter 2/2001 - June 25, 2001

================================================================
The "SHELLdorado Newsletter" covers UNIX shell script related
topics. To subscribe to this newsletter, leave your e-mail
address at the SHELLdorado home page:

	http://www.shelldorado.com/

"Heiner's SHELLdorado" is a place for UNIX shell script
programmers providing

     Many shell script examples,
     shell scripting tips & tricks + more...
================================================================

Contents

 o  What's new at the SHELLdorado?
 o  Shell Tip: Arrays for Bourne shell
 o  Shell Tip: Differentiate between empty and unset variables
 o  Shell Tip: Log out idle users
 o  Shell Tip: extend PATH variable in .profile
 o  Q&A: How can I find the home page of a user?

-----------------------------------------------------------------
>> What's new at the SHELLdorado?
-----------------------------------------------------------------

 o  The SHELLdorado has moved! After being guest of the OASE
    shareware organization (http://www.oase-shareware.org) for
    more than two years, it now finally has it's own domain
    name:

    	http://www.shelldorado.com/
 
    The old address (http://www.oase-shareware.org/shell/) will
    be valid until the end of this year, so there is plenty of
    time to adjust old bookmarks.

 o  If you want to contribute to the "AWK Compatibility List",
    there now is an extended AWK feature test script (thanks to
    Raffaele Guido Della Valle <valle@rigel1.fci.unibo.it> for
    suggestions):

    	http://www.shelldorado.com/articles/awkcompat.html

    If you run an operating system not listed there, you may
    help complete the list. Just run the feature test script
    from the page, and send the resulting output to
    mailto:heiner.steven@shelldorado.com .

-----------------------------------------------------------------
>> Shell Tip: Arrays for Bourne shell
-----------------------------------------------------------------

    The Korn shell (and BASH) have built-in support for an array
    type. The following code fragment sets an array from the
    command line arguments, counts the number of array elements,
    and prints them (example does not work with BASH):

	#! /usr/bin/ksh

	# Set array "args" from command line arguments
	set -A args -- "$@"

	integer n=${#args[@]}	# number of array elements

	print "$n arguments:"
	integer i=0
	while ((i<n))
	do
	    print ${args[i]}
	    ((i=i+1))
	done

    The standard Bourne shell (/bin/sh) does not have arrays.
    Here is a way to simulate them by using "eval" to
    dynamically generate variable names: 

	# "arrays" for the bourne shell

	# Set the variables "var1", "var2", ... with the command line
	# arguments
	n=0
	for value
	do
	    n=`expr $n + 1`
	    eval var$n="'""$value""'"
	done

	# print the variable values

	echo "$n variables are set:"
	i=0
	while [ $i -lt $n ]
	do
	    i=`expr $i + 1`
	    eval echo '"$'"var$i"'"'
	done

    The quoting at first seems to be unnecessary complex, but it
    ensures, that variables can contain "whitespace" characters,
    and even a quotation mark ("). The exception to this rule is
    the single quote character ('), which will not be preserved.


-----------------------------------------------------------------
>> Shell Tip: Differentiate between empty and unset variables
-----------------------------------------------------------------

    If we want to find out if a variable is empty, we usually
    use a code fragment like the following:

    	if [ -z "$varname" ]
	then
	    echo "varname is empty"
	fi

    In the example above, we do not know if the variable did not
    exist because it was not set anywhere), or if it was set to
    an empty value (e.g. using "varname="). Sometimes it's
    useful to know the difference, e.g. if the script provides
    default values for unset variables, but allows the user to
    specify an empty value.

    In this case we could find out if a variable is empty or not
    set using the following scheme:
   
	#variable=
	if [ -z "$variable" ]
	then                    # variable is empty
	    if [ ${variable+EMPTY} = EMPTY ]
	    then echo variable is empty, but set
	    else echo variable is not set
	    fi
	else                    # variable contains value
	    echo "variable has a value"
	fi

    We use the feature that ${var+VALUE} returns "VALUE" if $var
    is set, even if it is set to an empty string.

    Refer to the "Parameters" section of the sh(1) or ksh(1)
    manual page for a description of other parameter
    substitutions.


-----------------------------------------------------------------
>> Shell Tip: Log out idle users
-----------------------------------------------------------------

    System administrators know the problem: users forget to log
    out, and leave sessions "hanging" and using system resources
    until the system reboots.

    Some shells have the feature of setting a timeout value: if
    the user did not enter anything for a specified time span
    (e.g. 60 minutes), the session is terminated automatically.

    The Korn Shell (/usr/bin/ksh) has the TMOUT variable, that
    can be set to the number of seconds idle time after which
    the session will terminate. A system administrator could
    want to set this variable e.g. in the system-wide startup
    file /etc/profile:

    	TMOUT=3600	# logout after one hour idle time

    The TCSH has the environment variable "autologout", which
    may be set to the number of idle minutes (not seconds)
    before the user should be logged out.


-----------------------------------------------------------------
>> Shell Tip: extend PATH variable in .profile
-----------------------------------------------------------------

    After login, most shell dialects read in the file
    $HOME/.profile (this does not apply to CSH and dialects).
    Usually this file is used to set or extend the PATH variable
    with additional directory names that the shell should search
    commands in, and the resulting code looks similar to the
    following:

    	PATH=$HOME/bin:$PATH:/usr/local/bin
	export PATH

    This is perfectly legal. In reality, the assignment tends to
    become rather large. Sometimes the PATH variable may contain
    a directory more than once, or even invalid directories.
    Since all directories of the PATH may be searched for each
    command executed, this could cost time.

    The following lines add directories to the PATH variable
    once, and check if the directory exists:

	OldPath=`echo "$PATH" | tr : ' '`

	# Special handling: a leading or trailing ":" means:
	# "current directory"

	PATH=`echo "$PATH" | sed -e 's/^://' -e 's/:$//'`
	PATH=:.:
	for p in $HOME/local/bin $HOME/cmds $HOME/bin \
		/opt/Summertime_98.sparc/bin \
		$OldPath
		/bin /usr/bin /usr/sbin /usr/local/bin
	do
	    case "$PATH" in
		*:${p}:*)       ;;	# Directory already in PATH
		*)
		    [ -d "$p" ] && PATH="$PATH$p:" ;;
	    esac
	done
	# Remove unnecessary colons at start and end:
	PATH=`echo "$PATH" | sed -e 's/^://' -e 's/:$//'`
	export PATH


-----------------------------------------------------------------
>> Q&A: How can I find the home page of a user?
-----------------------------------------------------------------

    The usual way to find a home page is to use search engines
    like Google (http://www.google.com) or Yahoo!
    (http://www.yahoo.com). But sometimes we already have some
    hints, e.g. an e-mail address, that could be used to make an
    "educated guess" at the home page.

    Have a look at the following (not completely fictional)
    e-mail address:

	heiner.steven@odn.de

    We now could assume, that "heiner.steven" is the name of the
    user on the system specified. The web home directory of the
    user is specified using "~heiner.steven" (note the leading
    "~" [tilde] character), resulting in the URL

	http://odn.de/~heiner.steven		# will not work!

    If this guess did not succeed, we could try some
    modifications on the user name, e.g. "hsteven", "heiners",
    and try to get the resulting web page name.

    If these pages did still not exist, we could try to
    systematically modify some other parts of the web address,
    e.g. the directory part ("/", "/homes/", ...), the domain
    name part ("people.odn.de", "www.odn.de",
    "homes.odn.de", ...).

    This method is successful for a surprising wide range of
    e-mail addresses. Some scripts using this method guessing
    home pages given an e-mail address are available at the
    SHELLdorado:

    	http://www.shelldorado.com/scripts/quickies/guesshomepage
    	http://www.shelldorado.com/scripts/quickies/findhomepage

	[additional scripts may be needed]
    
    The idea was first used by Jonathan Shake and Marc
    Langheinrich in the "Ahoy!" home page search engine at the
    University of Washington (they used additional methods to
    find the home page). Interested readers can read a paper on
    "Ahoy!" at the following URL:

	http://www.cs.washington.edu/homes/etzioni/papers/paper.html


----------------------------------------------------------------
If you want to comment on the newsletter, have suggestions for
new topics to be covered in one of the next issues, or even want
to submit an article of your own, send a mail to

	mailto:heiner.steven@shelldorado.com

================================================================
To unsubscribe send a mail with the body "unsubscribe" to
newsletter@shelldorado.com
================================================================