The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Return of EXIT status ( $? ) ZINGARO HP-UX 1 03-12-2008 04:07 AM
Verify scp return status new2ss Shell Programming and Scripting 2 06-28-2006 07:04 PM
Return status of all previous runs mpang_ Shell Programming and Scripting 4 06-26-2006 11:58 PM
return ftp status blt123 Shell Programming and Scripting 12 07-21-2005 02:48 AM
Return status... Shaz Shell Programming and Scripting 7 11-19-2002 03:35 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 07-05-2008
Registered User
 

Join Date: Jul 2008
Posts: 2
evaluate return status of a function

Hi all

I'm trying to evalute the return status of a function without much success. I've put a very basic example below to explain.

check_ok() works fine but when used within an if statement, it always returns true, whether it is true or false. I'm guessing it returns true as the function succeeded, rather than the statement within it.

The check_ok function I am actually using has a lot of code ans is used many times in the script. I have tried various ways using $?/return etc but was hoping someone could explain the correct way to do this?
thanks for any help.


check_ok()
{
ps aux | grep -v grep | grep SOMETHING
}
if [[ check_ok ]]
then
echo OK
fi
Reply With Quote
Forum Sponsor
  #2  
Old 07-05-2008
Registered User
 

Join Date: Jul 2008
Location: Texas
Posts: 60
Without seeing the actual check_ok() it would be a little difficult to say exactly what your problem is. Unless you put in an explicit return statement, the routine will return the value of the last expression evaluated. If it always returns true then it would imply that the last expression in the subroutine either always returns true or has, coincidentally, always returned true in the cases you've tested.
Reply With Quote
  #3  
Old 07-05-2008
Registered User
 

Join Date: Jul 2008
Posts: 2
The function is really just a long series of greps.

I have tried to return $? and then check check_ok !=0 but this errors and I dont know how to check for the return status correcly?

check_ok()
{
ps aux | grep -v grep | grep SOMETHING
return $?
}
if [[ check_ok !=0 ]]
then
echo not running
fi
Reply With Quote
  #4  
Old 07-05-2008
Registered User
 

Join Date: Jul 2008
Location: Texas
Posts: 60
Here's just a stupid snippet to check return values. Should print
TRUE
NOT TRUE
when executed. If you wanted to test for not true, you could use -ne instead of -eq.


Code:
#!/usr/bin/ksh

return_true () {
  return 0
}

return_not_true () {
  return 1
}

return_true
if [[ $? -eq 0 ]]
then
  echo "TRUE"
else
  echo "NOT TRUE"
fi

return_not_true
if [[ $? -eq 0 ]]
then
  echo "TRUE"
else
  echo "NOT TRUE"
fi
Reply With Quote
  #5  
Old 07-06-2008
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,275
The return value of cmd | cmd1 | cmd2 is whatever cmd2 returns.
You can use grep -q <pattern> to suppress any out. It returns either > 0 (failure) or 0 = success (pattern found).

Long series of grep <> |grep <> | grep <> ... eat your system for lunch, if you get my drift.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 01:46 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0