From 8ddfcd34120b313cef46d299019ab46baeef7ece Mon Sep 17 00:00:00 2001 From: Sorin Zamfir Date: Fri, 3 Jan 2020 21:35:30 +0200 Subject: [PATCH 1/7] 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) From 50d7f321c5186782aa7d35d9ea9054c7877a396a Mon Sep 17 00:00:00 2001 From: Sorin Zamfir Date: Sun, 5 Jan 2020 00:35:53 +0200 Subject: [PATCH 2/7] BAEL-3451: Some more examples --- .../functions/src/main/bash/functions.sh | 60 ++++++++++++++----- 1 file changed, 45 insertions(+), 15 deletions(-) mode change 100644 => 100755 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 old mode 100644 new mode 100755 index 5c994e88be..33af9d3fcb --- a/linux-bash/functions/src/main/bash/functions.sh +++ b/linux-bash/functions/src/main/bash/functions.sh @@ -5,6 +5,26 @@ # variable shadowing # 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() { echo "This is the first argument [$1]" @@ -12,15 +32,21 @@ function simple_inputs() { 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() { 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 + echo $sum +} + +function ref_outputs() { + declare -n sum_ref=$3 + sum_ref=$(($1+$2)) } # missing brackets still works @@ -60,13 +86,17 @@ function fibonnaci_recursion() { } - -simple_inputs one two three -simple_outputs 1 2 -simple_for_loop -simple_comparison 6 -simple_comparison 4 -simple_subshell -echo $global_variable +#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" +# simple_for_loop +# simple_comparison 6 +# simple_comparison 4 +# simple_subshell +# echo $global_variable #echo $(fibonnaci_recursion 7) From f86e225d162ef8689f1def8dbbf5d6d702222d69 Mon Sep 17 00:00:00 2001 From: Sorin Zamfir Date: Sat, 11 Jan 2020 22:51:19 +0200 Subject: [PATCH 3/7] BAEL-3451: More samples --- .../functions/src/main/bash/functions.sh | 75 ++++++++++++++++--- linux-bash/functions/src/main/bash/infile | 3 + 2 files changed, 69 insertions(+), 9 deletions(-) create mode 100644 linux-bash/functions/src/main/bash/infile 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 From 09a183cd7a35912f6aa4939d25611b7405ad038d Mon Sep 17 00:00:00 2001 From: Sorin Zamfir Date: Sun, 12 Jan 2020 11:13:00 +0200 Subject: [PATCH 4/7] BAEL-3451: Organized code snippets --- .../functions/src/main/bash/functions.sh | 132 ++++++++---------- 1 file changed, 59 insertions(+), 73 deletions(-) diff --git a/linux-bash/functions/src/main/bash/functions.sh b/linux-bash/functions/src/main/bash/functions.sh index affe168310..64007f38d8 100755 --- a/linux-bash/functions/src/main/bash/functions.sh +++ b/linux-bash/functions/src/main/bash/functions.sh @@ -1,10 +1,6 @@ #!/bin/bash - -# simple input and output -# variable shadowing -# nesting and recursion - +# Subsection 2.1 simple_function() { echo "First" for ((i=0;i<5;++i)) do @@ -19,44 +15,11 @@ function simple_function { 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() { - echo "This is the first argument [$1]" - echo "This is the second argument [$2]" - echo "Calling function with $# aruments" -} - -# global_variable="lorem" -# sum=0 -# function simple_outputs() { -# sum=$(($1+$2)) -# global_variable="dolor" -# } - -function simple_outputs() { - sum=$(($1+$2)) - echo $sum -} - -function ref_outputs() { - declare -n sum_ref=$3 - sum_ref=$(($1+$2)) -} - -# 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" @@ -64,47 +27,58 @@ function simple_comparison() echo "$1 is greater than 5" fi -# command groups with subshells -# with the limitation of new enviornments -sum=0 -function simple_subshell() - ( - declare -n sum_ref=$3 - sum_ref=$(($1+$2)) - # sum=$(($1+$2)) - ) +# Subsection 2.2 +function simple_inputs() { + echo "This is the first argument [$1]" + echo "This is the second argument [$2]" + echo "Calling function with $# aruments" +} -# variable shadowing +# Subsection 2.3 +sum=0 +function simple_outputs() { + sum=$(($1+$2)) +} + +function simple_outputs() { + sum=$(($1+$2)) + echo $sum +} + +# Subsection 2.4 +function ref_outputs() { + declare -n sum_ref=$3 + sum_ref=$(($1+$2)) +} + +# Subsection 3.1 variable="baeldung" +function variable_scope2(){ + echo "Variable inside function variable_scope2 : [$variable]" + local variable="ipsum" +} + 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 -} +# Subsection 3.2 +sum=0 +function simple_subshell() + ( + sum=$(($1+$2)) + ) -function variable_scope3(){ - echo "Variable inside function variable_scope3 : [$variable]" -} - -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 -} +function simple_subshell_ref() + ( + declare -n sum_ref=$3 + sum_ref=$(($1+$2)) + ) +# Subsection 3.3 function redirection_in() { - # echo "$1" while read input; do echo "$input" @@ -112,13 +86,13 @@ function redirection_in() { } < infile function redirection_in_ps() { - while read input; + read + while read -a input; do - echo "$input" + echo "${input[2]} ${input[8]}" done } < <(ls -ll /) - function redirection_out_ps(){ declare -a output=("baeldung" "lorem" "ipsum" "caracg") for element in "${output[@]}" @@ -135,6 +109,18 @@ function redirection_out() { done } > outfile +# Subsection 3.4 +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_function # simple_inputs one 'two three' # sum=$(simple_outputs 1 2) @@ -145,8 +131,8 @@ function redirection_out() { # simple_for_loop # simple_comparison 6 # simple_comparison 4 -simple_subshell 1 2 sum -echo "Sum is $sum" +# simple_subshell 1 2 sum +# echo "Sum is $sum" #variable_scope # echo "Variable outside function variable_scope : [$variable]" From 362be8fc7a69a55844b0e8ca8e25ab2baa4ecb38 Mon Sep 17 00:00:00 2001 From: Sorin Zamfir Date: Sun, 12 Jan 2020 12:14:36 +0200 Subject: [PATCH 5/7] BAEL-3451: Included menu. Not complete --- .../functions/src/main/bash/functions.sh | 110 +++++++++++++++--- 1 file changed, 95 insertions(+), 15 deletions(-) diff --git a/linux-bash/functions/src/main/bash/functions.sh b/linux-bash/functions/src/main/bash/functions.sh index 64007f38d8..4446113ab3 100755 --- a/linux-bash/functions/src/main/bash/functions.sh +++ b/linux-bash/functions/src/main/bash/functions.sh @@ -2,14 +2,12 @@ # Subsection 2.1 simple_function() { - echo "First" for ((i=0;i<5;++i)) do echo -n " "$i" "; done } -function simple_function { - echo "Second" +function simple_function_no_parantheses { for ((i=0;i<5;++i)) do echo -n " "$i" "; done @@ -35,18 +33,18 @@ function simple_inputs() { } # Subsection 2.3 -sum=0 -function simple_outputs() { - sum=$(($1+$2)) +global_sum=0 +function global_sum_outputs() { + global_sum=$(($1+$2)) } -function simple_outputs() { - sum=$(($1+$2)) +function cs_sum_outputs() { + sum=$(($1+$2)) echo $sum } # Subsection 2.4 -function ref_outputs() { +function arg_ref_sum_outputs() { declare -n sum_ref=$3 sum_ref=$(($1+$2)) } @@ -54,27 +52,29 @@ function ref_outputs() { # Subsection 3.1 variable="baeldung" function variable_scope2(){ - echo "Variable inside function variable_scope2 : [$variable]" + echo "Variable inside function variable_scope2: [$variable]" local variable="ipsum" } function variable_scope(){ local variable="lorem" - echo "Variable inside function variable_scope : [$variable]" + echo "Variable inside function variable_scope: [$variable]" variable_scope2 } # Subsection 3.2 -sum=0 -function simple_subshell() +subshell_sum=0 +function simple_subshell_sum() ( - sum=$(($1+$2)) + subshell_sum=$(($1+$2)) + echo "Value of sum in function with global variables: [$subshell_sum]" ) -function simple_subshell_ref() +function simple_subshell_ref_sum() ( declare -n sum_ref=$3 sum_ref=$(($1+$2)) + echo "Value of sum in function with ref arguments: [$sum_ref]" ) # Subsection 3.3 @@ -121,6 +121,86 @@ function fibonnaci_recursion() { fi } +# main menu entry point +echo "****Functions samples menu*****" +PS3="Your choice (1,2,3 etc.):" +options=("function_definitions" "function_input_args" "function_outputs" \ + "function_variables" "function_subshells" "function_redirections" \ + "function_recursion" "quit") +select option in "${options[@]}" +do + case $option in + "function_definitions") + echo -e "\n" + echo "**Different ways to define a function**" + echo -e "No function keyword:" + simple_function + echo -e "\nNo function parantheses:" + simple_function_no_parantheses + echo -e "\nOmitting curly braces:" + simple_for_loop + echo -e "\n" + ;; + "function_input_args") + echo -e "\n" + echo "**Passing inputs to a function**" + simple_inputs lorem ipsum + echo -e "\n" + ;; + "function_outputs") + echo -e "\n" + echo "**Getting outputs from a function**" + global_sum_outputs 1 2 + echo -e "1+2 using global variables: [$global_sum]" + cs_sum=$(cs_sum_outputs 1 2) + echo -e "1+2 using command substitution: [$cs_sum]" + arg_ref_sum_outputs 1 2 arg_ref_sum + echo -e "1+2 using argument references: [$arg_ref_sum]" + echo -e "\n" + ;; + "function_variables") + echo -e "\n" + echo "**Overriding variable scopes**" + echo "Global value of variable: [$variable]" + variable_scope + echo -e "\n" + ;; + "function_subshells") + echo -e "\n" + echo "**Running function in subshell**" + echo "Global value of sum: [$subshell_sum]" + simple_subshell_sum 1 2 + echo "Value of sum after subshell function with \ +global variables: [$subshell_sum]" + subshell_sum_arg_ref=0 + simple_subshell_ref_sum 1 2 subshell_sum_arg_ref + echo "Value of sum after subshell function with \ +ref arguments: [$subshell_sum_arg_ref]" + echo -e "\n" + ;; + "function_redirections") + echo -e "\n" + echo "**Function redirections**" + echo -e "File input redirection:" + redirection_in + echo -e "Command input redirection:" + redirection_in_ps + ;; + # "timeout_input_read") + # echo "Enter something in 5 seconds or less" + # timeout_input_read + # ;; + # "exactly_n_read") + # echo "Enter at least 11 characters or wait 5 seconds" + # exactly_n_read + # ;; + "quit") + break + ;; + *) echo "Invalid option";; + esac +done + #simple_function # simple_inputs one 'two three' # sum=$(simple_outputs 1 2) From 9f2a1688b248c93f0cf311d903730be279958f48 Mon Sep 17 00:00:00 2001 From: Sorin Zamfir Date: Sun, 12 Jan 2020 15:08:54 +0200 Subject: [PATCH 6/7] BAEL-3451: Working version --- .../functions/src/main/bash/functions.sh | 61 +++++++------------ 1 file changed, 23 insertions(+), 38 deletions(-) diff --git a/linux-bash/functions/src/main/bash/functions.sh b/linux-bash/functions/src/main/bash/functions.sh index 4446113ab3..4b90de0959 100755 --- a/linux-bash/functions/src/main/bash/functions.sh +++ b/linux-bash/functions/src/main/bash/functions.sh @@ -89,7 +89,7 @@ function redirection_in_ps() { read while read -a input; do - echo "${input[2]} ${input[8]}" + echo "User[${input[2]}]->File[${input[8]}]" done } < <(ls -ll /) @@ -151,11 +151,11 @@ do echo -e "\n" echo "**Getting outputs from a function**" global_sum_outputs 1 2 - echo -e "1+2 using global variables: [$global_sum]" + echo -e ">1+2 using global variables: [$global_sum]" cs_sum=$(cs_sum_outputs 1 2) - echo -e "1+2 using command substitution: [$cs_sum]" + echo -e ">1+2 using command substitution: [$cs_sum]" arg_ref_sum_outputs 1 2 arg_ref_sum - echo -e "1+2 using argument references: [$arg_ref_sum]" + echo -e ">1+2 using argument references: [$arg_ref_sum]" echo -e "\n" ;; "function_variables") @@ -181,45 +181,30 @@ ref arguments: [$subshell_sum_arg_ref]" "function_redirections") echo -e "\n" echo "**Function redirections**" - echo -e "File input redirection:" + echo -e ">Function input redirection from file:" redirection_in - echo -e "Command input redirection:" + echo -e ">Function input redirection from command:" redirection_in_ps + echo -e ">Function output redirection to file:" + redirection_out + cat outfile + echo -e ">Function output redirection to command:" + red_ps=$(redirection_out_ps) + echo "$red_ps" + echo -e "\n" + ;; + "function_recursion") + echo -e "\n" + echo "**Function recursion**" + fibo_res1=$(fibonnaci_recursion 7) + echo "The 7th Fibonnaci number: [$fibo_res1]" + fibo_res2=$(fibonnaci_recursion 15) + echo "The 15th Fibonnaci number: [$fibo_res2]" + echo -e "\n" ;; - # "timeout_input_read") - # echo "Enter something in 5 seconds or less" - # timeout_input_read - # ;; - # "exactly_n_read") - # echo "Enter at least 11 characters or wait 5 seconds" - # exactly_n_read - # ;; "quit") break ;; *) echo "Invalid option";; esac -done - -#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" -# simple_for_loop -# simple_comparison 6 -# simple_comparison 4 -# simple_subshell 1 2 sum -# echo "Sum is $sum" - -#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 +done \ No newline at end of file From 135e9359bdfd0b238dea8cfc9b30c8c667cfc9ee Mon Sep 17 00:00:00 2001 From: Sorin Zamfir Date: Tue, 14 Jan 2020 22:27:23 +0200 Subject: [PATCH 7/7] BAEL-3451: Fixed formatting --- .../functions/src/main/bash/functions.sh | 26 +++++++++---------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/linux-bash/functions/src/main/bash/functions.sh b/linux-bash/functions/src/main/bash/functions.sh index 4b90de0959..41ff0ca434 100755 --- a/linux-bash/functions/src/main/bash/functions.sh +++ b/linux-bash/functions/src/main/bash/functions.sh @@ -51,12 +51,12 @@ function arg_ref_sum_outputs() { # Subsection 3.1 variable="baeldung" -function variable_scope2(){ +function variable_scope2() { echo "Variable inside function variable_scope2: [$variable]" local variable="ipsum" } -function variable_scope(){ +function variable_scope() { local variable="lorem" echo "Variable inside function variable_scope: [$variable]" variable_scope2 @@ -64,18 +64,16 @@ function variable_scope(){ # Subsection 3.2 subshell_sum=0 -function simple_subshell_sum() - ( - subshell_sum=$(($1+$2)) - echo "Value of sum in function with global variables: [$subshell_sum]" - ) +function simple_subshell_sum() ( + subshell_sum=$(($1+$2)) + echo "Value of sum in function with global variables: [$subshell_sum]" +) -function simple_subshell_ref_sum() - ( - declare -n sum_ref=$3 - sum_ref=$(($1+$2)) - echo "Value of sum in function with ref arguments: [$sum_ref]" - ) +function simple_subshell_ref_sum() ( + declare -n sum_ref=$3 + sum_ref=$(($1+$2)) + echo "Value of sum in function with ref arguments: [$sum_ref]" +) # Subsection 3.3 function redirection_in() { @@ -93,7 +91,7 @@ function redirection_in_ps() { done } < <(ls -ll /) -function redirection_out_ps(){ +function redirection_out_ps() { declare -a output=("baeldung" "lorem" "ipsum" "caracg") for element in "${output[@]}" do