I have a script which tries to read input from a user for every value read from a file. The input file is
#> more testfile
TEST1 | D200 | 12345601 | | ABC company | m
TEST2 | D201 | 12345602 | | ABC company | m
The script test.sh is as follows
while read line
do
read test?"Enter a Value:"
done < testfile
however when executed, shell never responds to a the read command.
#>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
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.
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
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
Or do this:
while read line
do
read test?"Enter a Value:" < /dev/tty
done < testfile
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