- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- capture exit code of function inside for 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
тАО03-02-2005 09:59 PM
тАО03-02-2005 09:59 PM
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
Solved! Go to Solution.
- Tags:
- exit status
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-02-2005 10:11 PM
тАО03-02-2005 10:11 PM
Re: capture exit code of function inside for loop
export exit_code=$?
do
echo ciao
done
echo ${exit_code)
Anil
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-02-2005 10:17 PM
тАО03-02-2005 10:17 PM
Re: capture exit code of function inside for loop
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-02-2005 10:28 PM
тАО03-02-2005 10:28 PM
Re: capture exit code of function inside for loop
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.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-02-2005 10:32 PM
тАО03-02-2005 10:32 PM
Solutionfor 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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-02-2005 10:45 PM
тАО03-02-2005 10:45 PM
Re: capture exit code of function inside for loop
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-02-2005 10:59 PM
тАО03-02-2005 10:59 PM
Re: capture exit code of function inside for loop
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-02-2005 11:00 PM
тАО03-02-2005 11:00 PM
Re: capture exit code of function inside for loop
echo ${exit_code) needs to as follows.
echo ${exit_code}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-02-2005 11:17 PM
тАО03-02-2005 11:17 PM
Re: capture exit code of function inside for loop
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-02-2005 11:24 PM
тАО03-02-2005 11:24 PM
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)
but "export exit_code=$?" is before "do" so the loop don't work