read is not on hold


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting read is not on hold
# 1  
Old 09-04-2012
read is not on hold

In end of https://www.unix.com/shell-programmin...les-shell.html

mjd_tech gives script which can read some values directly without manually input, but if no value is the right one, my understand is, it will on hold for waiting the next input, but when I run below codes, it keeps running and refresh my screen.

Code:
while [ "$line" != "Fred" ]; do
    read line
    echo $line
done << EOF
Wilma
Barney
#Fred
Betty
EOF

Why does it behave like that?
# 2  
Old 09-04-2012
Quote:
my understand is, it will on hold for waiting the next input
It won't. You feed the while loop with the content of here-document, so it processes it line-by-line.
Read builtin takes input from a file descriptor. A file descriptor is either an open file, and it takes a line from there, or stdin, and it waits for input from terminal. In your case it's a file (you are creating a here-document with << operator), that's where read is reading from, and it wouldn't make sense for it to wait for stdin.
# 3  
Old 09-04-2012
Thanks, so after read command get EOF , what will happen then?

Will EOF tell the read command, the here-document is finished, no read from now on, why read still keep reading with "null" value.
# 4  
Old 09-04-2012
Hi


It behaves like that becuause there is no condition which will make the read statement to stop reading. Even after the here document is complete, read tries to read from the stdin, does not find any, goes over to the while loop condition which is true, reads again, and this cycle goes on.

For your script to go ahead, you have to make the while loop break when there is no more input:

Code:
while [ "$line" != "Fred" ]; do
    read line
    [ -z "$line" ] && break
    echo $line
done <<EOF
Wilma
Barney
Frea
Betty
EOF

Guru
This User Gave Thanks to guruprasadpr For This Post:
# 5  
Old 09-04-2012
Quote:
Originally Posted by guruprasadpr
Hi


It behaves like that becuause there is no condition which will make the read statement to stop reading. Even after the here document is complete, read tries to read from the stdin, does not find any, goes over to the while loop condition which is true, reads again, and this cycle goes on.

Guru
So if follow your explain, why read is not on hold to wait the input?
# 6  
Old 09-04-2012
Hi

When 'read' reads from standard input, it holds, does not hold when it is reading from a file.

Try this, you will understand:
Code:
$ cat file
Wilma
Barney


Code:
$ while [ 1 ]
> do
>  read line
>  echo $line
> done < file

Guru.
# 7  
Old 09-04-2012
As others mentioned, read reads lines from standard input. In this case, standard input is coming from the here-document. Hence, read will always read from the here-doc. read will wait for the input (until a time either specified by TMOUT or that overridden by -t option) provided the source (in this case, the here-doc) has not been "exhausted". In your case, after printing out Betty, the here-doc has been "exhausted" and read tries to read from this source and reads a null value with an exit status of 1 and since you have not specified any exit condition for the loop for this case, the loop just prints out the null value of line.

Last edited by elixir_sinari; 09-04-2012 at 03:26 AM..
This User Gave Thanks to elixir_sinari For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Variable hold in UNIX job

In log directory file contain log files and files contain some unique job name which is dynamically created (example=55555 ),if job get 55555 then send alert message, but after next run if it find ---55555 then no need send alert message While checking the first job name you are capturing the Job... (3 Replies)
Discussion started by: Kalia
3 Replies

2. Shell Programming and Scripting

Hold buffer in sed

Hi Experts, i have a file like below **** table name is xyz row count for previous day 10 row count for today 20 diff between previous and today 10 scan result PASSED **** table name is abc row count for previous day 90 row count for today 35 diff between previous and today 55... (4 Replies)
Discussion started by: Lakshman_Gupta
4 Replies

3. Shell Programming and Scripting

Hold, Replace and Print with sed

Hi, I'm a newbie with scripting so I'd appreciate any help. I have a file import.txt with below text AA_IDNo=IDNoHere AA_Name=NameHere AA_Address=AddressHere AA_Telephone=TelephoneHere AA_Sex=SexHere AA_Birthday=BirthdayHere What I need is that the Lines for Name, Address and... (3 Replies)
Discussion started by: heretolearn
3 Replies

4. Shell Programming and Scripting

Hold buffer in perl

Hi, Can any one tell me is their any "hold buffer" in perl similar to sed. I have to find a pattern, once that pattern found then need to go backward and find another pattern and print. Example: Below are the contents present in a file ## block IPs URLs URL_IPs Unblock URLs ... (4 Replies)
Discussion started by: Anjan1
4 Replies

5. Shell Programming and Scripting

sed: hold buffer question

I've been using sed to help with reformatting some html content into latex slides using the beamer class. Since I'm new to sed, I've been reading a lot about it but I'm stuck on this one problem. I have text that looks like this: ******************* line of text that needs to be... (4 Replies)
Discussion started by: tfrei
4 Replies

6. Shell Programming and Scripting

What's the max integer a variable can hold?

I would like to know the maximum integer that a variable can hold. Actually one of my variable holds value 2231599773 and hence the script fails to process it.Do we have any other data type or options available to handle this long integers? (9 Replies)
Discussion started by: michaelrozar17
9 Replies

7. Shell Programming and Scripting

Hold previous date

A file named abc.txt being updated with date value by one process. Say , today it s updating the file with value 01-09-2009 Fine. Tomorrow the process will override the file with tomorrow date (02-09-2009) .Insome case the process will overriding the file with empty string. So I... (1 Reply)
Discussion started by: Gopal_Engg
1 Replies

8. UNIX for Dummies Questions & Answers

Determining which processes hold a semaphore

I have a situation where I have created a semaphore and set it's value to 10. I am using this semaphore to control access to a shared memory location. I can have 10 processes simultaneously read from the shared memory location, process 11 would get locked out. My question is, is there a way I... (6 Replies)
Discussion started by: tpotter01
6 Replies

9. UNIX for Advanced & Expert Users

MAX SIZE ARRAY Can Hold it

Hi, Do anyone know what's the max size of array (in awk) can be store before hit any memory issue. Regards (3 Replies)
Discussion started by: epall
3 Replies

10. UNIX for Dummies Questions & Answers

Support Needed: Education On Hold

I'm Presently Writting A College Report On Operating Systems, Not Enjoying It Very Much. I Was Hoping Someone Could Direct Me To A Site Where I Could Get Information Such As The Role Of Operating Systems, Types Of Operating Systems (Multi-User Multi-Tasking etc), Modes Of Operating systems... (3 Replies)
Discussion started by: OSNovice
3 Replies
Login or Register to Ask a Question