SHARE
TWEET

Untitled

a guest Sep 13th, 2017 45 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function fun1(){
  2.   return 34
  3. }
  4. function fun2(){
  5.   local res=$(fun1)
  6.   echo $res
  7. }
  8.    
  9. function fun1(){
  10.   echo 34
  11. }
  12.  
  13. function fun2(){
  14.   local res=$(fun1)
  15.   echo $res
  16. }
  17.    
  18. function fun1(){
  19.   return 34
  20. }
  21.  
  22. function fun2(){
  23.   fun1
  24.   local res=$?
  25.   echo $res
  26. }
  27.    
  28. fun1 (){
  29.   return 34
  30. }
  31.  
  32. fun2 (){
  33.   fun1
  34.   local res=$?
  35.   echo $res
  36. }
  37.    
  38. encrypt() {
  39.     gpg -c -o- $1 # encrypt data in filename to stdout (asks for a passphrase)
  40. }
  41.  
  42. encrypt public.dat > private.dat # write function result to file
  43.    
  44. function fun1(){
  45.   return 34
  46. }
  47.  
  48. function fun2(){
  49.   local res=$(fun1)
  50.   echo $? # <-- Always echos 0 since the 'local' command passes.
  51.  
  52.   res=$(fun1)
  53.   echo $?  #<-- Outputs 34
  54. }
  55.    
  56. POINTER= # used for function return values
  57.  
  58. my_function() {
  59.     # do stuff
  60.     POINTER="my_function_return"
  61. }
  62.  
  63. my_other_function() {
  64.     # do stuff
  65.     POINTER="my_other_function_return"
  66. }
  67.  
  68. my_function
  69. RESULT="$POINTER"
  70.  
  71. my_other_function
  72. RESULT="$POINTER"
  73.    
  74. my_function() {
  75.     echo "-> my_function()"
  76.     # do stuff
  77.     POINTER="my_function_return"
  78.     echo "<- my_function. $POINTER"
  79. }
  80.    
  81. #!/bin/bash
  82.  
  83. ##A 6-element array used for returning
  84. ##values from functions:
  85. declare -a RET_ARR
  86. RET_ARR[0]="A"
  87. RET_ARR[1]="B"
  88. RET_ARR[2]="C"
  89. RET_ARR[3]="D"
  90. RET_ARR[4]="E"
  91. RET_ARR[5]="F"
  92.  
  93.  
  94. function FN_MULTIPLE_RETURN_VALUES(){
  95.  
  96.    ##give the positional arguments/inputs
  97.    ##$1 and $2 some sensible names:
  98.    local out_dex_1="$1" ##output index
  99.    local out_dex_2="$2" ##output index
  100.  
  101.    ##Echo for debugging:
  102.    echo "running: FN_MULTIPLE_RETURN_VALUES"
  103.  
  104.    ##Here: Calculate output values:
  105.    local op_var_1="Hello"
  106.    local op_var_2="World"
  107.  
  108.    ##set the return values:
  109.    RET_ARR[ $out_dex_1 ]=$op_var_1
  110.    RET_ARR[ $out_dex_2 ]=$op_var_2
  111. }
  112.  
  113.  
  114. echo "FN_MULTIPLE_RETURN_VALUES EXAMPLES:"
  115. echo "-------------------------------------------"
  116. fn="FN_MULTIPLE_RETURN_VALUES"
  117. out_dex_a=0
  118. out_dex_b=1
  119. eval $fn $out_dex_a $out_dex_b  ##<--Call function
  120. a=${RET_ARR[0]} && echo "RET_ARR[0]: $a "
  121. b=${RET_ARR[1]} && echo "RET_ARR[1]: $b "
  122. echo
  123. ##----------------------------------------------##
  124. c="2"
  125. d="3"
  126. FN_MULTIPLE_RETURN_VALUES $c $d ##<--Call function
  127. c_res=${RET_ARR[2]} && echo "RET_ARR[2]: $c_res "
  128. d_res=${RET_ARR[3]} && echo "RET_ARR[3]: $d_res "
  129. echo
  130. ##----------------------------------------------##
  131. FN_MULTIPLE_RETURN_VALUES 4 5  ##<---Call function
  132. e=${RET_ARR[4]} && echo "RET_ARR[4]: $e "
  133. f=${RET_ARR[5]} && echo "RET_ARR[5]: $f "
  134. echo
  135. ##----------------------------------------------##
  136.  
  137.  
  138. read -p "Press Enter To Exit:"
  139.    
  140. FN_MULTIPLE_RETURN_VALUES EXAMPLES:
  141. -------------------------------------------
  142. running: FN_MULTIPLE_RETURN_VALUES
  143. RET_ARR[0]: Hello
  144. RET_ARR[1]: World
  145.  
  146. running: FN_MULTIPLE_RETURN_VALUES
  147. RET_ARR[2]: Hello
  148. RET_ARR[3]: World
  149.  
  150. running: FN_MULTIPLE_RETURN_VALUES
  151. RET_ARR[4]: Hello
  152. RET_ARR[5]: World
  153.  
  154. Press Enter To Exit:
RAW Paste Data
Top