Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

nest while loops (K-shell)

0 views
Skip to first unread message

rog...@my-deja.com

unread,
Dec 4, 2000, 3:00:00 AM12/4/00
to
How can I nest a while loop within another while loop??? What my script
is doing is, it ends the parent while loop
when it reads then 'done' statement of the inner/child loop.


Thanks,
Nasir (part of code is below)

while read dir prefix URL ####### PARENT LOOP
do
cd $dir/config
cp -p obj.conf obj.conf.nasir
########## EDITING THE FILE HERE ###########
print NameTrans fn='"redirect"' from='"'$prefix'"'
url='"'$URL'"' > $MY_DIR/text_line

IFS=:
while read whole_line ######## CHILD LOOP
do
if [[ whole_line = '<This>' ]]
then
echo $whole_line > testfile.third
print NameTrans fn='"redirect"' from='"'$prefix'"'
url='"'$URL'"' > testfile.third
else
echo $whole_line > testfile.third
done < testfile ####### The parent loop is ended
here instead of the child loop


### $MY_DIR/testfile < `sed -f $MY_DIR/sed-scr $MY_DIR/testfile`
echo "The value of prefix is $prefix and the URL is $URL"
echo "Working in directory $dir/config now ..."
cd $dir ; ./restart
echo "Working on direcotry $dir DONE successfully."
echo ++++++++++++++++++++++++++++++++++++++++++
done < $filename


Sent via Deja.com http://www.deja.com/
Before you buy.

JB

unread,
Dec 4, 2000, 3:00:00 AM12/4/00
to
Think I've had this one in a script of mine. It's not the while loops that
are the problems, it's what they're doing. Both of them are reading, and
UNIX is getting confused as to which read goes with which file descriptor.
I didn't find a way to fix mine, so coded around it. Mine was a simple ...
cat <filename> | while read ...
do
if <conditon>
echo "Ok to proceed"
read
fi
....

- and I found that the read from the screen was ignored, the script took the
answer from the next line of the <filename>.

Hope this helps you understand it, and maybe you can code around. I'll
watch your post for an answer from a true guru.

JB

<rog...@my-deja.com> wrote in message news:90grg2$et4$1...@nnrp1.deja.com...

Paul Brandariz x6546

unread,
Dec 4, 2000, 3:00:00 AM12/4/00
to
rog...@my-deja.com wrote:
>
> How can I nest a while loop within another while loop??? What my script
> is doing is, it ends the parent while loop
> when it reads then 'done' statement of the inner/child loop.
>
> Thanks,
> Nasir (part of code is below)
>
> while read dir prefix URL ####### PARENT LOOP
> do
> cd $dir/config
> cp -p obj.conf obj.conf.nasir
> ########## EDITING THE FILE HERE ###########
> print NameTrans fn='"redirect"' from='"'$prefix'"'
> url='"'$URL'"' > $MY_DIR/text_line
>
> IFS=:
> while read whole_line ######## CHILD LOOP
> do
> if [[ whole_line = '<This>' ]]
> then
> echo $whole_line > testfile.third
> print NameTrans fn='"redirect"' from='"'$prefix'"'
> url='"'$URL'"' > testfile.third
> else
> echo $whole_line > testfile.third

Don't you need a "fi" here to close the else ?

> done < testfile ####### The parent loop is ended
> here instead of the child loop
>
> ### $MY_DIR/testfile < `sed -f $MY_DIR/sed-scr $MY_DIR/testfile`
> echo "The value of prefix is $prefix and the URL is $URL"
> echo "Working in directory $dir/config now ..."
> cd $dir ; ./restart
> echo "Working on direcotry $dir DONE successfully."
> echo ++++++++++++++++++++++++++++++++++++++++++
> done < $filename
>
> Sent via Deja.com http://www.deja.com/
> Before you buy.

--
___________________________________________________________________________
Paul R. Brandariz E-mail Internet: bran...@lore.kla-tencor.com
KLA-Tencor Corporation
One Technology Dr.
Milpitas, CA 95035

Glenn West

unread,
Dec 4, 2000, 3:00:00 AM12/4/00
to
In article <90h0i0$5t4$1...@serv1.albacom.net>,

"JB" <romojo...@infinito.it> wrote:
> Think I've had this one in a script of mine. It's not the while
loops that
> are the problems, it's what they're doing. Both of them are reading,
and
> UNIX is getting confused as to which read goes with which file
descriptor.
> I didn't find a way to fix mine, so coded around it. Mine was a
simple ...
> cat <filename> | while read ...
> do
> if <conditon>
> echo "Ok to proceed"
> read
> fi
> ....
>
> - and I found that the read from the screen was ignored, the script
took the
> answer from the next line of the <filename>.
>
> Hope this helps you understand it, and maybe you can code around.
I'll
> watch your post for an answer from a true guru.
>

[original code snipped]

I agree with your assessment. If you are using the Korn shell, I
believe the read command supports the ability to read from a specific
descriptor using the -u option. So the solution would be to use "exec"
to associate one of the input files with a descriptor (other than 0)
and read from that specific descriptor.

while read -u3 record
do
# do work
done 3<input.dat

If you're using the Bourne shell, you'll have to do something more
complicated (save stdin in another descriptor, make stdin point to your
input file, do work using stdin, undo everything):

exec 3<&0
exec < input.dat
while read record
do
# do work
done
exec 0<&3 3<&-

HTH...

0 new messages