LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-26-2009, 07:29 PM   #1
vasireddy.jaipal
LQ Newbie
 
Registered: Nov 2009
Posts: 3

Rep: Reputation: 0
Unhappy need to read values in middle of reading value from a file


#!bin/bash

while read line
do
echo $line
read $variable
done <<filename

$variable is getting the same value which is being read from the file.
is there a way i can scan values from keyboard while reading a file line by line??
 
Old 11-26-2009, 09:16 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
what are you trying to do actually? in your while loop, you are reading lines and putting them into variable $line. Where does $variable come from??
 
Old 11-26-2009, 10:08 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,355

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
File & stdin read, using exec
Code:
#!/usr/bin/bash

# get file line cnt
lc=`wc -l t.t|awk '{print $1}'`
cnt=0

# set new file descriptor to read from file
exec 3< t.t

while [[ $cnt -lt $lc ]]
do
    # read from fd 3 ie t.t file
    read line <&3
    echo $line
    cnt=$(($cnt + 1))

    # read from stdin
    read var1
    echo $var1
done
 
Old 11-26-2009, 10:47 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by chrism01 View Post
File & stdin read, using exec
Code:
#!/usr/bin/bash

# get file line cnt
lc=`wc -l t.t|awk '{print $1}'`
cnt=0
to get just the line count, wc -l < file will do, so no need to call awk to print column 1.

Quote:
Code:
# set new file descriptor to read from file
exec 3< t.t

while [[ $cnt -lt $lc ]]
do
    # read from fd 3 ie t.t file
    read line <&3
    echo $line
    cnt=$(($cnt + 1))

    # read from stdin
    read var1
    echo $var1
done
Code:
while read -r line
do
  echo $line
  read var <&1 #read stdin
  echo "stdin: $var"
done < "file"
 
Old 11-27-2009, 02:32 PM   #5
vasireddy.jaipal
LQ Newbie
 
Registered: Nov 2009
Posts: 3

Original Poster
Rep: Reputation: 0
Talking read values in middle

Hi thanks for ur response
variable is just a input needed from the key board not from the file,but its not allowing me to take input from keyboard,what i mean is variable is getting the same value in $line.what i exactly wanted is,i want to take input from keyboard while iam reading a file line by line.

After almost searching for an hour i found out the solution,which is working perfect,now variable is able to get input from keyboard
But can anyone please tell me whats the functionality of < /dev/tty??i just dont want to do things blindly

while read line
echo $line
read variable < /dev/tty
done < file name

Last edited by vasireddy.jaipal; 11-27-2009 at 02:37 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading a CSV text file and storing the values in Oracle Database table shafi2all Linux - Newbie 3 04-17-2008 12:19 PM
LXer: Getting Error Values From The Middle Of A Pipe Chain In Bash LXer Syndicated Linux News 0 12-20-2007 11:20 AM
read config file parameter values sharathkv25 Programming 2 02-15-2007 01:55 AM
bash; reading values from a file km4hr Programming 16 07-28-2005 02:07 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:29 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration