Operating System - HP-UX
1752754 Members
5614 Online
108789 Solutions
New Discussion юеВ

capture exit code of function inside for loop

 
SOLVED
Go to solution
Paolo Gilli
Frequent Advisor

capture exit code of function inside for loop

hi,
I have this problem running a for loop:
-----------------------------------------------
for MONTHTOCHECK in `find /pippo -path "/pippo/*" -prune -name "DC*.DAT*" -type f -mtime +45 |sed s/.*DC/DC/| sort -d | awk '{ printf( substr($1,3,6)"\n" ); }' | uniq`
do
echo ciao
done

find: cannot stat /pippo
$ echo $?
0
-----------------------------------------------
how cah I cpture the exit code of find or any other function inside the for loop?

Thankyou
Paolo
12 REPLIES 12
RAC_1
Honored Contributor

Re: capture exit code of function inside for loop

for MONTHTOCHECK in `find /pippo -path "/pippo/*" -prune -name "DC*.DAT*" -type f -mtime +45 |sed s/.*DC/DC/| sort -d | awk '{ printf( substr($1,3,6)"\n" ); }' | uniq`
export exit_code=$?
do
echo ciao
done

echo ${exit_code)

Anil
There is no substitute to HARDWORK
Ranjith_5
Honored Contributor

Re: capture exit code of function inside for loop

HI Paolo,

echo $?

the above will give the status value of last command executed.

See an example.

SVR#-/crash>ls
bounds crash.0 iostat lost+found web.log.0827_1400hrs.Z
SVR#-/crash>echo $?
0
SVR#-/crash>xyz
ksh: xyz: not found
SVR#-/crash>echo $?
1

This means, for a command which is executed succesfully, the value of $? will be zero. If the last command is not successfull it will give a value which is non zero. This function is the common method which is used to check the exit code, and widely used in shell scripts.

Regards,
Syam
Fred Ruffet
Honored Contributor

Re: capture exit code of function inside for loop

Something strange with your find command :
If you use -path instead of -name, path will be used, but leading / removed (see manpage for details). So, you're looking for all files under pippo dir in /pippo (i.e. /pippo/pippo/*). Then you apply -name... very strange.

If I really understand, you want all files under /pippo named DC*.DAT* that have been modified under 45 days, without going into subdirs ? here it is :
find /pippo/* -prune -type f -name "DC*.DAT*" -mtime +45

Regards,

Fred
--

"Reality is just a point of view." (P. K. D.)
Muthukumar_5
Honored Contributor
Solution

Re: capture exit code of function inside for loop

One was as,

for file in `find . -type f;echo $?`; do
echo $file
done

Last file data contains $? (return value of find command)

Else try as,

find /pippo -path "/pippo/*" -prune -name "DC*.DAT*" -type f -mtime +45 |sed s/.*DC/DC/| sort -d | awk '{ printf( substr($1,3,6)"\n" ); }' | uniq > /tmp/testlog

if [[ $? -eq 0 ]]
then
while read line; do
echo $line
done < /tmp/testlog
fi

HTH.
Easy to suggest when don't know about the problem!
Paolo Gilli
Frequent Advisor

Re: capture exit code of function inside for loop

Thank to all,
What I need is know if it's possible capture the error that can occour in any function inside the for loop.
So if find,sort,awk or uniq fail, I can capture the exit code !0 and stop the proceure

Anil, your suggestion don't work
Peter Godron
Honored Contributor

Re: capture exit code of function inside for loop

Paolo,
I suggest you break your oneliner into seperate steps and check the error code at each step. (find/sed/sort/awk)
The end result would then be a temporary file with all your data, which you can then process within your loop.
Regards
RAC_1
Honored Contributor

Re: capture exit code of function inside for loop

There is a typo in my post.
echo ${exit_code) needs to as follows.
echo ${exit_code}
There is no substitute to HARDWORK
Steve Steel
Honored Contributor

Re: capture exit code of function inside for loop

Hi

do the find twice is the best way

example

param=$1
find $param > /dev/null 2>&1
if [ "$?" = "0" ]
then
for a in $(find $param -name *.log)
do
echo $a
done
else
echo bad
fi


Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
Paolo Gilli
Frequent Advisor

Re: capture exit code of function inside for loop

you wrote:
for MONTHTOCHECK in `find /pippo -path "/pippo/*" -prune -name "DC*.DAT*" -type f -mtime +45 |sed s/.*DC/DC/| sort -d | awk '{ printf( substr($1,3,6)"\n" ); }' | uniq`
export exit_code=$?
do
echo ciao
done

echo ${exit_code)

but "export exit_code=$?" is before "do" so the loop don't work