Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get reply why reading a file 1

Status
Not open for further replies.

wdellin

MIS
Feb 8, 2002
36
US
Looking for some help again with my scripting. I'm trying to read a file and for each entry in the file I want to ask the person running if they want to export that table. If they do, I put the table name in a final output file.

the file I'm reading has

book
chapter
case


My part of the script in question has the following:

rm -f finalout.txt
while read tabname;
do
echo 'Do you want to export '${tabname}
echo 'reply Y to export, N to skip'
read ans1
echo ${ans1}
if [ $ans1} = 'Y' ]
then echo $tabname >> finalout.txt
else
echo 'table will not be exported'
fi
done

output.....

Do you want to export book
reply Y to export, N to skip

test4.sh[XX]: test: argument expected (where XX is the line number of the if statement)


Any ideas on this. I have a feeling I cannot use the read within a read, but do not know how else to do it.

thanks
 
Sorry, I forgot an important part of the script. On the done statement is the file that I'm reading. The script has been updated.


rm -f finalout.txt
while read tabname;
do
echo 'Do you want to export '${tabname}
echo 'reply Y to export, N to skip'
read ans1
echo ${ans1}
if [ $ans1} = 'Y' ]
then echo $tabname >> finalout.txt
else
echo 'table will not be exported'
fi
done < newtableout.txt
 
try this ...

exec 3< your_file
quite=1
while [ $quite -eq 1 ]
do
read -u3 f1 f2 f3
if [ $? -eq 0 ]
then
read anw?"do you want to export $f1"
.
.
else
quit=0
fi
done
 
cptk

thanks for your answer. My problem is the number of lines in the text file can range from 1 to 10, so I'm not sure how many I would have in a run.

 
I should note that this based on ksh shell, which handles "read" a little different than the other shells.

But to answer your initial question, yes, multiple "read" cmds can be used.

Let me know what shell your using - I'll explain how this works -- simple!!



 
number of lines is not a problem at all in my script ...
Let me know what shell your using - I'll explain how this works -- simple!!

 
perfect ...

then my example as shown should work ...

Do you want me to elaborate on the specifics?
 
Sorry to take your time on this but if you could provide some insite into the commands I would appreciate it.
 
rm final_out.txt 2> /dev/null #remove final file, traps for errors (i.e. - if file doesn't exist)
exec 3< your_file.txt # sets your initial file to a file descriptor (note no space btween 3<)
quite=1 # sets while loop variable
while [ $quite -eq 1 ]
do
read -u3 f1 f2 f3 # read from file descriptor 1 line at a time into three fields
if [ $? -eq 0 ] # checks the exit status of previous cmd (i.e. read cmd)
then
read anw1?"do you want to export $f1 (Y|y)? "
if [[ #anw1 == "Y" || $anw1 == "y" ]]
then
echo $f1 >> final_out.txt
fi
read anw2?"do you want to export $f2 (Y|y)? "
if [[ #anw2 == "Y" || $anw2 == "y" ]]
then
echo $f2 >> final_out.txt
fi
read anw3?"do you want to export $f3 (Y|y)? "
if [[ #anw3 == "Y" || $anw3 == "y" ]]
then
echo $f3 >> final_out.txt
fi
else
quit=0
fi
done


...now you could clean up the 3 separate read stmts (if you don't know the # of fields within each line, but to keep it simple, I left it this way)
 
alright, because I'm heading home (u caught me at a good time) ...

This is a little cleaner

rm final_out.txt 2> /dev/null
exec 3< your_file.txt
quite=1
while [ $quite -eq 1 ]
do
read -u3 line # read from file descriptor 1 line at a time into one field
if [ $? -eq 0 ]
then
for x in $line #breaks out each field in the line (thus the line can be variable # of fields)
do
read an?"do you want to export $x (Y|y)? "
if [[ $an == "Y" || $an == "y" ]]
then
echo $x >> final_out.txt
fi
done
else
quit=0
fi
done
 
You're original error is because you had a typo in your script. The line [tt]if [ $ans1} = 'Y' ][/tt] should have been [tt]if [ ${ans1} = 'Y' ][/tt]. Note the missing left brace ([tt]{[/tt]) on [tt]${ans1}[/tt].

Hope this helps.
 
cptk

Thank you so much for the solution and your time to explain, greatly appreciated.
 
another possibility is to read the answer from /dev/tty, while redirecting stdin from your input file for the while read line loop:

while read line
do
...
read ans1 </dev/tty
...
done </path/to/file

HTH,

p5wizard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top