Skip to content

Commit 1abef2a

Browse files
authored
Merge pull request #8513 from srzamfir/feature/BAEL-3451_bash_functions
Feature/bael 3451 bash functions
2 parents 314cdfc + 135e935 commit 1abef2a

2 files changed

Lines changed: 211 additions & 0 deletions

File tree

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
#!/bin/bash
2+
3+
# Subsection 2.1
4+
simple_function() {
5+
for ((i=0;i<5;++i)) do
6+
echo -n " "$i" ";
7+
done
8+
}
9+
10+
function simple_function_no_parantheses {
11+
for ((i=0;i<5;++i)) do
12+
echo -n " "$i" ";
13+
done
14+
}
15+
16+
function simple_for_loop()
17+
for ((i=0;i<5;++i)) do
18+
echo -n " "$i" ";
19+
done
20+
21+
function simple_comparison()
22+
if [[ "$1" -lt 5 ]]; then
23+
echo "$1 is smaller than 5"
24+
else
25+
echo "$1 is greater than 5"
26+
fi
27+
28+
# Subsection 2.2
29+
function simple_inputs() {
30+
echo "This is the first argument [$1]"
31+
echo "This is the second argument [$2]"
32+
echo "Calling function with $# aruments"
33+
}
34+
35+
# Subsection 2.3
36+
global_sum=0
37+
function global_sum_outputs() {
38+
global_sum=$(($1+$2))
39+
}
40+
41+
function cs_sum_outputs() {
42+
sum=$(($1+$2))
43+
echo $sum
44+
}
45+
46+
# Subsection 2.4
47+
function arg_ref_sum_outputs() {
48+
declare -n sum_ref=$3
49+
sum_ref=$(($1+$2))
50+
}
51+
52+
# Subsection 3.1
53+
variable="baeldung"
54+
function variable_scope2() {
55+
echo "Variable inside function variable_scope2: [$variable]"
56+
local variable="ipsum"
57+
}
58+
59+
function variable_scope() {
60+
local variable="lorem"
61+
echo "Variable inside function variable_scope: [$variable]"
62+
variable_scope2
63+
}
64+
65+
# Subsection 3.2
66+
subshell_sum=0
67+
function simple_subshell_sum() (
68+
subshell_sum=$(($1+$2))
69+
echo "Value of sum in function with global variables: [$subshell_sum]"
70+
)
71+
72+
function simple_subshell_ref_sum() (
73+
declare -n sum_ref=$3
74+
sum_ref=$(($1+$2))
75+
echo "Value of sum in function with ref arguments: [$sum_ref]"
76+
)
77+
78+
# Subsection 3.3
79+
function redirection_in() {
80+
while read input;
81+
do
82+
echo "$input"
83+
done
84+
} < infile
85+
86+
function redirection_in_ps() {
87+
read
88+
while read -a input;
89+
do
90+
echo "User[${input[2]}]->File[${input[8]}]"
91+
done
92+
} < <(ls -ll /)
93+
94+
function redirection_out_ps() {
95+
declare -a output=("baeldung" "lorem" "ipsum" "caracg")
96+
for element in "${output[@]}"
97+
do
98+
echo "$element"
99+
done
100+
} > >(grep "g")
101+
102+
function redirection_out() {
103+
declare -a output=("baeldung" "lorem" "ipsum")
104+
for element in "${output[@]}"
105+
do
106+
echo "$element"
107+
done
108+
} > outfile
109+
110+
# Subsection 3.4
111+
function fibonnaci_recursion() {
112+
argument=$1
113+
if [[ "$argument" -eq 0 ]] || [[ "$argument" -eq 1 ]]; then
114+
echo $argument
115+
else
116+
first=$(fibonnaci_recursion $(($argument-1)))
117+
second=$(fibonnaci_recursion $(($argument-2)))
118+
echo $(( $first + $second ))
119+
fi
120+
}
121+
122+
# main menu entry point
123+
echo "****Functions samples menu*****"
124+
PS3="Your choice (1,2,3 etc.):"
125+
options=("function_definitions" "function_input_args" "function_outputs" \
126+
"function_variables" "function_subshells" "function_redirections" \
127+
"function_recursion" "quit")
128+
select option in "${options[@]}"
129+
do
130+
case $option in
131+
"function_definitions")
132+
echo -e "\n"
133+
echo "**Different ways to define a function**"
134+
echo -e "No function keyword:"
135+
simple_function
136+
echo -e "\nNo function parantheses:"
137+
simple_function_no_parantheses
138+
echo -e "\nOmitting curly braces:"
139+
simple_for_loop
140+
echo -e "\n"
141+
;;
142+
"function_input_args")
143+
echo -e "\n"
144+
echo "**Passing inputs to a function**"
145+
simple_inputs lorem ipsum
146+
echo -e "\n"
147+
;;
148+
"function_outputs")
149+
echo -e "\n"
150+
echo "**Getting outputs from a function**"
151+
global_sum_outputs 1 2
152+
echo -e ">1+2 using global variables: [$global_sum]"
153+
cs_sum=$(cs_sum_outputs 1 2)
154+
echo -e ">1+2 using command substitution: [$cs_sum]"
155+
arg_ref_sum_outputs 1 2 arg_ref_sum
156+
echo -e ">1+2 using argument references: [$arg_ref_sum]"
157+
echo -e "\n"
158+
;;
159+
"function_variables")
160+
echo -e "\n"
161+
echo "**Overriding variable scopes**"
162+
echo "Global value of variable: [$variable]"
163+
variable_scope
164+
echo -e "\n"
165+
;;
166+
"function_subshells")
167+
echo -e "\n"
168+
echo "**Running function in subshell**"
169+
echo "Global value of sum: [$subshell_sum]"
170+
simple_subshell_sum 1 2
171+
echo "Value of sum after subshell function with \
172+
global variables: [$subshell_sum]"
173+
subshell_sum_arg_ref=0
174+
simple_subshell_ref_sum 1 2 subshell_sum_arg_ref
175+
echo "Value of sum after subshell function with \
176+
ref arguments: [$subshell_sum_arg_ref]"
177+
echo -e "\n"
178+
;;
179+
"function_redirections")
180+
echo -e "\n"
181+
echo "**Function redirections**"
182+
echo -e ">Function input redirection from file:"
183+
redirection_in
184+
echo -e ">Function input redirection from command:"
185+
redirection_in_ps
186+
echo -e ">Function output redirection to file:"
187+
redirection_out
188+
cat outfile
189+
echo -e ">Function output redirection to command:"
190+
red_ps=$(redirection_out_ps)
191+
echo "$red_ps"
192+
echo -e "\n"
193+
;;
194+
"function_recursion")
195+
echo -e "\n"
196+
echo "**Function recursion**"
197+
fibo_res1=$(fibonnaci_recursion 7)
198+
echo "The 7th Fibonnaci number: [$fibo_res1]"
199+
fibo_res2=$(fibonnaci_recursion 15)
200+
echo "The 15th Fibonnaci number: [$fibo_res2]"
201+
echo -e "\n"
202+
;;
203+
"quit")
204+
break
205+
;;
206+
*) echo "Invalid option";;
207+
esac
208+
done
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Honda Insight 2010
2+
Honda Element 2006
3+
Chevrolet Avalanche 2002

0 commit comments

Comments
 (0)