Hi,
Can anyone please show me how to assign the returned value of a function to a variable? Thanks.
Hi,
Can anyone please show me how to assign the returned value of a function to a variable? Thanks.
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
Here is one example::
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
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.