Assign the returned value of a function to a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Assign the returned value of a function to a variable
# 1  
Old 09-28-2006
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  
Old 09-29-2006
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  
Old 09-29-2006
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 05:57 AM.. Reason: typo
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How can I assign awk's variable to shell script's variable?

I have the following script, and I want to assign the output ($10 and $5) from awk to N and L: grdinfo data.grd | awk '{print $10,$5}'| read N L output from gridinfo data.grd is: data.grd 50 100 41 82 -2796 6944 0.016 0.016 3001 2461. where N and L is suppose to be 3001 and 100. I use... (8 Replies)
Discussion started by: geomarine
8 Replies

2. UNIX for Beginners Questions & Answers

Need to pass variable in a command and assign value to a variable

Hello All, Hope you're doing well ! I am trying below command to be passed in a shell script, header_date_14 is a variable and $1 is the name of a file I intend to pass as a command line argument, however command line argument is not being accepted. header_date_14=$(m_dump... (8 Replies)
Discussion started by: ektubbe
8 Replies

3. Shell Programming and Scripting

Substring a returned function value

Hello, I have something that should be very simple yet I am losing my head in figuring out how to get it to work: I am calling a function passing a parameter, this will return a particular string, next I want to substring the returned value and break it apart. All of this I want to do on a... (2 Replies)
Discussion started by: gio001
2 Replies

4. Shell Programming and Scripting

check variable value when nothing is returned

Hi all, I am wondering how can I check when a variable has nothing returned in it. I am trying to store a pid in this variable to see if a script is running in the background. So I am using something like that proc_pid=`ps -ef |grep script.sh|grep -v grep|awk '{print $2}'` if then ... (1 Reply)
Discussion started by: geovas
1 Replies

5. Shell Programming and Scripting

Variable not found error for a variable which is returned from stored procedure

can anyone please help me with this: i have written a shell script and a stored procedure which has one OUT parameter. now i want to use that out parameter as an input to the unix script but i am getting an error as variable not found. below are the unix scripts and stored procedure... ... (4 Replies)
Discussion started by: swap21783
4 Replies

6. Shell Programming and Scripting

assign awk's variable to shell script's variable?

Dear All, we have a command output which looks like : Total 200 queues in 30000 Kbytes and we're going to get "200" and "30000" for further process. currently, i'm using : numA=echo $OUTPUT | awk '{print $2}' numB=echo $OUTPUT | awk '{print $5}' my question is : can I use just one... (4 Replies)
Discussion started by: tiger2000
4 Replies

7. UNIX for Dummies Questions & Answers

Assigning the output of a command to a variable, where there may be >1 line returned?

Hello I am using unix CLI commands for the Synergy CM software. The command basically searches for a folder ID and returns the names of the projects the folder sits in. The result is assigned to a variable: FIND_USE=`ccm folder -fu -u -f "%name"-"%version" ${FOLDER_ID}` When the command... (6 Replies)
Discussion started by: Glyn_Mo
6 Replies

8. Shell Programming and Scripting

How to assign value to a variable with row(using -n) returned by sed

Hi Friends, REQUIREMENT: Want to delete files from the current directory match with the same in the file test.txt set -x i=1 echo "i=$i" COUNT=`sed -n '$=' test.txt` echo "Count=$COUNT" while do "## Here is error##" FILETOREMOVE=`sed -n \'$i,1p\' test.txt` echo $FILETOREMOVE... (5 Replies)
Discussion started by: sourabhsharma
5 Replies

9. Shell Programming and Scripting

Passing global variable to a function which is called by another function

Hi , I have three funcions f1, f2 and f3 . f1 calls f2 and f2 calls f3 . I have a global variable "period" which i want to pass to f3 . Can i pass the variable directly in the definition of f3 ? Pls help . sars (4 Replies)
Discussion started by: sars
4 Replies

10. Shell Programming and Scripting

retrieve a value from a function and assign it to a variable

hi i have a function. parse() { qname="lpdma111" qmgr="lpdma111" qalias="lpqqali" } Now the requirement is i want ot get the vaues form the function start() { //retireve the values from the parse function } (5 Replies)
Discussion started by: Satyak
5 Replies
Login or Register to Ask a Question