diff --git a/linux-bash/functions/src/main/bash/functions.sh b/linux-bash/functions/src/main/bash/functions.sh index 33af9d3fcb..affe168310 100755 --- a/linux-bash/functions/src/main/bash/functions.sh +++ b/linux-bash/functions/src/main/bash/functions.sh @@ -66,14 +66,32 @@ function simple_comparison() # command groups with subshells # with the limitation of new enviornments +sum=0 function simple_subshell() ( - echo "I'm a little tea pot" - simple_comparison 10 - cd .. - global_variable="ipsum" + declare -n sum_ref=$3 + sum_ref=$(($1+$2)) + # sum=$(($1+$2)) ) +# variable shadowing +variable="baeldung" +function variable_scope(){ + local variable="lorem" + echo "Variable inside function variable_scope : [$variable]" + variable_scope2 +} + +function variable_scope2(){ + echo "Variable inside function variable_scope2 : [$variable]" + local variable="ipsum" + variable_scope3 +} + +function variable_scope3(){ + echo "Variable inside function variable_scope3 : [$variable]" +} + function fibonnaci_recursion() { argument=$1 if [[ "$argument" -eq 0 ]] || [[ "$argument" -eq 1 ]]; then @@ -85,18 +103,57 @@ function fibonnaci_recursion() { fi } +function redirection_in() { + # echo "$1" + while read input; + do + echo "$input" + done +} < infile + +function redirection_in_ps() { + while read input; + do + echo "$input" + done +} < <(ls -ll /) + + +function redirection_out_ps(){ + declare -a output=("baeldung" "lorem" "ipsum" "caracg") + for element in "${output[@]}" + do + echo "$element" + done +} > >(grep "g") + +function redirection_out() { + declare -a output=("baeldung" "lorem" "ipsum") + for element in "${output[@]}" + do + echo "$element" + done +} > outfile #simple_function # simple_inputs one 'two three' # sum=$(simple_outputs 1 2) # echo "Sum is $sum" # sum=0 -ref_outputs 1 9 sumt -echo "Sum is $sumt" +# ref_outputs 1 9 sumt +# echo "Sum is $sumt" # simple_for_loop # simple_comparison 6 # simple_comparison 4 -# simple_subshell -# echo $global_variable +simple_subshell 1 2 sum +echo "Sum is $sum" -#echo $(fibonnaci_recursion 7) +#variable_scope +# echo "Variable outside function variable_scope : [$variable]" +# FUNCNEST=5 +# echo $(fibonnaci_recursion 7) +# echo $(fibonnaci_recursion 15) +# redirection_in +# redirection_in_ps +# redirection_out +# redirection_out_ps diff --git a/linux-bash/functions/src/main/bash/infile b/linux-bash/functions/src/main/bash/infile new file mode 100644 index 0000000000..b1fa680af4 --- /dev/null +++ b/linux-bash/functions/src/main/bash/infile @@ -0,0 +1,3 @@ +Honda Insight 2010 +Honda Element 2006 +Chevrolet Avalanche 2002