![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
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.
|
|
#3
|
|||
|
|||
|
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 |
|
#4
|
|||
|
|||
|
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
|
|
#5
|
|||
|
|||
|
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. |
|||
| Google The UNIX and Linux Forums |
| Thread Tools | |
| Display Modes | |
|
|