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() {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
sum=0
|
|
|
|
function simple_outputs() {
|
|
|
|
sum=$(($1+$2))
|
|
|
|
}
|
2020-01-04 17:35:53 -05:00
|
|
|
|
2020-01-03 14:35:30 -05:00
|
|
|
function simple_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-04 17:35:53 -05:00
|
|
|
function ref_outputs() {
|
|
|
|
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"
|
|
|
|
function variable_scope2(){
|
|
|
|
echo "Variable inside function variable_scope2 : [$variable]"
|
|
|
|
local variable="ipsum"
|
|
|
|
}
|
|
|
|
|
2020-01-12 04:13:00 -05:00
|
|
|
function variable_scope(){
|
|
|
|
local variable="lorem"
|
|
|
|
echo "Variable inside function variable_scope : [$variable]"
|
|
|
|
variable_scope2
|
2020-01-11 15:51:19 -05:00
|
|
|
}
|
|
|
|
|
2020-01-12 04:13:00 -05:00
|
|
|
# Subsection 3.2
|
|
|
|
sum=0
|
|
|
|
function simple_subshell()
|
|
|
|
(
|
|
|
|
sum=$(($1+$2))
|
|
|
|
)
|
|
|
|
|
|
|
|
function simple_subshell_ref()
|
|
|
|
(
|
|
|
|
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.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 04:13:00 -05:00
|
|
|
echo "${input[2]} ${input[8]}"
|
2020-01-11 15:51:19 -05:00
|
|
|
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
|
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-04 17:35:53 -05:00
|
|
|
#simple_function
|
|
|
|
# simple_inputs one 'two three'
|
|
|
|
# sum=$(simple_outputs 1 2)
|
|
|
|
# echo "Sum is $sum"
|
|
|
|
# sum=0
|
2020-01-11 15:51:19 -05:00
|
|
|
# ref_outputs 1 9 sumt
|
|
|
|
# echo "Sum is $sumt"
|
2020-01-04 17:35:53 -05:00
|
|
|
# simple_for_loop
|
|
|
|
# simple_comparison 6
|
|
|
|
# simple_comparison 4
|
2020-01-12 04:13:00 -05:00
|
|
|
# simple_subshell 1 2 sum
|
|
|
|
# echo "Sum is $sum"
|
2020-01-11 15:51:19 -05:00
|
|
|
|
|
|
|
#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
|