LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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
 
LinkBack 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,695
Blog Entries: 5

Rep: Reputation: 239Reputation: 239Reputation: 239
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
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5
Posts: 16,086

Rep: Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995
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,695
Blog Entries: 5

Rep: Reputation: 239Reputation: 239Reputation: 239
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
Trackbacks are Off
Pingbacks are On
Refbacks are 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


All times are GMT -5. The time now is 11:35 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration