1752794 Members
6750 Online
108789 Solutions
New Discussion юеВ

continue for a loop ?

 
SOLVED
Go to solution
totoperdu
Frequent Advisor

continue for a loop ?

hello,

i try to make an exit function like that:

fct_exit () {
RVAL="${?}"
test "${RVAL}" != "0" && {
echo "Erreur ${1}"
continue
}

but that don't works in a loop i think because the continue is interpreted by the function itself:

for i in "unavailable available"
do
cat $i
fct_exit "erreur number 1"
echo $i
done

the result give me the $1 value and unavailable and the available echo ;(
if i replace "continue" by an "exit", the script stops, but it's not what i want.

someone can help the newbie i am please?
Regards,
Cedrick Gaillard
8 REPLIES 8
Jean-Yves Picard
Trusted Contributor

Re: continue for a loop ?

Hello,


quote around "unavailable available"
make it a single argument, hence your loop is executed once.

if you remove quote, you'll get :

cat: Cannot open unavailable: No such file or directory
Erreur erreur number 1
unavailable
Mon Sep 4 13:50:40 METDST 2006
available

I am not sure continue will work inside a function contained is a loop.
((because, function environment overide loop.
in other word loop is not seen from inside the function. ))

use return inside function and continuie outside :


fct_exit () {
RVAL="${?}"
x=1
test "${RVAL}" != "0" && {
echo "Erreur ${1}"
x=0
}
return $x
}

for i in unavailable available
do
cat $i
if fct_exit "erreur number 1"
then continue
fi
echo echo $i
done


Jean-Yves Picard
totoperdu
Frequent Advisor

Re: continue for a loop ?

thanks for your answer.

the goal is to reduce the scripts, with your way, tests are added and it's not what i want.

i also have tried :
#!/bin/ksh

for i in toto tata tutu
do
fct_exit="$(if [ ! -r "${i}" ] ; then continue ; fi)"

echo ${i}
${fct_exit}
echo "-"

done


but that do not works more ;-(

thanks for your help
Cheers,
Cedrick Gaillard
James R. Ferguson
Acclaimed Contributor

Re: continue for a loop ?

Hi Cedrick:

I suggest this, using an inline approach:

#!/usr/bin/sh
for i in unavailable available
do
cat $i 2> /dev/null
[ $? != 0 ] && { echo "Erreur ${i}"; continue; }
echo $i
done

...I have added the redirection of STDERR for 'cat' to 'dev'null'. This eliminates 'cat' reporting the non-existent file and leaves only your "Erreur ${i}".

Regards!

...JRF...
Jean-Yves Picard
Trusted Contributor

Re: continue for a loop ?

Hello,

you second try can't work, the instruction

fct_exit="$(if [ ! -r "${i}" ] ; then continue ; fi)"

fork a new shell, continue in this shell can't be related to for loop in previous shell.

James's solution is a bit better.

however, in real world, what you want to put in the function is more likely the real code instead of echo "Erreur ${1}".
this is what you should use a function for.

code overhead for James's solution or mine is light.
besides hiding a continue inside a function will greatly hinder debugging ...

Jean-Yves Picard
James R. Ferguson
Acclaimed Contributor
Solution

Re: continue for a loop ?

Hi (again) Cedrick:

This will come close to the format you were originally trying to achieve.

#!/usr/bin/sh
fct_exit () {
RVAL="${?}"
test "${RVAL}" != "0" && {
echo "Erreur ${1}"
return 1
}
return 0
}
for i in unavailable available
do
cat $i
fct_exit "erreur number 1" || continue
echo $i
done

Regards!

...JRF...
Sandman!
Honored Contributor

Re: continue for a loop ?

The "continue" statement applies only to loops and not to "test" cases. Could you explain what exactly you are trying to do inside your function "fct_exit".

http://docs.hp.com/en/B2355-90046/ch21s05.html

thanks!
Sandman!
Honored Contributor

Re: continue for a loop ?

Give the script below a try...

=============================================
#!/bin/sh -x

fct_exit () {
RVAL="${?}"
test "${RVAL}" != "0" && {
echo "Erreur ${1}"
}
}

for i in unavailable available
do
cat $i 2>/dev/null || fct_exit
done
=============================================

cheers!
totoperdu
Frequent Advisor

Re: continue for a loop ?

Thanks Jean-Yves Picard but i have already tried to insert into a var with no success.
maybe i made a mistake.

Thanks ...JRF... i will try the 'return 1' asap ;)

Thanks Sandman but i don't understand your link : http://docs.hp.com/en/B2355-90046/ch21s05.html
it says: continue skips any lines following it in a for, while, until, or select loop

in my case, my function is not in a for, while, until, or select loop.

it's why i'm confused.

Cheers,
Cedrick Gaillard