I have a main script with while loop having for loop inside. Again in for loop based on if condition few functions will be called. So when a function is called for certain condition it should come out from the main for loop and should continue with while loop.
Let me explain with example here:
I am having a ksh as below
###Functions#####
execute_sqlldr()
{
sqlldr user/pass control=$ctl log=$log data=$file discard=$dis
RC=$?
if [$RC -eq 0] then
echo "loaded successfully"
elif [$RC -eq 1 -o $RC -eq 3]; then
echo "sqlldr problem"
elif [$RC -eq 2]; then
??
fi
return 0
}
execute_sp()
{
-----
}
###Main Program####
while [1]
do
for file in `ls /home/usr/*.txt`
do
if grep 'Oracle Available'; then
execute_sqlldr $file
else
echo "database is not available"
fi
execute_sp $file
done
done
Here in the main program when execute_sqlldr $file is called and when $RC -eq 2 then my program instead of coming back to for loop in main program and calling the next function execute_sp $file it should come wholly out of for loop and should start from while loop. So with what should i replace ?? with in the function execute_sqlldr().
I hope i am clear here. Any help is greatly appreciated. Thanks
Return some unique no from execute_sqlldr (when $RC -eq 2), say return 2
execute_sqlldr()
{
sqlldr user/pass control=$ctl log=$log data=$file discard=$dis
RC=$?
if [$RC -eq 0] then
echo "loaded successfully"
elif [$RC -eq 1 -o $RC -eq 3]; then
echo "sqlldr problem"
elif [$RC -eq 2]; then
return 2
fi
return 0
}
Then
while [1]
do
for file in `ls /home/usr/*.txt`
do
if grep 'Oracle Available'; then
execute_sqlldr $file
if [ $? -eq 2 ]; then
break 1
fi
else
echo "database is not available"
fi
execute_sp $file
done
done
In break n, n specifies the nth enclosing loop to exit from.
etcetera..
Like Anurag says, you cannot break out of the for loop from within the function. You can only pass a return code or value which you can test in the main loop and the you can use break. break 1 is the default, so break and break 1 are the same.
I stand corrected. Also in dash it breaks the calling loop... Thanks:b:
---------- Post updated at 11:29 ---------- Previous update was at 09:55 ----------
So to sum up, you could use break inside a function which would break out of a calling loop. I would not use that however as I think it is confusing. I would generate a return code and check for that in the calling part.