BAEL-3451: Organized code snippets

This commit is contained in:
Sorin Zamfir 2020-01-12 11:13:00 +02:00
parent f86e225d16
commit 09a183cd7a
1 changed files with 59 additions and 73 deletions

View File

@ -1,10 +1,6 @@
#!/bin/bash #!/bin/bash
# Subsection 2.1
# simple input and output
# variable shadowing
# nesting and recursion
simple_function() { simple_function() {
echo "First" echo "First"
for ((i=0;i<5;++i)) do for ((i=0;i<5;++i)) do
@ -19,44 +15,11 @@ function simple_function {
done done
} }
# missing brackets still works
# as long as we have compound commands
function simple_for_loop() function simple_for_loop()
for ((i=0;i<5;++i)) do for ((i=0;i<5;++i)) do
echo -n " "$i" "; echo -n " "$i" ";
done 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() function simple_comparison()
if [[ "$1" -lt 5 ]]; then if [[ "$1" -lt 5 ]]; then
echo "$1 is smaller than 5" echo "$1 is smaller than 5"
@ -64,47 +27,58 @@ function simple_comparison()
echo "$1 is greater than 5" echo "$1 is greater than 5"
fi fi
# command groups with subshells # Subsection 2.2
# with the limitation of new enviornments function simple_inputs() {
echo "This is the first argument [$1]"
echo "This is the second argument [$2]"
echo "Calling function with $# aruments"
}
# Subsection 2.3
sum=0 sum=0
function simple_subshell() 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 declare -n sum_ref=$3
sum_ref=$(($1+$2)) sum_ref=$(($1+$2))
# sum=$(($1+$2)) }
)
# variable shadowing # Subsection 3.1
variable="baeldung" variable="baeldung"
function variable_scope2(){
echo "Variable inside function variable_scope2 : [$variable]"
local variable="ipsum"
}
function variable_scope(){ function variable_scope(){
local variable="lorem" local variable="lorem"
echo "Variable inside function variable_scope : [$variable]" echo "Variable inside function variable_scope : [$variable]"
variable_scope2 variable_scope2
} }
function variable_scope2(){ # Subsection 3.2
echo "Variable inside function variable_scope2 : [$variable]" sum=0
local variable="ipsum" function simple_subshell()
variable_scope3 (
} sum=$(($1+$2))
)
function variable_scope3(){ function simple_subshell_ref()
echo "Variable inside function variable_scope3 : [$variable]" (
} declare -n sum_ref=$3
sum_ref=$(($1+$2))
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
}
# Subsection 3.3
function redirection_in() { function redirection_in() {
# echo "$1"
while read input; while read input;
do do
echo "$input" echo "$input"
@ -112,13 +86,13 @@ function redirection_in() {
} < infile } < infile
function redirection_in_ps() { function redirection_in_ps() {
while read input; read
while read -a input;
do do
echo "$input" echo "${input[2]} ${input[8]}"
done done
} < <(ls -ll /) } < <(ls -ll /)
function redirection_out_ps(){ function redirection_out_ps(){
declare -a output=("baeldung" "lorem" "ipsum" "caracg") declare -a output=("baeldung" "lorem" "ipsum" "caracg")
for element in "${output[@]}" for element in "${output[@]}"
@ -135,6 +109,18 @@ function redirection_out() {
done done
} > outfile } > 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_function
# simple_inputs one 'two three' # simple_inputs one 'two three'
# sum=$(simple_outputs 1 2) # sum=$(simple_outputs 1 2)
@ -145,8 +131,8 @@ function redirection_out() {
# simple_for_loop # simple_for_loop
# simple_comparison 6 # simple_comparison 6
# simple_comparison 4 # simple_comparison 4
simple_subshell 1 2 sum # simple_subshell 1 2 sum
echo "Sum is $sum" # echo "Sum is $sum"
#variable_scope #variable_scope
# echo "Variable outside function variable_scope : [$variable]" # echo "Variable outside function variable_scope : [$variable]"