From 8ddfcd34120b313cef46d299019ab46baeef7ece Mon Sep 17 00:00:00 2001 From: Sorin Zamfir Date: Fri, 3 Jan 2020 21:35:30 +0200 Subject: [PATCH] BAEL-3451: First samples --- .../functions/src/main/bash/functions.sh | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 linux-bash/functions/src/main/bash/functions.sh diff --git a/linux-bash/functions/src/main/bash/functions.sh b/linux-bash/functions/src/main/bash/functions.sh new file mode 100644 index 0000000000..5c994e88be --- /dev/null +++ b/linux-bash/functions/src/main/bash/functions.sh @@ -0,0 +1,72 @@ +#!/bin/bash + + +# simple input and output +# variable shadowing +# nesting and recursion + + +function simple_inputs() { + echo "This is the first argument [$1]" + echo "This is the second argument [$2]" + echo "Calling function with $# aruments" +} + +global_variable="lorem" +function simple_outputs() { + sum=$(($1+$2)) + echo "Sum is $sum" + global_variable="dolor" + # this is just the status code + # does not work if we want to return + # other than numerical + return $sum +} + +# missing brackets still works +# as long as we have compound commands +function simple_for_loop() + # echo "Looping through numbers" + for ((i=0;i<5;++i)) do + echo -n " "$i; + done + +function simple_comparison() + if [[ "$1" -lt 5 ]]; then + echo "$1 is smaller than 5" + else + echo "$1 is greater than 5" + fi + +# command groups with subshells +# with the limitation of new enviornments +function simple_subshell() + ( + echo "I'm a little tea pot" + simple_comparison 10 + cd .. + global_variable="ipsum" + ) + +function fibonnaci_recursion() { + argument=$1 + if [[ "$argument" -eq 0 ]] || [[ "$argument" -eq 1 ]]; then + echo $argument + else + first=$(fibonnaci_recursion $(($argument-1))) + second=$(fibonnaci_recursion $(($argument-2))) + echo $(( $first + $second )) + fi +} + + + +simple_inputs one two three +simple_outputs 1 2 +simple_for_loop +simple_comparison 6 +simple_comparison 4 +simple_subshell +echo $global_variable + +#echo $(fibonnaci_recursion 7)