Read statement within while read loop


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Read statement within while read loop
# 1  
Old 08-05-2011
Read statement within while read loop

hi,

this is my script
Code:
#!/bin/ksh
cat temp_file.dat | while read line
do
  read test
  if [[ $test == "1" ]]; then
     break
  else echo "ERROR"
  fi
done

when i execute this code , the script does wait for the user input . it directly prints "ERROR" and terminates after the no. of times as there are lines in the file. couls anyone help?!

Last edited by pludi; 08-05-2011 at 08:05 AM..
# 2  
Old 08-05-2011
Code:
#!/bin/ksh
cat temp_file.dat | while read line
do
  read test < /dev/tty
  if [[ $test == "1" ]]; then
     break
  else echo "ERROR"
  fi
done

# 3  
Old 08-05-2011
Beware of useless use of cat

https://secure.wikimedia.org/wikiped...ess_use_of_cat

Try this and save some overhead. While not really an impact in this situation, in a bigger program it will have a bigger impact and it is just a good habit to get into.

Code:
#!/bin/ksh 
while read line 
do   
  read test < /dev/tty   
  if [[ $test == "1" ]]; then      
    break   
  else echo "ERROR"   
  fi 
done < temp_file.dat

# 4  
Old 08-12-2011
Thanks Gary and Ibrahim...
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Solved] While read line and if statement not working

I'm looking for some help in figuring why my little bit of code will not process any entries other then the first one in my list. while read line ;do hostname=${line//\"} a=`ssh user@$hostname uptime;echo $?` if ];then dt=`date` touch... (6 Replies)
Discussion started by: whegra
6 Replies

2. Shell Programming and Scripting

FORTRAN read statement

Hi, I've been having trouble figuring out what the following read statement is doing. DO I = 1, natom k = 3 * (i - 1) + 1 READ(8,*) AtNum(I), (X_2(J),J=k,k+2) END DO The part I don't understand is: (X_2(J),J=k,k+2) the file it is reading is simply a set of... (8 Replies)
Discussion started by: butson
8 Replies

3. Shell Programming and Scripting

Read statement not working

hello guys, i am having the below piece of code error () { echo"Press y /n" read ans case $ans in y) main;; n) exit esac } In the abve code, read statement is not working i.e not waiting for user to enter input. ,i tested exit status its 1. could anyone help me to do this ... (11 Replies)
Discussion started by: mohanalakshmi
11 Replies

4. Shell Programming and Scripting

awk doesn't understand 'read' statement!!!

While working on awk programming, i found that it doesn't understand 'read' statement. Then what's the use of 'continue' and 'break' statement in awk. For ex: awk '{k=1; while (k<10) {print $0; k++}}' emp.lst Now, please say if I want to put the logic that after priting 1 line, it will ask for... (13 Replies)
Discussion started by: ravisingh
13 Replies

5. Shell Programming and Scripting

Read SQL statement in Script

Hi Guys.. need some urgent help... I am stuck in something badly I need to write a script which would read a sql statement (which might be a join/inner join/select/sub select etc. ) I need to read that sql statement ... and in the output I want all the table names and columns (doesn't... (4 Replies)
Discussion started by: freakygs
4 Replies

6. Shell Programming and Scripting

Read from user inside a while statement

Hi there I want to ask to a user something about each line of a file. This is my code: #cat test.txt first line second line third line #cat test.sh #!/bin/ksh read_write () { echo "Do you want to print the line (YES/NO)?\n" read TEST case ${TEST} in YES) return 0;; ... (3 Replies)
Discussion started by: tirkha
3 Replies

7. Shell Programming and Scripting

Read statement not working in a script

I have a script consisting of certain functions whose input is a file at same location. In that file i have written the name of anothe file at same location. The third file contains a word which act as a function in the first script.Let me give an example i have a scrip file say 1.sh in which i am... (7 Replies)
Discussion started by: sumitdua
7 Replies

8. Shell Programming and Scripting

read statement not working in a function

Pls this is emergency.I have written a script which is taking input from another script. and the contents of my second script are acting as functions to my main script.Now the problem is that in one of the functions i want the script ececution to stop and start when user enters any character r... (2 Replies)
Discussion started by: sumitdua
2 Replies

9. Shell Programming and Scripting

how to read a file to an echo statement

I was searching for an option where i can echo some strings together with the contents of a file. Eg. I was to echo the below string to a file "alter application PMS_ add variable CurrYear Y2009;" >> ${ESS_MAXL_DIR}/PMS_SUB.txt The value 2009 is coming from a file called date.txt without the... (3 Replies)
Discussion started by: Celvin VK
3 Replies

10. Shell Programming and Scripting

while read loop w/ a nested if statement - doesn't treat each entry individually

Hi - Trying to take a list of ldap suffixes in a file, run an ldapsearch command on them, then run a grep command to see if it's a match, if not, then flag that and send an email alert. The list file (ldaplist) would look like - *********** o=company a o=company b *********** **... (7 Replies)
Discussion started by: littlefrog
7 Replies
Login or Register to Ask a Question