Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Script does not stop when doing a read

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 11-05-2007   -   Original Discussion by rooseter
rooseter rooseter is offline
Registered User
 
Join Date: Jul 2007
Last Activity: 8 July 2008, 2:34 AM EDT
Location: i live evrywhere
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
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
Sponsored Links
    #2  
Old Unix and Linux 11-05-2007   -   Original Discussion by rooseter
porter porter is offline Forum Advisor  
Registered User
 
Join Date: Jan 2007
Last Activity: 8 January 2008, 6:50 PM EST
Posts: 2,965
Thanks: 0
Thanked 5 Times in 5 Posts
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

Sponsored Links
    #3  
Old Unix and Linux 11-05-2007   -   Original Discussion by rooseter
rooseter rooseter is offline
Registered User
 
Join Date: Jul 2007
Last Activity: 8 July 2008, 2:34 AM EDT
Location: i live evrywhere
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
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  
Old Unix and Linux 11-05-2007   -   Original Discussion by rooseter
rooseter rooseter is offline
Registered User
 
Join Date: Jul 2007
Last Activity: 8 July 2008, 2:34 AM EDT
Location: i live evrywhere
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
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!
Sponsored Links
    #5  
Old Unix and Linux 11-05-2007   -   Original Discussion by rooseter
porter porter is offline Forum Advisor  
Registered User
 
Join Date: Jan 2007
Last Activity: 8 January 2008, 6:50 PM EST
Posts: 2,965
Thanks: 0
Thanked 5 Times in 5 Posts
Quote:
Originally Posted by rooseter View Post
porter, I have just tried using the TTY option, and it worked!
Cool.

Linux
Sponsored Links
    #6  
Old Unix and Linux 11-05-2007   -   Original Discussion by rooseter
aigles's Unix or Linux Image
aigles aigles is offline Forum Advisor  
Registered User
 
Join Date: Apr 2004
Last Activity: 1 December 2014, 9:03 AM EST
Location: Bordeaux, France
Posts: 1,711
Thanks: 2
Thanked 62 Times in 58 Posts
Another way :

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

Jean-Pierre.
Sponsored Links
    #7  
Old Unix and Linux 11-05-2007   -   Original Discussion by rooseter
Perderabo's Unix or Linux Image
Perderabo Perderabo is offline Forum Advisor  
Unix Daemon (Administrator Emeritus)
 
Join Date: Aug 2001
Last Activity: 26 February 2016, 12:31 PM EST
Location: Ashburn, Virginia
Posts: 9,930
Thanks: 64
Thanked 471 Times in 271 Posts
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

Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Stop child script by stoping parent script geovas Shell Programming and Scripting 0 11-21-2011 10:26 AM
read a string from its end to its start and stop when you find a specific character hakermania Shell Programming and Scripting 9 09-01-2010 12:44 AM
How to stop a script running in remote server from local script mannepalli Shell Programming and Scripting 1 03-04-2009 08:18 PM
script don't stop larne Shell Programming and Scripting 3 10-12-2008 10:07 AM
Script to run non-stop Raynon Shell Programming and Scripting 37 01-21-2008 05:36 AM



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