The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
ls while read loop - internal read picking up wrong input dkieran Shell Programming and Scripting 2 05-14-2007 03:02 PM
While read loop and rsh 104234 UNIX for Advanced & Expert Users 1 01-15-2006 11:53 AM
input inside while read loop jhansrod Shell Programming and Scripting 3 08-13-2005 10:46 AM
Nested while read line loop Rakker Shell Programming and Scripting 7 06-24-2005 07:42 AM
read inside a while loop dta4316 UNIX for Dummies Questions & Answers 3 05-21-2005 10:53 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rating: Thread Rating: 1 votes, 5.00 average. Display Modes
  #1 (permalink)  
Old 01-11-2006
jerardfjay jerardfjay is offline
Registered User
  
 

Join Date: Feb 2005
Posts: 146
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
  #2 (permalink)  
Old 01-11-2006
bhargav's Avatar
bhargav bhargav is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2004
Location: USA
Posts: 511
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 (permalink)  
Old 01-11-2006
jerardfjay jerardfjay is offline
Registered User
  
 

Join Date: Feb 2005
Posts: 146
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 (permalink)  
Old 01-11-2006
mahendramahendr mahendramahendr is offline Forum Advisor  
Registered User
  
 

Join Date: Dec 2005
Location: London
Posts: 222
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 (permalink)  
Old 01-11-2006
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
Or do this:
Code:
while read line
do
read test?"Enter a Value:"  < /dev/tty
done < testfile
  #6 (permalink)  
Old 01-12-2006
jerardfjay jerardfjay is offline
Registered User
  
 

Join Date: Feb 2005
Posts: 146
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:47 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0