Issue in using read keyword twice


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Issue in using read keyword twice
# 1  
Old 12-21-2014
Issue in using read keyword twice

Hi,

I have a situation where i need to read line by line from a text pad and with each line , i need to take inputs from command line and do some process.
Using below code i am not able to use 'read' keyword twice.

Can any one please help
Code:
cat > t.txt
a
d
c
 
> cat > t.ksh
while read line
do
echo "press oprion"
read kind
echo "kind=$kind
echo "line=$line
done<t.txt

> sh t.ksh
press oprion
kind=d
echo line=a
press oprion
kind=
echo line=c

# 2  
Old 12-21-2014
Ravindra,

Your script:
Quote:
Originally Posted by Ravindra Swan
Code:
while read line
do
echo "press oprion"
read kind
echo "kind=$kind"
echo "line=$line"
done<t.txt

is working as written, once the missing quotes were added:
Code:
$ ksh -x t.ksh 
+ 0< t.txt
+ read line
+ echo 'press oprion'
press oprion
+ read kind
+ echo kind=d
kind=d
+ echo line=a
line=a
+ read line
+ echo 'press oprion'
press oprion
+ read kind
+ echo kind=
kind=
+ echo line=c
line=c
+ read line

The first read assigned "a" to line, the second read assigned "d" to kind. The loops repeats and assigned "c" to line and "" to kind. Maybe this would make more sense if the input file contained:
Code:
Line 1  a
Line 2  d
Line 3  c
Line 4

And the resulting output would be:
Code:
press oprion
kind=Line 2  d
line=Line 1  a
press oprion
kind=Line 4
line=Line 3  c

Is this the desired behavior?
# 3  
Old 12-21-2014
Hi derekludwig,

Thanks for reply,

My expectation is something else.

Alternate display of input from command editor and each line from text pad.
If 1,2,3 are the 3 inputs supplied, then
O/P:
Code:
kind=1
line=a
kind=2
line=d
kind=3
line=c


Last edited by Scrutinizer; 12-21-2014 at 04:57 PM.. Reason: CODE TAGS
# 4  
Old 12-21-2014
If I understand correctly, you want to read from the terminal every other time. You can do that with input redirection. Try:
Code:
exec 3<&0

while read line
        do      echo "press oprion"
                read kind <&3
                echo "kind=$kind"
                echo "line=$line"
        done < t.txt

This duplicates file descriptor 3 from stdin (terminal) fd 0. The second read uses this fd to get its input from. In the while block, stdin is redirected from your text file. Result:
Code:
press oprion
1
kind=1
line=a
press oprion
2
kind=2
line=d
press oprion
3
kind=3
line=c

This User Gave Thanks to RudiC For This Post:
# 5  
Old 12-22-2014
Or use the extra descriptor for the while-loop's input
Code:
while read line <&3
do echo "press oprion"
read kind
echo "kind=$kind"
echo "line=$line"
done 3< t.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

While read pipe input issue

Hello, I have an ffmpeg bash script which is working nice and I need to do the same for other sources. To create new scripts and to deal with multiple bash files sounds not logical. It is a bit hard to manage for me.. I wondered if it was possible to make my input file as variable. Then I... (1 Reply)
Discussion started by: baris35
1 Replies

2. Shell Programming and Scripting

ksh while read issue

Hello, I have used a chunk of ksh script similar to this in many places without any issue: while : do print; read OPTION?"Enter a number (q to quit): " expr ${OPTION} + 1 >/dev/null 2>&1 CHECKVAL=$? if }" != ${OPTION} ]; then ... (2 Replies)
Discussion started by: port43
2 Replies

3. Shell Programming and Scripting

Read line, issue with leading - and {}'s

Heyas With my forum search term 'issue with leading dash' i found 2 closed threads which sadly didnt help me. Also me was to eager to add the script, that i didnt properly test, and just now figured this issue. So i have this code: if ] then while read line do line="${line/-/'\-'}"... (7 Replies)
Discussion started by: sea
7 Replies

4. Shell Programming and Scripting

Search for a Keyword in file and replace another keyword or add at the end of line

Hi I want to implement something like this: if( keyword1 exists) then check if(keyword2 exists in the same line) then replace keyword 2 with New_Keyword else Add New_Keyword at the end of line end if eg: Check for Keyword JUNGLE and add/replace... (7 Replies)
Discussion started by: dashing201
7 Replies

5. Shell Programming and Scripting

while read issue

I'm using while read in a script to create a file but when I paste on the screen it echos out different than the data. The file is created correctly and the script does as it should. Just trying to resolve what's being show on the screen. while read FILES do echo... (3 Replies)
Discussion started by: toor13
3 Replies

6. Shell Programming and Scripting

while read LINE issue

Hi, This is the script and the error I am receiving Can anyone please suggest ? For the exmaple below assume we are using vg01 #!/bin/ksh echo "##### Max Mount Count Fixer #####" echo "Please insert Volume Group name to check" read VG lvs |grep $VG | awk {'print $1'} > /tmp/audit.log ... (2 Replies)
Discussion started by: galuzan
2 Replies

7. UNIX for Advanced & Expert Users

read() from ttyS1 issue while write() is Ok

Hi! I've got a problem with reading from serial port, when I run this code on Digi ConnectCore Wi-9c. But writing to serial port is Ok. By the way, when I'm running this code on "full" Linux it is working Ok - I can read and write to serial without mistakes. Where is a problem? uname -a:... (3 Replies)
Discussion started by: Japonomatj
3 Replies

8. Shell Programming and Scripting

read the first 10 lines below a keyword found by grep

Hello Everyone, i need to read specific number of lines ( always serialized ; i.e from 10 to 20 or from 34 to 44 ) in a file , where the first line is found by grep 'ing a keyword. example file.txt ------------------------------------------------------------------ --header this is the... (7 Replies)
Discussion started by: alain.kazan
7 Replies

9. Shell Programming and Scripting

While loop read line Issue

Hi I am using while loop, below, to read lines from a very large file, around 400,000 rows. The script works fine until around line 300k but then starts giving incorrect result. I have tried running the script with a smaller data set and it works fine. I made sure to include the line where... (2 Replies)
Discussion started by: saurabhkumar198
2 Replies

10. UNIX for Dummies Questions & Answers

While read issue...

I think I saw another post where someone said he an issue with a 'while within a while' but there were no solutions on it. I have an input file of data columns separated by pipes "|" and I want to set each column to a variable and do something with it. This I can do. When I get to a certain... (5 Replies)
Discussion started by: giannicello
5 Replies
Login or Register to Ask a Question