Please explain read in a while loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Please explain read in a while loop
# 1  
Old 01-11-2006
Please explain read in a while loop

I have a script which tries to read input from a user for every value read from a file. The input file is

Code:
#> more testfile
TEST1 | D200 | 12345601 | | ABC company | m
TEST2 | D201 | 12345602 | | ABC company | m

The script test.sh is as follows

Code:
while read line
do
read test?"Enter a Value:"
done < testfile

however when executed, shell never responds to a the read command.

Code:
#>test.sh
#>

Is there a special read option that I have to use when using read within a while loop? Any help is greatly appreciated. Thanks

Jerardfjay Smilie
# 2  
Old 01-11-2006
Code:
count=`wc -l file1 | tr -s " " | cut -d" " -f 2`
echo $count

while test $count -gt 0
do
    echo "Enter input :"
    read input
    echo $input
    ((count=count-1))
done


Last edited by bhargav; 01-11-2006 at 04:48 PM..
# 3  
Old 01-11-2006
Quote:
Originally Posted by bhargav
Code:
count=`wc -l file1 | tr -s " " | cut -d" " -f 2`
echo $count

while test $count -gt 0
do
    echo "Enter input :"
    read input
    echo $input
    ((count=count-1))
done

Bhargav,

I need to read input from user for every line that I read from the testfile. How would I accomplish that. Your example does not involve the loop reading from a file as well as reading user input within the loop of reading from a file. I use the $line variable within the loop to extract data that I require for other purposes.

Code:
while read line
do
    var1=$(echo "${line}" | awk -F "|" '{print $2}' | sed -e 's/^ *//g;s/ *$//g')
    read input?"Enter value :"      <<<---- This read does not work
    echo $input
#    do some processing with $var1 and $input and continue 
done < testfile

Any thoughts?
Jerardfjay

Last edited by jerardfjay; 01-11-2006 at 05:27 PM.. Reason: clarification of reply
# 4  
Old 01-11-2006
Code:
 while read line
do
read test?"Enter a Value:"
done < testfile


In the above code, in the first line, it says read line.. it reads the first line from the testfile and puts it into the variable called line, again when you say read test, it actually reads the second line from testfile and puts it into the variable test.. it won't take the input from the keyboard because you have given the input as testfile by specifying the "<".

use this code

#!/usr/bin/ksh

IFS="
"

for line in $(< testfile)
do
echo $line
echo "Enter value :"
read a
done

Last edited by mahendramahendr; 01-11-2006 at 05:44 PM..
# 5  
Old 01-11-2006
Or do this:
Code:
while read line
do
read test?"Enter a Value:"  < /dev/tty
done < testfile

# 6  
Old 01-12-2006
Thanks

Thanks mahendramahendr and Perderabo. Both of your code works. However I prefer perderabo's snippet since I dont have to mess around with IFS. Thanks for your valuable input.

Regards
Jerardfjay
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Please explain AWK Fibonnaci for loop

Referring to this: #!/bin/awk -f BEGIN{ for(i=0;i<=10;i++) { if (i <=1 ) { x=0; y=1; print i; } else { z=x+y; print z; x=y; y=z; } } (3 Replies)
Discussion started by: p1ne
3 Replies

2. Shell Programming and Scripting

Help with while read loop

Hey all, Tried searching the forums but my search-fu may not be strong today; please feel free to redirect me if I have simply missed a post that would be helpful! Trying to create a while loop that reads a list of configuration files, checks for a line that starts with "SOME_CMD" and ends... (4 Replies)
Discussion started by: jdwyer
4 Replies

3. Shell Programming and Scripting

explain while loop in ksh shell script

#!/bin/ksh log=ABCl log=EFG log=HIJ i=0 while <------ what is the meaning of ($i - lt 3) do print ${log} (( i=i+1 )) done (1 Reply)
Discussion started by: Bperl1967
1 Replies

4. UNIX for Dummies Questions & Answers

Read statement within while read loop

hi, this is my script #!/bin/ksh cat temp_file.dat | while read line do read test if ]; 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... (3 Replies)
Discussion started by: siva1612
3 Replies

5. Filesystems, Disks and Memory

Please explain how to read df -k

What I'm trying to find out is how many gb do we actually have free, how do you calculate that with the following output from the df -k command. Filesystem 1024-blocks Free %Used Iused %Iused Mounted on /dev/prod001 46137344 9200468 81% 26166 1% ... (6 Replies)
Discussion started by: NycUnxer
6 Replies

6. Shell Programming and Scripting

until loop and read

Hi guys what I wanna do is to create a script where can I input several times a file - assume with read function that will be inserted into a temp. file for further processing. When I press q I want that the loop will stop and continue in my script I typed this but the options q is not working... (6 Replies)
Discussion started by: kl1ngac1k
6 Replies

7. Shell Programming and Scripting

Need help with for loop with while read

Hello, The code below pulls printer information from a three-column file (set by the ${MULTIFILE} variable at the end), that has the printer queue name, IP, and type of printer. It is supposed to use this info to add the printers in the list to one, or more servers . The input file... (3 Replies)
Discussion started by: LinuxRacr
3 Replies

8. UNIX for Dummies Questions & Answers

if elif loop with read

I am trying to setup an if, elif statement that is dependant on a variable. If the user does not enter a valid option I would like it to prompt them again and start the loop over. Here is what I have so far: echo -n "enter variable (a, b, or c): " read freq if ; then echo "a" elif ;... (2 Replies)
Discussion started by: BStanley346
2 Replies

9. Shell Programming and Scripting

read line in a for loop

Hi All, How can we use read line using the index value of a FOR loop? eg: pt_mstr,pt_status,8 pt_mstr,pt_buyer,8 pt_mstr,pt_sfty_stk,8 pt_mstr,pt_ord_pol,3 pt_mstr,pt_capacity,8 pt_mstr,pt_plan_ord,3 pt_mstr,pt_ord_mult,8 From this file i want to read the line2, 3 and 4 only using a FOR... (3 Replies)
Discussion started by: balajim
3 Replies

10. Shell Programming and Scripting

Need Help : while read loop

hi all, Can you please help me with this issue ? while ####infinite loop do cat file1 |while read var1 var2 do func1 $var1 $var2 done cat file2 | while read var11 var22 do func2 $var11 $var22 done done func1 is called till all the values are read... (2 Replies)
Discussion started by: Navatha
2 Replies
Login or Register to Ask a Question