listempty

[last updated - 27 July 2003]

This is a QC'ing tool. You go to the output directory with your tables or listings in it and type in something like "listempty *.lst" and it will show you a list of all the files that fit this bill that are empty. I'll go straight into the code of this utility and then explain any features you might not have seen before. Copy and paste the code into a member called "listempty" in your shell script library.

#!/bin/sh
# Script     : listempty
# Version    : 1.0
# Author     : Roland Rashleigh-Berry
# Date       : 27 July 2003
# Contact    : roland@rashleigh-berry.fsnet.co.uk
# Purpose    : To list "empty" files 
# SubScripts : none
# Notes      : You can use this to check that all your output listings contain
#              something.
# Usage      : listempty *.lst
#              
#================================================================================
# PARAMETERS:
#-pos- -------------------------------description--------------------------------
#  1   File or list of files to check that they contain something
#================================================================================
# AMENDMENT HISTORY:
# init --date-- mod-id ----------------------description-------------------------
# 
#================================================================================
# This is public domain software. No guarantee as to suitability or accuracy is
# given or implied. User uses this code entirely at their own risk.
#================================================================================

if [ $# -lt 1 ] ; then
  echo "Usage: listempty *.lst" 1>&2
  exit 1
fi

while [ $# -gt 0 ]
do
  if [ ! -s $1 ]
  then
    echo $1
  fi
  shift
done

The header and check for number of parameters and writing a usage message to standard error I assume you are familiar with. If not then please refer to the previous examples as I will only explain what follows it. The rest of the code might be a puzzle, though. See the "shift" command in the "do...done" loop? This is another way of performing a loop. I'll explain...

I'm going to explain about shift. When you type in a command like "listempty *.lst" then the script does not see the parameter as you typed it. Instead of the *.lst you typed in it sees what the command interpreter gives it. This scans your command line before anything else is allowed to see it. The command interpreter sees *.lst and matches every file it can to it and turns it into a list. So instead of *.lst the command interpreter might translate it into something like aa.lst bb.lst cc.lst dd.lst etc., depending on what files in that directory match the pattern you specified, and this is what your shell script receives. Now think of the command you type in as parameter 0 and that you can refer to its name as $0. The first of the file names will be parameter 1 and you refer to its contents as $1. The next parameter is 2 and you refer to it as $2. And so on up to parameter 9 where the rest of the parameters are all lumped together. Now what you can do is shift the parameters along. When you shift parameters then $1 gets lost and $2 becomes $1. You can keep doing this until none are left. Have a look at the above code again. You see my test while [ $# -gt 0 ]?. This is testing if the number of parameters left are still greater than zero. In other words whether I have one or more parameter values left to work on. This will be a file name. So then I test whether the file contains something using if [ ! -s $1 ] which you can think of as "if not something in file named to parameter 1" and if this condition is met it echoes the filename to standard output. Then it has done its work for that file so it makes the next filename available in position one by shifting everything along one. And it keeps doing this until nothing is left.

I've used shift in this script but you would more often see it used in a script where your first one or two parameters were not file names but your third parameter was. You'd then assign the parameter values that are not file names to variables and then shift until the first file name was at position 1. You would then use the loop like the above that shifts one along each time you are finished with a file until you were done with all of them.

Go back to the home page.

E-mail the macro and web site author.