evaluate return status of a function


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting evaluate return status of a function
# 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
# 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.
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #926
Difficulty: Medium
On 05:20:00 UTC on Saturday, 24 January 2065, the Unix time value will equal 4000000000 seconds.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Function - Make your function return an exit status

Hi All, Good Day, seeking for your assistance on how to not perform my 2nd, 3rd,4th etc.. function if my 1st function is in else condition. #Body function1() { if then echo "exist" else echo "not exist" } #if not exist in function1 my all other function will not proceed.... (4 Replies)
Discussion started by: meister29
4 Replies

2. Shell Programming and Scripting

Return: can only `return' from a function or sourced script

Not sure where the problem is. I can run the script without any issue using the following command. . /opt/app/scripts/cdc_migration.sh But it fails with the below error when I try it this way /opt/app/scripts/cdc_migration.sh /opt/app/scripts/cdc_migration.sh: line 65: return: can only... (1 Reply)
Discussion started by: svajhala
1 Replies

3. Shell Programming and Scripting

Return Status

Hi can you explain me, what does variables $@ and $* return and how are they used, if can give me a sample example it could be helpful. Thanks in advance, Regards, Abhishek S. (1 Reply)
Discussion started by: abhisheksunkari
1 Replies

4. UNIX for Dummies Questions & Answers

opposite return status in c shell

there is something wrong with my system. when I do this: diff file1 file1 && echo 1 the output is 1. but diff file1 file2 >/dev/null && echo 1 output nothing while diff file1 file2 >/dev/null || echo 1 shows 1. the same with "grep" return status. they are both GNU utilities.... (5 Replies)
Discussion started by: phil518
5 Replies

5. Shell Programming and Scripting

Return a value from called function to the calling function

I have two scripts. script1.sh looks -------------------------------- #!/bin/bash display() { echo "Welcome to Unix" } display ----------------------------- Script2.sh #!/bin/bash sh script1.sh //simply calling script1.sh ------------------------------ (1 Reply)
Discussion started by: mvictorvijayan
1 Replies

6. HP-UX

Return of EXIT status ( $? )

I have the question: How return the exit code from then assign : VAR=$(command ) for ex. VAR=$(ls ....) VAREXIT=$? echo $VAREXIT VAREXIT is equal to 0 if the directory exist or not exist. WHI?? if i execute the command direct from line-command , the value of $? is different if... (1 Reply)
Discussion started by: ZINGARO
1 Replies

7. Shell Programming and Scripting

Verify scp return status

Hi all below is a snippet of my perl codesystem ("scp -pq $dest_file $path");How i can i trap the return status? ie if the scp fails how can i know ? (2 Replies)
Discussion started by: new2ss
2 Replies

8. Shell Programming and Scripting

Return status of all previous runs

hi, I set the crontab to execute script A every 5 minutes from 9:00 am to 4:00 pm everyday, now at 12:00am I want to run another script if and only if all the previous runs of script A return OK, can anyone tell me how it could be done? thank you very very much! (4 Replies)
Discussion started by: mpang_
4 Replies

9. Shell Programming and Scripting

return ftp status

Hello, I still have problems when trying to figure out if the status of an ftp was successful. I ftp to different types (nt, vax, unix, etc...) of machines. I am trying to write a universal script that will ftp a file and then check to see if the ftp was successful. I have tried the... (12 Replies)
Discussion started by: blt123
12 Replies

10. Shell Programming and Scripting

Return status...

Hello there! Here is my problem. I hope I can get some help about it. I need to know how can I get the return code of an application in the Unix shell script. The script is like below: PREVIOUS STATEMENT & VARIABLES sqlplus scott/tiger @$sqldir/$sqlscript NEXT STATEMENT (Like... (7 Replies)
Discussion started by: Shaz
7 Replies

Featured Tech Videos