-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.sh
More file actions
executable file
·137 lines (123 loc) · 2.5 KB
/
functions.sh
File metadata and controls
executable file
·137 lines (123 loc) · 2.5 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/bin/bash
RESTORE=$'\033[0m'
RED=$'\033[00;31m'
GREEN=$'\033[00;32m'
YELLOW=$'\033[00;33m'
BLUE=$'\033[00;34m'
PURPLE=$'\033[00;35m'
CYAN=$'\033[00;36m'
LIGHTGRAY=$'\033[00;37m'
LRED=$'\033[01;31m'
LGREEN=$'\033[01;32m'
LYELLOW=$'\033[01;33m'
LBLUE=$'\033[01;34m'
LPURPLE=$'\033[01;35m'
LCYAN=$'\033[01;36m'
WHITE=$'\033[01;37m'
echo "${GREEN}=============="
echo "## FUNCTION ##"
echo "==============${RESTORE}"
echo "${YELLOW}Usage:${RESTORE}"
cat <<EOF
function function-name () {
# Code goes here.
}
or
function-name () {
# Code goes here. }
}
function-name
EOF
echo "${BLUE}Example:${RESTORE}"
out='function hello () {
echo "Hello!"
}
hello'
echo "${out}"
echo "${GREEN}TEST:${RESTORE}"
eval "${out}"
echo
echo "${GREEN}=============="
echo "### NESTED ###"
echo "==============${RESTORE}"
echo "${YELLOW}Usage:${RESTORE}"
cat <<EOF
function-name_1 () {
# Code goes here.
function-name_2
}
function-name_2 () {
# Code goes here.
}
function-name_1
EOF
echo "${LRED}You cannot call main function before nested!${RESTORE}"
echo "${BLUE}Example:${RESTORE}"
out='hello () {
echo "Hello!"
now
}
now () {
echo "It is $(date +%r)"
}
hello'
echo "${out}"
echo "${GREEN}TEST:${RESTORE}"
eval "${out}"
echo
echo -e "${GREEN}Functions mainly use 2 types of vars:\n${YELLOW}-LOCAL${RESTORE} - works only within function\n${YELLOW}-GLOBAL${RESTORE} - usual vars, works outside and within function."
echo "${YELLOW}Usage:${RESTORE}"
cat <<EOF
function-name_1 () {
# Code goes here.
local VAR=var
}
function-name
EOF
echo "${BLUE}Example:${RESTORE}"
out='backup_file () {
if [ -f "$1" ]
then
local BACKUP_FILE="/var/tmp/$(basename ${1}).$(date +%F).$$"
echo "Backing up $1 to ${BACKUP_FILE}"
cp $1 $BACKUP_FILE
fi
}
backup_file /etc/hosts'
echo "${out}"
echo "${GREEN}TEST:${RESTORE}"
eval "${out}"
echo
echo -e "${GREEN}Functions are using return statement and following them\nwith the status you would like to return.${RESTORE}"
cat <<EOF
function-name_1 () {
# Code goes here.
return #
}
function-name
EOF
echo "${BLUE}Example:${RESTORE}"
out='backup_file () {
if [ -f "$1" ]
then
local BACKUP_FILE="/var/tmp/$(basename ${1}).$(date +%F).$$"
echo "Backing up $1 to ${BACKUP_FILE}"
cp $1 $BACKUP_FILE
else
# The file does not exist, so return
# a non-zero exit status.
return 1
fi
}
backup_file /etc/hosts
if [ $? -eq "0" ]
then
echo "Backup succeeded!"
else
echo "Backup failed!"
#Abort the script and return a non-zero status.
exit 1
fi'
echo "${out}"
echo "${GREEN}TEST:${RESTORE}"
eval "${out}"