Comment, comment, comment

Comments are very popular to ignore. I often want to do something I've done before so consult an old script. I often can't figure out what, why, or how I did something in an old script because I didn't bother to comment. With notable exceptions, comments are lines that begin with the pound sign: #.

The notable exception is:
#!/bin/csh

(pronounced pound, bang, slash bin, slash sea-shell) or it could be
#!/usr/local/bin/tcsh

(pronounced pound, bang, user local bin tee-shell) where in these cases, we're declaring what shell our script is using.

It also helps to use spaces and blank lines to make your script readable. While it may take more paper if you print it, it only takes two bytes to make new line or a space. Consider the two line below. I realize that both may be unreadable at this point, but which looks better?


#!/bin/csh
set outdir=../sac/MCMT
foreach myfile (`ls */*.gz`)
echo working on $myfile
set thisfile=`/bin/basename $myfile`
set thisdir=`/bin/dirname $myfile`
set sta=`echo $thisfile|awk -F. '{print $1}'`
set cha=`echo $thisfile|awk -F. '{print $2}'`
set net=`echo $thisfile|awk -F. '{print $3}'`
echo "will workone $thisdir/stdin.1.sac and move to $outdir/$sta.$cha.$net.$thisdir"
gunzip -cd $myfile|ah2sac
mv stdin.1.sac $outdir/$sta.$cha.$net.$thisdir
end

#!/bin/csh
# convert a bunch of ah files to sac and use more
# comments than I ever do

#
# where the sac files go
#
set outdir=../sac/MCMT

#
# loop over all the compressed file in every directory
#
foreach myfile (`ls */*.gz`)

  echo working on $myfile

  #
  # strip off the file name
  #
  set thisfile = `/bin/basename $myfile`

  #
  # strip off the directory name
  #
  set thisdir = `/bin/dirname $myfile`

  #
  # get the station, channel, and net from the file name
  #
  set sta = `echo $thisfile | awk -F. '{print $1}'`
  set cha = `echo $thisfile | awk -F. '{print $2}'`
  set net = `echo $thisfile | awk -F. '{print $3}'`

  #
  # tell me what I'm going to do, unzip it and pipe through ah2sac,
  # then put it in the right place
  #
  echo "will work on $thisdir/stdin.1.sac and move to $outdir/$sta.$cha.$net.$thisdir"
  gunzip -cd $myfile | ah2sac
  mv stdin.1.sac $outdir/$sta.$cha.$net.$thisdir

end