- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- continue for a loop ?
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-03-2006 09:59 PM
тАО09-03-2006 09:59 PM
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
Solved! Go to Solution.
- Tags:
- for loop
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-04-2006 12:10 AM
тАО09-04-2006 12:10 AM
Re: continue for a loop ?
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-04-2006 12:43 AM
тАО09-04-2006 12:43 AM
Re: continue for a loop ?
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-04-2006 12:54 AM
тАО09-04-2006 12:54 AM
Re: continue for a loop ?
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-04-2006 01:04 AM
тАО09-04-2006 01:04 AM
Re: continue for a loop ?
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-04-2006 02:16 AM
тАО09-04-2006 02:16 AM
SolutionThis 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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-04-2006 06:39 AM
тАО09-04-2006 06:39 AM
Re: continue for a loop ?
http://docs.hp.com/en/B2355-90046/ch21s05.html
thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-04-2006 09:37 AM
тАО09-04-2006 09:37 AM
Re: continue for a loop ?
=============================================
#!/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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-04-2006 10:11 PM
тАО09-04-2006 10:11 PM
Re: continue for a loop ?
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