TCLUG Archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [TCLUG:2223] How to debug




Darn... someone beat me to answering.. I was still typing..
Here is a summary..

Normally what you would do is:
    1. Reformat the script to have easy to read spacing
       (making sure the if, elif,else,fi match up).
    2. #!/bin/sh -x
	- this echos everything as it is set..
	(or set -x)

However, there are 3 problems:

 1. the "; then" is missing.
    The line:
	elif [ -z $RECURE2 ]
    should be:
	elif [ -z $RECURE2 ] ; then

 2. The quotes are missing and should not be used
    multiple times in the same line.

    And the lines:
	if [ -z $RECURE1 ]; then
	NEWFLAGS="$NEWFLAGS `echo $FLAGS |sed -e "s/r//"`"
	elif [ -z $RECURE2 ]
	NEWFLAGS="$NEWFLAGS `echo $FLAGS |sed -e "s/R//"`
    Should be:
	if [ -z $RECURE1 ]; then
	NEWFLAGS="$NEWFLAGS `echo $FLAGS |sed -e 's/r//'`"
	elif [ -z $RECURE2 ] ; then
	NEWFLAGS="$NEWFLAGS `echo $FLAGS |sed -e 's/R//'`"

	Note the duplicate use of '"'

 3. it is 'safer' to use "${VARIABLE}" then just $VARIABLE.
     Changed:
	if [ -z $DASH ]; then
	if [ -z $RECURE1 ]; then
	elif [ -z $RECURE2 ] ; then
     to:
        if [ -z "${DASH} ]; then
        if [ -z "${RECURE1}" ]; then
        elif [ -z "${RECURE2}" ] ; then

     Actually it is easiest to do this by habit to all.

The new script would be:
#!/bin/sh

for FLAGS in $*
do
  FLAGS=`echo ${FLAGS} |sed -e "s/--/dumbgrep/"`
  DASH=`echo ${FLAGS} |grep dumbgrep`

  if [ -z ${DASH} ]; then
    echo double dash

    if [ ${FLAGS} = --recursive ]; then
      echo --recursive
    else
      FLAGS=`echo ${FLAGS} |sed -e "s/dumbgrep/--/"`
      NEWFLAGS="${NEWFLAGS} ${FLAGS}"
    fi

  else
    echo no double
    RECURE1=`echo ${FLAGS} |grep - |grep r`
    RECURE2=`echo ${FLAGS} |grep - |grep R`

    if [ -z "${RECURE1}" ]; then
      NEWFLAGS="${NEWFLAGS} `echo ${FLAGS} |sed -e 's/r//'`"
    elif [ -z "${RECURE2}" ] ; then
      NEWFLAGS="${NEWFLAGS} `echo ${FLAGS} |sed -e 's/R//'`"

    else
      echo good
      NEWFLAGS="${NEWFLAGS} ${FLAGS}"
    fi
fi
done

#if ( -d /home/delete/${LOGNAME} ) then
  echo mv ${NEWFLAGS} /home/delete/${LOGNAME}

#else
  echo mkdir /home/delete/${LOGNAME}
  echo mv ${NEWFLAGS} /home/delete/${LOGNAME}
#fi