Script does not stop when doing a read


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script does not stop when doing a read
# 1  
Old 11-05-2007
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  
Old 11-05-2007
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  
Old 11-05-2007
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
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 11-05-2007
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  
Old 11-05-2007
Quote:
Originally Posted by rooseter
porter, I have just tried using the TTY option, and it worked!
Cool.

Smilie
# 6  
Old 11-05-2007
Another way :
Code:
exec 3<testdata
while read -u3 CurrDatad
do
   . . .
   read YESNO?"Continue to process? [y/n]: "
   . . .
done
exec 3<&-

Jean-Pierre.
# 7  
Old 11-05-2007
Quote:
Originally Posted by porter
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Read in numbers from console won't stop at newline.

Hello, I have snippet code from Lippman's <<C++ primer>>. The program is to convert regular decimal (0 ~ 15) numbers to basic hexdecimals. The instruction tells the program will execute by hitting newline at the end. When I tried to run the compiled program, hitting ENTER did not work as... (3 Replies)
Discussion started by: yifangt
3 Replies

2. Shell Programming and Scripting

Read command stop on either EOF or a specific line

I'm trying to stop reading a file until the end of the file is reached or a defined delimiter line is reached. Some how I need to test the fail state of the last 2 commands, not just the last command. echo -e "hello\ngoodbye\n####\ntesting" | while read line; ]; do echo "$line"; done hello... (4 Replies)
Discussion started by: Michael Stora
4 Replies

3. Shell Programming and Scripting

Loop to read parameters and stop

Hey guys, How do I make a loop that reads all the parameters en then stop when there are no parameters anymore ? Something that gives an output like this: ./Script.sh parameter1 parameter2 parameter3 parameter = parameter1 parameter = parameter2 parameter = parameter3 Thanks a lot,... (5 Replies)
Discussion started by: Miki1579
5 Replies

4. Shell Programming and Scripting

Stop child script by stoping parent script

Hi everyone, I have this problem with a script I'm writting. I want to execute a code running in the background several times through a script. I am writting it like that parent_script for a in 1 2 3 4 5 do exec test -n $a done What I want to do is when parent_script is killed,... (0 Replies)
Discussion started by: geovas
0 Replies

5. Shell Programming and Scripting

read a string from its end to its start and stop when you find a specific character

How can I do this? Actually I have a file which contains a path e.g. /home/john/Music/hello.mp3 and I want to take only the filename (hello.mp3) So, I need to read the file from its end to its start till the character "/" Is this possible? Thanks, I am sure you'll not disappoint me here! Oh,... (9 Replies)
Discussion started by: hakermania
9 Replies

6. Shell Programming and Scripting

How to stop a script running in remote server from local script

Hi, I have googled for quite some time and couldn't able to get what exactly I am looking for.. My query is "how to stop a shell script which is running inside a remote server, using a script"??? can any one give some suggestions to sort this out. (1 Reply)
Discussion started by: mannepalli
1 Replies

7. Shell Programming and Scripting

script don't stop

Hello everybody! I am new to this and I am trying to change a script in an open source program that plots some offset vectors and then calls a postscript viewer. I have commented away the call for the postscript viewer but somehow the script doesn't return to the shell prompt. I cant figure out... (3 Replies)
Discussion started by: larne
3 Replies

8. UNIX for Dummies Questions & Answers

Stop a shell script

Hi, I am writing a bash shell script. How can I tell it to stop. For example, I would like to have something similar to the following: mike=1 if ; then STOP THE SCRIPT fi (3 Replies)
Discussion started by: msb65
3 Replies

9. Shell Programming and Scripting

Script to run non-stop

Hi All, I am on a Solaris OS and i have come up with a csh script named " mycshscript " which will grab data from a datalog file & format the grabbed data & upload formated version to web server. I would want to have this script to run non-stop so that the latest can be captured since data is... (37 Replies)
Discussion started by: Raynon
37 Replies

10. UNIX for Dummies Questions & Answers

Script that doesn't stop

Hi all, I got a script that runs automatically using the cron file. The script starts running at midnight and suppose to delete image files from folders and sub-folders. The script ends when he finishes deleting or after 5 hours. My problem is that the script doesn't stop running even after 5... (3 Replies)
Discussion started by: biot
3 Replies
Login or Register to Ask a Question