Num VAR's and Compact Code

Num VAR's and Compact Code

Post by Pete Cohe » Tue, 12 Mar 1996 04:00:00



Greetings from the Jersey shore....

I am new to shell scripting and would appreciate some feedback.

1) How to best test and validate for numeric input while excluding
   others.
2) How to break out cleanly from do while loop and continue
   execution.
3) Any recommendation for making this a more efficient compact
   script.

THANKS for the consideration...
                                    Pete...
#!/bin/sh
clear
EXFILE=spool/temp/exfile.$$
TMPFILE=spool/temp/tmpfile.$$
NUMFILE=spool/temp/numfile.$$
# Retrieve fax keys from ../expired
echo
fxstat -x | `grep "fax.-" | cut -c6-9 | sort -r > $EXFILE`
for X in `cat $EXFILE`
do
   flog -a | egrep -h $X | egrep '(MAXTRY  5|MODIFY  *)' | sed -n 1p

Quote:>>$TMPFILEdone

echo "The following FAX transmissions *DID NOT* go successfully."
echo "                                                                PO
Number"

echo
`cat $TMPFILE | grep -n ".*" | sed '/:/s/:/  /' > REFAX1`
`cat REFAX1 | cut -c0-1 | sort > $NUMFILE`
y=0
while y=0
do
  more REFAX1
  echo
  echo
  echo "Enter the LINE NUMBER of the FAX you want to SEND AGAIN:\c"
  read ANSWER
  ANSTEST=0
  `cat $NUMFILE | grep -n $ANSWER` > $ANSTEST
  if [ $ANSWER -eq $ANSTEST ]
  then
      y=1
  else
      echo
      echo "You didn't select a number. Do you need to try again? (Y/N):
\c"
      read ANSWER2
      if [ $ANSWER2 = "Y" -o $ANSWER2 = "y" ]
      then
         continue
      else
         y=1
         exit
      fi
  fi
done
cat REFAX1 |grep -h $ANSWER")" >REFAX2
FAXID=`cat REFAX2 | cut -d" " -f2`
PONUM=`flog -e $FAXID | cut -d" " -f14 | cut -c7-12`
echo
echo "You selected PO Number $PONUM to be faxed again"
echo
fx -x $FAXID
echo
echo "$PONUM has been queued to fax again"
echo
rm $EXFILE
rm $TMPFILE
rm $NUMFILE

 
 
 

Num VAR's and Compact Code

Post by Heiner Stev » Wed, 13 Mar 1996 04:00:00


 > I am new to shell scripting and would appreciate some feedback.

 > 1) How to best test and validate for numeric input while
 >    excluding others.

# Loop until user enters a valid number
while :
do
    echo "Enter a number:"
    read Number
    case "$Number" in
        *[!0-9]*) echo "this is no number: $Number";;
        *)        break;;       # Valid number - break out of while loop
    esac
done

 > 2) How to break out cleanly from do while loop and continue
 >    execution.

See example above.

 > 3) Any recommendation for making this a more efficient compact
 >    script.

I made some comments on the script. I neither have the commands
"fxstat" and "flog", nor do I know their output format, but I
tried my very best ;-)

 > #!/bin/sh
 > clear
 > EXFILE=spool/temp/exfile.$$
 > TMPFILE=spool/temp/tmpfile.$$
 > NUMFILE=spool/temp/numfile.$$
 > # Retrieve fax keys from ../expired
 > echo
 > fxstat -x | `grep "fax.-" | cut -c6-9 | sort -r > $EXFILE`

 > for X in `cat $EXFILE`
 > do
 >    flog -a | egrep -h $X | egrep '(MAXTRY  5|MODIFY  *)' | sed -n 1p
 > >>$TMPFILE
 > done

while read X
do
    flog -a | egrep "$X.*(MAXTRY  5| MODIFY  *)" | head -1
done < $EXFILE > $TMPFILE

 > echo "The following FAX transmissions *DID NOT* go successfully."
 > echo "                                                                PO
 > Number"
 > echo

 > `cat $TMPFILE | grep -n ".*" | sed '/:/s/:/  /' > REFAX1`
 > `cat REFAX1 | cut -c0-1 | sort > $NUMFILE`

The two lines above are not necessary (see below).

 > y=0
 > while y=0
 > do
 >   more REFAX1
 >   echo
 >   echo
 >   echo "Enter the LINE NUMBER of the FAX you want to SEND AGAIN:\c"
 >   read ANSWER
 >   ANSTEST=0
 >   `cat $NUMFILE | grep -n $ANSWER` > $ANSTEST
 >   if [ $ANSWER -eq $ANSTEST ]
 >   then
 >       y=1
 >   else
 >       echo
 >       echo "You didn't select a number. Do you need to try again? (Y/N):
 > \c"
 >       read ANSWER2
 >       if [ $ANSWER2 = "Y" -o $ANSWER2 = "y" ]
 >       then
 >          continue
 >       else
 >          y=1
 >          exit
 >       fi
 >   fi
 > done

If I got it right, the above loop presents a numbered list of the
lines in $TMPFILE. It then prompts for a valid line number from
this file and returns the number in $ANSWER.

This could be rewritten this way:

NumLines=`wc -l $TMPFILE`
# Read until valid number given
while :
do
    nl REFAX | ${PAGER:=more}   # Present numbered list

    echo "Enter the LINE NUMBER of the FAX (1-$NumLines): \c"
    read ANSWER
    case "$ANSWER" in
        *[!0-9]*)
            echo "this is no valid number: $ANSWER"
            continue;;          # Present choice again
    esac

    # $ANSWER is a positive integer. Check for valid range.
    if [ "$ANSWER" -gt "$NumLines" ]
    then
        echo "Please enter a line number from 1 to $NumLines"
        continue
    fi

    # $ANSWER is valid and in the range: exit loop and continue
    # with program
    break
done

 > cat REFAX1 |grep -h $ANSWER")" >REFAX2
 > FAXID=`cat REFAX2 | cut -d" " -f2`

I'm not too sure, but if you just want to get the selected line
$ANSWER from file REFAX1, you can use the following command:

FAXID=`sed -n "$ANSWER{p;q;}" REFAX1

 > PONUM=`flog -e $FAXID | cut -d" " -f14 | cut -c7-12`
 > echo
 > echo "You selected PO Number $PONUM to be faxed again"
 > echo
 > fx -x $FAXID
 > echo
 > echo "$PONUM has been queued to fax again"
 > echo
 > rm $EXFILE
 > rm $TMPFILE
 > rm $NUMFILE

Heiner
--

World - Europe - Germany - Bavaria - Nuernberg