LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-01-2008, 09:42 AM   #1
viveksnv
Member
 
Registered: Feb 2008
Posts: 38

Rep: Reputation: 15
Problem in checking function return value


Hello All,

I am newbie for linux.

I have problem for checking if the function executes correctly or not.

#!/bin/bash

function test()
{
echo "Function executed"
return 0
}

if [ test $? == "0" ]
then
echo "Success";
#Ends

i want to check the function execution with in the if condition.

Thanks in Advance.
 
Old 10-01-2008, 09:58 AM   #2
CRC123
Member
 
Registered: Aug 2008
Distribution: opensuse, RHEL
Posts: 374
Blog Entries: 1

Rep: Reputation: 32
Be careful using 'test' as a function as there is bash command called 'test' (the bash command is actually the same thing as square brackets '[ ]'.

You need to run the function first, then perform the if statement on its return code($?)

When comparing numbers with the 'test' command (aka '[ ]'), you use special tags:
[ 0 -eq 0 ] means 0 == 0
[ 0 -lt 0 ] means 0 < 0
[ 0 -gt 0 ] means 0 > 0
[ 0 -le 0 ] means 0 <= 0
etc

also, 'if' statements need to be completed with 'fi'
 
Old 10-01-2008, 10:04 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
First of all: Using 'test' as a name for a function, variable, or script-file is not a good idea. This is because there is nearly always a program called 'test' installed in UN*X / Linux system as a part of the system.

So, in the scripts below I changed the name of your function to 'mytest'.

Two possible scripts for what you are trying to do. (but you mixed the two ways, which doesn't work) :
Code:
#!/bin/bash

function mytest()
{
    echo "Function executed"
    return 0
}

mytest  # First call the function
if [ $? == "0" ] # Then check the code returned
then
    echo "Success";
fi ## you forgot 'fi' here to end the 'if' statement.

exit 0 # Not needed, but "Good practice" in general
But since you wanted the "if"-statement only to executed if the function returns 0, the if statement can be simpler:
Code:
#!/bin/bash

function mytest()
{
    echo "Function executed"
    return 0
}

if mytest 
then
    echo "Success";
fi

exit 0
 
Old 10-02-2008, 01:17 AM   #4
viveksnv
Member
 
Registered: Feb 2008
Posts: 38

Original Poster
Rep: Reputation: 15
Thanks to all

More thanks to all.

Now i realize, this is a silly question.

I will do more home works.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash function return rejeep Programming 15 08-24-2007 04:21 AM
How can I get the return value of a function in a shellscript? [KIA]aze Programming 8 07-05-2007 08:18 AM
sh script: problem with return value in function jhwilliams Linux - Software 1 06-20-2007 11:47 AM
return value of function in script ramesh_manu Red Hat 1 02-18-2007 01:05 PM
using return in recursive function hubabuba Programming 9 10-10-2005 09:59 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration