2020-01-03 14:35:30 -05:00
|
|
|
#!/bin/bash
|
|
|
|
|
2020-01-12 04:13:00 -05:00
|
|
|
# Subsection 2.1
|
2020-01-04 17:35:53 -05:00
|
|
|
simple_function() {
|
|
|
|
for ((i=0;i<5;++i)) do
|
|
|
|
echo -n " "$i" ";
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2020-01-12 05:14:36 -05:00
|
|
|
function simple_function_no_parantheses {
|
2020-01-04 17:35:53 -05:00
|
|
|
for ((i=0;i<5;++i)) do
|
|
|
|
echo -n " "$i" ";
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
function simple_for_loop()
|
|
|
|
for ((i=0;i<5;++i)) do
|
|
|
|
echo -n " "$i" ";
|
|
|
|
done
|
2020-01-03 14:35:30 -05:00
|
|
|
|
2020-01-12 04:13:00 -05:00
|
|
|
function simple_comparison()
|
|
|
|
if [[ "$1" -lt 5 ]]; then
|
|
|
|
echo "$1 is smaller than 5"
|
|
|
|
else
|
|
|
|
echo "$1 is greater than 5"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Subsection 2.2
|
2020-01-03 14:35:30 -05:00
|
|
|
function simple_inputs() {
|
|
|
|
echo "This is the first argument [$1]"
|
|
|
|
echo "This is the second argument [$2]"
|
|
|
|
echo "Calling function with $# aruments"
|
|
|
|
}
|
|
|
|
|
2020-01-12 04:13:00 -05:00
|
|
|
# Subsection 2.3
|
2020-01-12 05:14:36 -05:00
|
|
|
global_sum=0
|
|
|
|
function global_sum_outputs() {
|
|
|
|
global_sum=$(($1+$2))
|
2020-01-12 04:13:00 -05:00
|
|
|
}
|
2020-01-04 17:35:53 -05:00
|
|
|
|
2020-01-12 05:14:36 -05:00
|
|
|
function cs_sum_outputs() {
|
|
|
|
sum=$(($1+$2))
|
2020-01-04 17:35:53 -05:00
|
|
|
echo $sum
|
|
|
|
}
|
|
|
|
|
2020-01-12 04:13:00 -05:00
|
|
|
# Subsection 2.4
|
2020-01-12 05:14:36 -05:00
|
|
|
function arg_ref_sum_outputs() {
|
2020-01-04 17:35:53 -05:00
|
|
|
declare -n sum_ref=$3
|
|
|
|
sum_ref=$(($1+$2))
|
2020-01-03 14:35:30 -05:00
|
|
|
}
|
|
|
|
|
2020-01-12 04:13:00 -05:00
|
|
|
# Subsection 3.1
|
2020-01-11 15:51:19 -05:00
|
|
|
variable="baeldung"
|
2020-01-14 15:27:23 -05:00
|
|
|
function variable_scope2() {
|
2020-01-12 05:14:36 -05:00
|
|
|
echo "Variable inside function variable_scope2: [$variable]"
|
2020-01-11 15:51:19 -05:00
|
|
|
local variable="ipsum"
|
|
|
|
}
|
|
|
|
|
2020-01-14 15:27:23 -05:00
|
|
|
function variable_scope() {
|
2020-01-12 04:13:00 -05:00
|
|
|
local variable="lorem"
|
2020-01-12 05:14:36 -05:00
|
|
|
echo "Variable inside function variable_scope: [$variable]"
|
2020-01-12 04:13:00 -05:00
|
|
|
variable_scope2
|
2020-01-11 15:51:19 -05:00
|
|
|
}
|
|
|
|
|
2020-01-12 04:13:00 -05:00
|
|
|
# Subsection 3.2
|
2020-01-12 05:14:36 -05:00
|
|
|
subshell_sum=0
|
2020-01-14 15:27:23 -05:00
|
|
|
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]"
|
|
|
|
)
|
2020-01-03 14:35:30 -05:00
|
|
|
|
2020-01-12 04:13:00 -05:00
|
|
|
# Subsection 3.3
|
2020-01-11 15:51:19 -05:00
|
|
|
function redirection_in() {
|
|
|
|
while read input;
|
|
|
|
do
|
|
|
|
echo "$input"
|
|
|
|
done
|
|
|
|
} < infile
|
|
|
|
|
|
|
|
function redirection_in_ps() {
|
2020-01-12 04:13:00 -05:00
|
|
|
read
|
|
|
|
while read -a input;
|
2020-01-11 15:51:19 -05:00
|
|
|
do
|
2020-01-12 08:08:54 -05:00
|
|
|
echo "User[${input[2]}]->File[${input[8]}]"
|
2020-01-11 15:51:19 -05:00
|
|
|
done
|
|
|
|
} < <(ls -ll /)
|
|
|
|
|
2020-01-14 15:27:23 -05:00
|
|
|
function redirection_out_ps() {
|
2020-01-11 15:51:19 -05:00
|
|
|
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
|
2020-01-03 14:35:30 -05:00
|
|
|
|
2020-01-12 04:13:00 -05:00
|
|
|
# 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
|
|
|
|
}
|
|
|
|
|
2020-01-12 05:14:36 -05:00
|
|
|
# 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
|
2020-01-12 08:08:54 -05:00
|
|
|
echo -e ">1+2 using global variables: [$global_sum]"
|
2020-01-12 05:14:36 -05:00
|
|
|
cs_sum=$(cs_sum_outputs 1 2)
|
2020-01-12 08:08:54 -05:00
|
|
|
echo -e ">1+2 using command substitution: [$cs_sum]"
|
2020-01-12 05:14:36 -05:00
|
|
|
arg_ref_sum_outputs 1 2 arg_ref_sum
|
2020-01-12 08:08:54 -05:00
|
|
|
echo -e ">1+2 using argument references: [$arg_ref_sum]"
|
2020-01-12 05:14:36 -05:00
|
|
|
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**"
|
2020-01-12 08:08:54 -05:00
|
|
|
echo -e ">Function input redirection from file:"
|
2020-01-12 05:14:36 -05:00
|
|
|
redirection_in
|
2020-01-12 08:08:54 -05:00
|
|
|
echo -e ">Function input redirection from command:"
|
2020-01-12 05:14:36 -05:00
|
|
|
redirection_in_ps
|
2020-01-12 08:08:54 -05:00
|
|
|
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"
|
2020-01-12 05:14:36 -05:00
|
|
|
;;
|
|
|
|
"quit")
|
|
|
|
break
|
|
|
|
;;
|
|
|
|
*) echo "Invalid option";;
|
|
|
|
esac
|
2020-01-12 08:08:54 -05:00
|
|
|
done
|