-
Notifications
You must be signed in to change notification settings - Fork 8
/
function.sh
executable file
·93 lines (72 loc) · 1.55 KB
/
function.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# complete examples - autocomplete.sh
print_title(){
echo "my own title"
}
print_title
echo $(print_title)
# execute function by name
echo `print_title`
echo $("print_title")
function_for_execution=`echo "print_title"`
$function_for_execution
# --------
# function should return Integers only, checking return value via $?
print_invitation(){
echo "this is invitation for $1"
# you can return only digits
return 5
}
print_invitation 'John'
echo $?
# --------
# function can 'return' string
generate_body() {
cat <<EOF
"string from function"
EOF
}
echo ">>>> $(generate_body)"
# ---------
# function set argument by name
set_argument_by_name(){
eval "$1='value from function'"
}
set_argument_by_name my_variable
echo $my_variable
# ----------
# function set return value to global variable
set_global_var(){
return_value="value from function 'set_global_var'"
}
set_global_var
echo $return_value
# ---------
# function alias
function ssh-staging-copy-from(){
if [[ -z "$1" ]]; then
echo "mandatory first parameter is empty"
return 1
fi
}
function staging-ssh-copy-from(){
ssh-staging-copy-from $*
}
# ----------
# function array argument
function function_with_array_argument() {
mvn_modules=""
for java_module in ${1}
do
mvn_modules+=":${java_module},"
done
echo "${mvn_modules}"
changed_files_list=$(echo "${mvn_modules[@]}" | tr '\n' ' ')
echo "${changed_files_list}"
}
java_modules=(
"interval"
"marker"
"search"
"video"
)
result=$(function_with_array_argument "${java_modules[@]}")