-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbash_templates
173 lines (140 loc) · 3.26 KB
/
bash_templates
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/bin/bash
########################## - BASH TEMPLATES - ###############################
#_ section_variables
########## _header files_ ###########
#source ./<path/somefilename>
#source ./<path/somefilename>
########### _variables_ #############
a1=$1; a2=$2; a3=$3; a4=$4
#_ section_functions
########### _functions_ #############
#_ section_main
############## _main_ ###############
#_ regex_comparison
# for re or check if number
if [[ $1 =~ ^[0-9] ]]; then
echo "$1 is an integer between 1-9"
fi
#_ missing_requirements_exit
# error function
function error(){
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2
}
# didnt supply any arguments when executing script
if [[ $# -lt 0 ]]; then
err "Missing required argument\n"
exit 1
fi
# did not run as root
if [[ $UID == 1 ]];then
err "Error: you must be root to run this script, try sudo <scriptname>"
exit 1
fi
#_ skeleton_main_while_if
function main(){ # <arg1> <arg2>
local input1
local input2
input1=$1
input2=$2
# infinite loop
while :
do
if [[ $1 == 1 ]]; then
#something
elif [[ $2 < 1 ]]; then
#something
else
#something
fi
done
}
#_ skeleton_while
while :
do
#something
done
#_ skeleton_for_loop
for i in $1
do
#something
done
#_ skeleton_int_if_statement
if (( $num > 10 )); then
#something
fi
#_ skeleton_if_statement
if [[ "$item" !== "" ]] || [[ "$item2" == "" ]]; then
#something
elif [[ "$item" == "" ]]; then
#something
else
#something
fi
#_ skeleton_for_if_nested
for i in $item
do
if [[ "$i" == "" ]]; then
#something
fi
done
#_ skeleton_if_for_nested
if [[ -n "$item" ]]; then
for i in $item
do
echo "$i"
done
fi
#_ library_of_basic_functions
function print_date(){
date +"%D %M %Y" | awk '{print $1}'
}
pd=$(print_date)
function print_time(){
date | awk '{print $5}' | tr ":" " " | awk '{print $1" "$2}' | tr " " ":"
}
pt=$(print_time)
function delfile(){
local file1
file1=$1
if [ -f $file ]; then
rm "$file"
else
printf "\n\033[31mError:\033[36m cant delete, file does not exist\033[0m\n"
fi
}
function percentage(){
local part
local whole
part=$1
whole=$2
echo $(( part * 100 / whole ))
}
# library_of_complex_functions
function trimstring() {
# trim leading and trailing white space
: "${1#"${1%%[![:space:]]*}"}"
: "${%"${##*[![:space:]]}"}"
printf '%s\n' "$"
}
function regex() {
# Usage: regex "string" "regex"
[[ $1 =~ $2 ]] && printf '%s\n' "${BASH_REMATCH[1]}"
}
function upper() {
# change string to uppercase
echo ${1}|tr "[[:lower:]]" "[[:upper:]]"
}
function lower() {
# if upper to lower
echo ${1}|tr "[[:upper:]]" "[[:lower:]]"
}
function trim_quotes() {
# Usage: trimquotes "string"
: "${1//'}"
printf '%s\n' "${//"}"
}
function strip_all() {
# Usage: stripall "string" "pattern"
printf '%s\n' "${1//$2}" # //all, /one, instance of
}
# end