how to retrun a value from a function in shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to retrun a value from a function in shell script
# 1  
Old 08-25-2012
how to retrun a value from a function in shell script

Code:
fnPingFunction()
{
ping -c1 $1  
if [[ $? = 0 ]]
then result = 'Y'
else 
result = 'N'
fi 
} 
value=$(fnPingFunction "localhost")
echo $value

How do i get the value from the function ? value should be the result output Y or N

Last edited by Scrutinizer; 08-25-2012 at 10:18 AM.. Reason: code tags instead of quote tags
# 2  
Old 08-25-2012
There is no need to return the value (and also functions return only integer values). In your POSIX-style function definition, the variables used are global (unless you define these in the function using typeset). Just use the value of result after calling the function.
Code:
fnPingFunction "localhost"
value=$result
echo $value

or
Code:
fnPingFunction "localhost"
echo $result

directly.

Last edited by elixir_sinari; 08-25-2012 at 06:33 AM..
# 3  
Old 08-25-2012
Other ways:

Code:
foo () {
  local _fooresult='happy    end'
  echo "$_fooresult"
}

echo "$(foo)"

Code:
bar () {
    local _barresult=$1
    local _result='happy    end'
    eval $_barresult="'$_result'"
}

bar result
echo "$result"

--
Bye
# 4  
Old 08-25-2012
I have changed the script as
Code:
fnPingFunction "localhost" echo $result

but it still shows me the same
Quote:
line 5: result: command not found
# 5  
Old 08-25-2012
Remove the spaces before and after the assignment operator = in your function. Also, put in a ; before that echo.
Code:
fnPingFunction()
{
ping -c1 $1 
if (( $? == 0 ))
then result='Y'
else 
result='N'
fi 
}

And while calling, use
Code:
fnPingFunction localhost; echo $result

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to pass the config file lines as variable on the respective called function on a script

I want to make a config file which contain all the paths. i want to read the config file line by line and pass as an argument on my below function. Replace all the path with reading config path line by line and pass in respective functions. how can i achieve that? Kindly guide. ... (6 Replies)
Discussion started by: sadique.manzar
6 Replies

2. Shell Programming and Scripting

What is the function of the following lines at the top of a shell script file: Directory and Script?

The file starts like this: Directory: <path to the script> Script: <script fife name> #!bin/ksh ##Comments <actual script> What is the use of the first two lines in the script? What if I save the file without them? What will be the effect? They are not comments. Im very new to this,... (4 Replies)
Discussion started by: remytom
4 Replies

3. Shell Programming and Scripting

Shell Script function to use script name for log file output

Hi Team - I"m very new to Shell Scripting so I have a rather novice question. My forte is Windows Batch Scripting so I was just wondering what the Shell Script equivalent is to the DOS command %~n? %~n is a DOS variable that dispayed the script name. For instance (in DOS): REM... (11 Replies)
Discussion started by: SIMMS7400
11 Replies

4. Shell Programming and Scripting

Call a function in shell script from another shell script

I've 2 shell scripts viz., CmnFuncs.ksh and myScript.ksh. 1st script contains all common functions and its code is as below: $vi CmnFuncs.ksh #!/bin/ksh RunDate() { .... .... export Rundt=`date +%Y%m%d` } 2nd script is invoking the above one and I expect to use the RunDt variable... (8 Replies)
Discussion started by: njny
8 Replies

5. Red Hat

how to call a particular function from one shell another shell script

please help me in this script shell script :1 *********** >cat file1.sh #!/bin/bash echo "this is first file" function var() { a=10 b=11 } function var_1() { c=12 d=13 (2 Replies)
Discussion started by: ponmuthu
2 Replies

6. Shell Programming and Scripting

Function in Shell script

Legends, Can you please debug, what's wrong with the below code. I am gettng unexpected token error RebuldPF() ( #Changing the directory to data directory where the pf exists. cd /home/sandeep/files #Listing the names of the Pricefiles for rebuilding echo "The following pricefiles will... (6 Replies)
Discussion started by: sdosanjh
6 Replies

7. Shell Programming and Scripting

SHELL SCRIPT Function Calling Another Function Please Help...

This is my function which is creating three variables based on counter & writing these variable to database by calling another function writeRecord but only one record is getting wrote in DB.... Please advise ASAP...:confused: function InsertFtg { FTGSTR="" echo "Saurabh is GREAT $#" let... (2 Replies)
Discussion started by: omkar.sonawane
2 Replies

8. Shell Programming and Scripting

Shell Script Function

Dear Friends, I am on SOLARIS 9 (ksh) I am working on a shell script that upload files with NcFTP with resume option. So far the script is working correctly. The script makes a list of files that need to be transferred and then launch the NcFTP command to start the transfer. ... (0 Replies)
Discussion started by: Aswex
0 Replies

9. Shell Programming and Scripting

getting the value of a c function in shell script

Let say there is a module fileselection module written in c language which returns the file name. Is it possible to get the file name from the file selection module directly, I mean can we call a c function directly in shell script without doing executable. If possible then how it can be... (1 Reply)
Discussion started by: surjyap
1 Replies

10. Shell Programming and Scripting

How to retrun a value from a function call

Hi I have the following code - It returns a value but I don't know how to access it in the main code!!! #!/usr/bin/ksh set +x function f { d=`date +"%H%M%S` typeset -Z8 x x=$RANDOM i="$d""$x"$$ return $i #<-- i need to access this value!!! } echo $f #<-- How to show... (4 Replies)
Discussion started by: GNMIKE
4 Replies
Login or Register to Ask a Question