how_to_pas_and_return_values_to_and_from_functions
Table of Contents
If you want to send us your comments, please do so. Thanks
More on comments
How to pas and return values to and from functions
The eval statement is used to send results form the function to the calling program
Example
The main program
The main program is named program.using.function.sh
#! /bin/bash # Declare the function library file . /home/user/libprogram.using.function.sh # Variables number1=7 number2=8 number3=9 number4=10 result="" other="" calculation="" # Calling the function. Sending 4 and receiving 3 variables testfunction $number1 $number2 $number3 $number4 result other calculation echo returnvalue: $? echo -e "$result\n$other\n$calculation"
The library file with the function
The library file with the function is called libprogram.using.function.sh
#! /bin/bash function testfunction() { # Letting the user know that this is the function echo Start function # Variables # Input variables local fn_number1=$1 local fn_number2=$2 local fn_number3=$3 local fn_number4=$4 # Variables for the results local fn_result1=$5 local fn_result2=$6 local fn_result3=$7 # Display the contents of the variables echo $1 $2 $3 $4 $5 $6 $7 # Variables of which the value will be returned by the function with a value given local fn_variable1='Tekst 145' local fn_variable2='An other text with number 169' local fn_calculation=$(( $fn_number1 * $fn_number2 * $fn_number3 * $fn_number4 )) # Show the contents of these variables echo fn_variable 2x: $fn_variable1 $fn_variable2 echo Result of calculation: $fn_calculation # Set the return values eval $fn_result1="'$fn_variable1'" eval $fn_result2="'$fn_variable2'" eval $fn_result3="'$fn_calculation'" # Display the return values echo fn_result 3x: $fn_result1 $fn_result2 $fn_result3 # Show that the function is ready, finished echo finish function # Return the exit code (a number between 0 and 256). Here we choose 27 return 27 }
Note that the values used in the eval commands have to be a variable. So eval $fn_result4=“'Hello'“ does not work
Main subjects on this wiki: Linux, Debian, HTML, Microcontrollers, Privacy
RSS
Disclaimer
Privacy statement
Bugs statement
Cookies
Copyright © : 2014 - 2024 Webevaluation.nl and the authors
Changes reserved.
how_to_pas_and_return_values_to_and_from_functions.txt · Last modified: 29-04-2018 23:18 by wim