The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Assign o/p of awk to a variable c2b2 Shell Programming and Scripting 8 04-01-2009 07:50 AM
assign a value to a variable Shivdatta Shell Programming and Scripting 10 01-17-2007 07:40 AM
assign value to variable using AWK HAA Shell Programming and Scripting 4 12-06-2006 11:43 AM
assign a value to variable markjason Shell Programming and Scripting 3 10-10-2006 02:05 PM
assign to variable AkumaTay UNIX for Dummies Questions & Answers 1 05-18-2002 02:11 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-28-2006
trivektor trivektor is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 8
Assign the returned value of a function to a variable

Hi,

Can anyone please show me how to assign the returned value of a function to a variable? Thanks.
  #2 (permalink)  
Old 09-29-2006
Yogesh Sawant's Avatar
Yogesh Sawant Yogesh Sawant is offline Forum Staff  
Part Time Moderator and Full Time Dad
  
 

Join Date: Sep 2006
Location: Rossem, Tazenda
Posts: 1,086
in a shell script:

function_name # call the function
return_val=$? # store the return value
echo $return_val # do something with the return value

Function can return a value, called an exit status

The exit status may be explicitly specified by a return statement, otherwise it is the exit status of the last command in the function

This exit status may be used in the script by referencing it as $?

The largest positive integer a function can return is 255
  #3 (permalink)  
Old 09-29-2006
Dhruva's Avatar
Dhruva Dhruva is offline
Registered User
  
 

Join Date: Mar 2006
Location: India
Posts: 255
Here is one example::
Code:
month_to_num() {
if [ -n "$1" ]
then
t_month=$1
case $t_month in

Jan) return 01;;
Feb) return 02;;
Mar) return 03;;
Apr) return 04;;
May) return 05;;
Jun) return 06;;
Jul) return 07;;
Aug) return 08;;
Sep) return 09;;
Oct) return 10;;
Nov) return 11;;
Dec) return 12;;
*) return 0;;

esac
fi
}
To call a function and store result in a variable see below lines
Code:
month_to_num $time_month
v_month_index=$?
if you call this function with parameter jan,feb,mar....you will store their index value in variable v_month_index.

Last edited by Dhruva; 09-29-2006 at 04:57 AM.. Reason: typo
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:59 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0