LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   need to read values in middle of reading value from a file (https://www.linuxquestions.org/questions/linux-newbie-8/need-to-read-values-in-middle-of-reading-value-from-a-file-771801/)

vasireddy.jaipal 11-26-2009 07:29 PM

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??

ghostdog74 11-26-2009 09:16 PM

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??

chrism01 11-26-2009 10:08 PM

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


ghostdog74 11-26-2009 10:47 PM

Quote:

Originally Posted by chrism01 (Post 3770938)
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"


vasireddy.jaipal 11-27-2009 02:32 PM

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


All times are GMT -5. The time now is 07:01 AM.