Written by Phil Braham. Realtime Software Pty Ltd. Released under GNU license. Free to use. Please pass any modifications or comments to the author Phil Braham: realtime@mpx.com.au =============================================================================== To make: make shlist Copy shlist to /usr/bin (you will require root access for this). =============================================================================== Usage: shlist [-vrRslLF] [-n ] [-o |-c ] [|-] shlist [-vrRslLF] [-n ] [-o |-c ] [-f] [|-] shlist allows the use of lists from a shell. Lists take the form of a list name and an index, which may be a name. This may be considered as list[index], eg: employee[name] -- employee[j.blogs]. The list is actually a directory under the list root directory (see note below) and the indexes are filenames. Thus lists keep their contents across reboots. -r - reset the index. (Deletes the entry). -R - reset the list. (Deletes the directory supplied with -n or LIST_NAME variable). -s - supress the display of the current value. -l - list the indexes -L - list the indexes and their values -F - when displaying, displays the name of the file containing the value -f - requires a filename. The file is copied as the index file. -n - name of list. See note below for defaults. -o - index into array. Default is 0. -c - denotes that stdin is to be used and the number is the column (or field) number to use for the index. Column 0 is the first column. -v - writes the version number to stderr. Value is the (optional) value put into the index. If a - is supplied then the value is taken from stdin. The input will be read until terminated by an EOF allowing the output of a program to be piped through shlist. Note that most characters can be used in an index but any / characters will be converted to | characters. This character, and some others, may need to be prefixed by a \ (\|). The current value is always returned (unless the -s flag is supplied). If a value is supplied then it is put into the index. If the -r flag is set the file (the index entry) is deleted. When a deleted value is requested, shlist will return a value of ERR_EXIT_NOT_SET. -r with a value is illegal as is -l or -R with any flag or a value or -c and -o. The following variables may be set: LIST_ROOT Root directory. Default is .listRoot in your home directory LIST_NAME List name. Default is list Note: If LIST_ROOT is not a full path then the directory will be created from the current directory. Possible return values from shlist are as follows: No error 0 - Good return ERR_EXIT_NOT_SET 1 - Attempted to retrieve a value that was not set ERR_EXIT_OFFSET -1 - Unable to create index file ERR_EXIT_ARGS -2 - Incorrect parameters supplied ERR_EXIT_OTHER -3 - Other error In addition, errors may be passed back from creating the root or list directories Returned data is sent to stdout. Errors are sent to stderr. Arrays are stored in a directory structure as follows: ROOT-DIR ---Directory | list1 list2 list... ---Directory | | | val1 valx etc ---Files (index). Each file contains val2 valy the value of the variable The root dir is set in LIST_ROOT. Default is ~/.listRoot if variable is not set. Note that a . is always prefixed to the root list name if not already present. As the value for each reference is stored in a file, there is no limit to the value that may stored. shlist with no parameters returns the index of 0: $ shlist -o 0 "This is the default value" $ shlist This is the default value$ Example usage: $ # $ # Setup a phone number database $ # $ #### Either: $ export LIST_NAME="PhoneNumbers" # Uses a shell variable to define list name $ alias phone='shlist -o' $ #### OR: $ alias phone='shlist -n PhoneNumbers -o' # Define list name on command line $ #### $ # $ # Add phone numbers to the db $ # $ phone john "07 5521 8723" $ phone peter "03 8734 8723" $ # $ # Retrieve a phone number $ # $ phone john 07 5521 8723$ $ # $ # Put the phone number into a variable $ # $ johnPn=$(phone john) $ echo $johnPh 07 5521 8723 $ shlist.sh is a wrapper that takes each input line and passes it to shlist. This enables you to create a number of entries from a file or from the output of a file. Piping results through shlist.sh: Example using ls: $ ls -l --color='no' / | tail -n+2 | shlist -s -n rootdir -c 7 - $ shlist -n rootdir -l bin "drwxr-xr-x 2 root root 2304 2003-04-16 21:11 bin" dev "drwxr-xr-x 28 root root 94840 2003-05-10 22:48 dev" etc "drwxr-xr-x 60 root root 6856 2003-05-10 22:49 etc" lib "drwxr-xr-x 7 root root 2720 2003-04-17 11:28 lib" mnt "drwxr-xr-x 3 root root 72 2003-04-16 21:58 mnt" opt "drwxr-xr-x 11 root root 272 2003-04-17 11:20 opt" srv "drwxr-xr-x 4 root root 96 2003-04-16 20:53 srv" tmp "drwxrwxrwt 20 root root 1232 2003-05-11 04:06 tmp" var "drwxr-xr-x 17 root root 432 2003-04-17 11:07 var" usr "drwxr-xr-x 12 root root 344 2003-04-16 21:12 usr" boot "drwxr-xr-x 4 root root 1024 2003-04-17 11:28 boot" home "drwxr-xr-x 3 root root 72 2003-04-16 21:24 home" proc "dr-xr-xr-x 85 root root 0 2003-05-11 08:48 proc" sbin "drwxr-xr-x 3 root root 7064 2003-04-17 11:12 sbin" root "drwx------ 13 root root 696 2003-05-10 19:26 root" media "drwxr-xr-x 5 root root 120 2003-04-16 20:53 media" windows "drwxr-xr-x 3 root root 72 2003-04-16 20:53 windows" $ $ shlist -n rootdir -o boot drwxr-xr-x 4 root root 1024 2003-04-17 11:28 boot$ Example using df: $ df | tail -n+2 | ./shlist -n df -c5 -s $ shlist -n df -l | "/dev/hda3 4457860 2999612 1458248 68% /" |boot "/dev/hda5 15522 4624 10097 32% /boot" |windows|C "/dev/hda1 10241404 8402644 1838760 83% /windows/C" |dev|shm "shmfs 123736 0 123736 0% /dev/shm" $ $ shlist -n df -o \|boot /dev/hda5 15522 4624 10097 32% /boot$ Note that in the index names /, /boot, /windows/C, etc the / character has been replaced by a |. This is because filenames cannot contain a / character. Note also that when these index names are used on the command line, the | must be prefixed by a \ character to prevent the shell from attempting to interperate it.