Script does not stop when doing a read

Hi Folks,

I have been trying to create a script wherein after it reads a certain number of data, it will pause and ask the user if he wants to continue or not. However, it seems that when it is supposed to read the user's answer, the script will go into a loop. What is wrong with my script here?

#!/usr/bin/ksh

trap exit 1 2 3 15

DataCtr=1
MaxData=10
while read CurrData
do
   echo "Data# $CurrData"
   if [[ $DataCtr -eq $MaxData  ]] ; then
      echo " "
      echo "Data are almost done."
     while : 
     do
       echo "Continue to process? [y/n]: \c"
       read YESNO
       case "$YESNO" in
         [yY]|[yY][eE][sS])
            YESNO=y ; break ;;
         [nN]|[nN][oO])
            exit ;;
         *)
            YESNO="" ;;
       esac
     done
   fi
   echo "Processing data $CurrData"
   DataCtr=`expr $DataCtr + 1`
done < testdata
echo "Finished!"

My testdata file will just contain text data.

Thanks in advance for your help!

  1. what does "while :" mean?

  2. everything inside the while/read/do/done will use testdata as stdin, including the "read YESNO", you would need to do

TTY=`tty`

while read ...
do
...
         read YESNO <$TTY
...
done <testdata

Hi porter,

"while :" would mean to do a loop of the whole while - do block. On this case, it is supposed to echo the question until the user either answers a Y (which on this case will execute the break command) and a N (which will exit the script).

If you put the inner while-do block outside of the outer while-do, the routine will work. It is only when it is inside the outer while-do that the routine goes into loop.

porter, I have just tried using the TTY option, and it worked! I am just wondering why my current script will not work when on another script, it did (although it is placed outside of another while-do loop).

Anyway, thanks for your help on this!

Cool.

:slight_smile:

Another way :

exec 3<testdata
while read -u3 CurrDatad
do
   . . .
   read YESNO?"Continue to process? [y/n]: "
   . . .
done
exec 3<&-

Jean-Pierre.

: is a built-in command that does nothing. It does parse its arguments and it sets the exit code. Originally it was intended for comments but it has too many side effects for that and the # comment style was copied from csh. In addition to the infinite loop construct you sometimes see it in "if" constructs:

if some-condition ; then
     :
else
     echo something
fi

The colon ":" as a NULL command can be quite useful for debug output. A line that begins with ":" is not executed, but it is evaluated. So instead of this:

echo "The value of VAL is '$VAL'."

do this:

: The value of VAL is '$VAL'.

The latter will only show when "set -x" is used, and only once; the former show up twice in the debug output.

BUT, since it is evaluated, care must be taken. For instance:

: This won't work!

The apostophe in "won't" is evaluated and treated like an unmatched single-quote.