Operating System - HP-UX
1834926 Members
2793 Online
110071 Solutions
New Discussion

Re: Scripting question (exiting a while loop)

 
Steve Sauve
Frequent Advisor

Scripting question (exiting a while loop)

Hello everyone,
I'm writing a while loop that reads input from a file. I then run some tests on the data. If the tests pass then it should continue, but if they fail then I want to move to the next record. I've tried putting a "done" statement in my "then" statement, but apparently it doesn't like that (I get an unexpected done error). Any thoughts?

Thanks ,
Steve
16 REPLIES 16
Kofi ARTHIABAH
Honored Contributor

Re: Scripting question (exiting a while loop)

Steve:

Use the "break" keyword....

do a man on sh-posix for more details.

cheers
nothing wrong with me that a few lines of code cannot fix!
John Palmer
Honored Contributor

Re: Scripting question (exiting a while loop)

Steve,

Use 'break' to exit the loop, 'continue' to go back to the start.

Both commands have a numeric argument which indicates which level of loop (for loops within loops).

Regards,
John
John Palmer
Honored Contributor

Re: Scripting question (exiting a while loop)

Steve,

Use 'break' to exit the loop, 'continue' to go back to the start.

Both commands have a numeric argument which indicates which level of loop (for loops within loops).

Regards,
John
CHRIS_ANORUO
Honored Contributor

Re: Scripting question (exiting a while loop)

Use while (equation, argument)
do (command)
done.
When We Seek To Discover The Best In Others, We Somehow Bring Out The Best In Ourselves.
Victor BERRIDGE
Honored Contributor

Re: Scripting question (exiting a while loop)

Follow Kofi's advice:
YOu will have to use in your while loop with if statements the words break or continue
example of while syntax:
while true
do
echo Enter filename
read FILE
if [ "$FILE" = "" ]
then
break
fi
echo "Edit : " $FILE " ?"
read AA
if [ "$AA" = "" -o "$AA" = no ]
then
continue
else
rm $FILE
fi
done
Victor BERRIDGE
Honored Contributor

Re: Scripting question (exiting a while loop)

I meant vi $FILE
...

Very hard trying to be multitasking, you know being at the phone on rm subject, people calling you from outside and trying to reply all at the same time...
Sorry
Steve Sauve
Frequent Advisor

Re: Scripting question (exiting a while loop)

Thanks for the advice guys, but it still doesn't seem to be working. Apparently the script is just ignoring the break/continue command. Could it be the fact that a function is calling the break/continue while the loop is in the main body?
Ex.
test() {
if (logical test)
then
break
fi
}
#main body
while read xyz
do
test;
done

Any thoughts?
Thanks for the help,
Steve
Steve Sauve
Frequent Advisor

Re: Scripting question (exiting a while loop)

Thanks for the advice guys, but it still doesn't seem to be working. Apparently the script is just ignoring the break/continue command. Could it be the fact that a function is calling the break/continue while the loop is in the main body?
Ex.
test() {
if (logical test)
then
break
fi
}
#main body
while read xyz
do
test;
done

Any thoughts?
Thanks for the help,
Steve
Steve Sauve
Frequent Advisor

Re: Scripting question (exiting a while loop)

Thanks for the advice guys, but it still doesn't seem to be working. Apparently the script is just ignoring the break/continue command. Could it be the fact that a function is calling the break/continue while the loop is in the main body?
Ex.
test() {
if (logical test)
then
break
fi
}
#main body
while read xyz
do
test;
done

Any thoughts?
Thanks for the help,
Steve
Steve Sauve
Frequent Advisor

Re: Scripting question (exiting a while loop)

Thanks for the advice guys, but it still doesn't seem to be working. Apparently the script is just ignoring the break/continue command. Could it be the fact that a function is calling the break/continue while the loop is in the main body?
Ex.
test() {
if (logical test)
then
break
fi
}
#main body
while read xyz
do
test;
done

Any thoughts?
Thanks for the help,
Steve
James R. Ferguson
Acclaimed Contributor

Re: Scripting question (exiting a while loop)

Steve:

If, in your last post, the objective is to exit the function at large, change the break to a return. You will exit the loop and the function at that point.

...JRF...
Victor BERRIDGE
Honored Contributor

Re: Scripting question (exiting a while loop)

Hi steve,
1) you have no condition to your while:
while
do
..
then you should put your read in the loop
while..
do
read

your test

done
Victor BERRIDGE
Honored Contributor

Re: Scripting question (exiting a while loop)

Second thought,
I wonder if you should not try to use continue!
For continue tells the shel to ignore the remainder of the loop and return to the beginning of your loop.
So you should write somthing like
while..
do
read..
if
then
continue
fi
more treatment
..
done
James R. Ferguson
Acclaimed Contributor

Re: Scripting question (exiting a while loop)

Steve:

The break needs to be in the loop. Do something like:

while read X
do
echo "I saw $X"
if [ "$X" = "ok" ]
then
break #...quit reading...exit the loop!
fi
done

...JRF...
Steve Sauve
Frequent Advisor

Re: Scripting question (exiting a while loop)

Thanks for the help everyone. After some testing it seems that the continue statement doesn't work from inside a function. The "return" statement also doesn't really help since the function only tests the input and doesn't modify it (that's the next function). The tests I ran are below. The script that has no functions works fine, where as the one with the function ignores the test. Unless someone has another thought, I'll just modify my script to include the test in the main body. Thanks for the help,
Steve

# This one works
while read first last loc
do
if [ "$last" = "" ]
then
continue
fi
echo $first $last
done < names

# This one don't
tst() {
if [ "$last" = "" ]
then
continue
fi
}

while read first last loc
do
tst;
echo $first $last
done < names
John Palmer
Honored Contributor

Re: Scripting question (exiting a while loop)

As you've discovered, continue and break will not work in a function (not when the do loop is outside it anyway). The way around this if you want to keep the function is to get it to return a value to indicate whether or not to continue.

Your example with a function:-

tst() {
if [ "$last" = "" ]
then return 0
else return 1
fi
}

while read first last loc
do
if tst;
then continue
fi
echo $first $last
done < names