The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
stop execution of script with in the script dsdev_123 AIX 3 03-20-2008 11:57 AM
Script to run non-stop Raynon Shell Programming and Scripting 37 01-21-2008 05:36 AM
Script that doesn't stop biot UNIX for Dummies Questions & Answers 3 11-30-2007 01:19 AM
stop Prstat using shell script filthymonk Shell Programming and Scripting 19 05-23-2007 06:24 AM
Start/Stop Script jjv1 UNIX for Dummies Questions & Answers 2 12-16-2003 04:28 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-05-2007
rooseter rooseter is offline
Registered User
  
 

Join Date: Jul 2007
Location: i live evrywhere
Posts: 6
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?


Code:
#!/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!

Last edited by rooseter; 11-05-2007 at 03:40 AM.. Reason: changed continue to break
  #2 (permalink)  
Old 11-05-2007
porter porter is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2007
Posts: 2,965
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


Code:
TTY=`tty`

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

  #3 (permalink)  
Old 11-05-2007
rooseter rooseter is offline
Registered User
  
 

Join Date: Jul 2007
Location: i live evrywhere
Posts: 6
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.

Quote:
Originally Posted by porter View Post
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


Code:
TTY=`tty`

while read ...
do
...
         read YESNO <$TTY
...
done <testdata
  #4 (permalink)  
Old 11-05-2007
rooseter rooseter is offline
Registered User
  
 

Join Date: Jul 2007
Location: i live evrywhere
Posts: 6
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!
  #5 (permalink)  
Old 11-05-2007
porter porter is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2007
Posts: 2,965
Quote:
Originally Posted by rooseter View Post
porter, I have just tried using the TTY option, and it worked!
Cool.

  #6 (permalink)  
Old 11-05-2007
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,433
Another way :

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

Jean-Pierre.
  #7 (permalink)  
Old 11-05-2007
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,131
Quote:
Originally Posted by porter View Post
1. what does "while :" mean?
: 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:

Code:
if some-condition ; then
     :
else
     echo something
fi

Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:18 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0