BAEL-3451: Some more examples

This commit is contained in:
Sorin Zamfir 2020-01-05 00:35:53 +02:00
parent 8ddfcd3412
commit 50d7f321c5
1 changed files with 45 additions and 15 deletions

60
linux-bash/functions/src/main/bash/functions.sh Normal file → Executable file
View File

@ -5,6 +5,26 @@
# variable shadowing # variable shadowing
# nesting and recursion # nesting and recursion
simple_function() {
echo "First"
for ((i=0;i<5;++i)) do
echo -n " "$i" ";
done
}
function simple_function {
echo "Second"
for ((i=0;i<5;++i)) do
echo -n " "$i" ";
done
}
# missing brackets still works
# as long as we have compound commands
function simple_for_loop()
for ((i=0;i<5;++i)) do
echo -n " "$i" ";
done
function simple_inputs() { function simple_inputs() {
echo "This is the first argument [$1]" echo "This is the first argument [$1]"
@ -12,15 +32,21 @@ function simple_inputs() {
echo "Calling function with $# aruments" echo "Calling function with $# aruments"
} }
global_variable="lorem" # global_variable="lorem"
# sum=0
# function simple_outputs() {
# sum=$(($1+$2))
# global_variable="dolor"
# }
function simple_outputs() { function simple_outputs() {
sum=$(($1+$2)) sum=$(($1+$2))
echo "Sum is $sum" echo $sum
global_variable="dolor" }
# this is just the status code
# does not work if we want to return function ref_outputs() {
# other than numerical declare -n sum_ref=$3
return $sum sum_ref=$(($1+$2))
} }
# missing brackets still works # missing brackets still works
@ -60,13 +86,17 @@ function fibonnaci_recursion() {
} }
#simple_function
simple_inputs one two three # simple_inputs one 'two three'
simple_outputs 1 2 # sum=$(simple_outputs 1 2)
simple_for_loop # echo "Sum is $sum"
simple_comparison 6 # sum=0
simple_comparison 4 ref_outputs 1 9 sumt
simple_subshell echo "Sum is $sumt"
echo $global_variable # simple_for_loop
# simple_comparison 6
# simple_comparison 4
# simple_subshell
# echo $global_variable
#echo $(fibonnaci_recursion 7) #echo $(fibonnaci_recursion 7)