-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunktions.sh
More file actions
141 lines (126 loc) · 3.85 KB
/
funktions.sh
File metadata and controls
141 lines (126 loc) · 3.85 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
138
139
140
141
#!/bin/bash
############################################################
## screen output
############################################################
NC='\033[0m' # No Color
RED='\033[0;31m'
GREEN='\033[0;32m'
BROWN='\033[0;33m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
WHITE='\033[0;37m'
infoscreen() {
printf "%-.80s" $(printf "${BROWN}$1 ${BLUE}$2 : .................................................................................${NC}") 1>&3
}
infoscreendone() {
printf " ${GREEN}DONE${NC}\n" 1>&3
}
infoscreenfailed() {
printf " ${RED}FAILED${NC}\n" 1>&3
}
############################################################
## System tools
############################################################
get_default_hostname() {
# Guess the machine's hostname. It should be a fully qualified
# domain name suitable for DNS. None of these calls may provide
# the right value, but it's the best guess we can make.
set -- $(hostname --fqdn 2>/dev/null ||
hostname --all-fqdns 2>/dev/null ||
hostname 2>/dev/null)
printf '%s\n' "$1" # return this value
}
## Uncapitalize a string
lower() {
# $1 required a string
# return an uncapitalize string
if [ ! -n ${1:-} ]; then
echo "lower() requires the a string as the first argument"
return 1;
fi
echo $1 | tr '[:upper:]' '[:lower:]'
}
get_publicip_from_web_service() {
curl -$1 --fail --silent --max-time 15 icanhazip.com 2>/dev/null || /bin/true
}
system_get_user_home() {
# $1 required a user name
# return user hame path
cat /etc/passwd | grep "^$1:" | cut --delimiter=":" -f6
}
## Delete domain in /etc/hosts
hostname_delete() {
# $1 required a domain name
if [ ! -n ${1:-} ]; then
echo "hostname_delete() requires the domain name as the first argument"
return 1;
fi
if [ -z "$1" ]; then
local newhost=${1//./\\.}
sed -i "/$newhost/d" /etc/hosts
fi
}
############################################################
## Net tools
############################################################
kill_prosses_port() {
## kill prosses that is listen to port number
# $1 required a port number
kill $(fuser -n tcp $1 2> /dev/null)
}
############################################################
## Param tools
############################################################
update_param_boolean() {
# $1 required a file path
# $2 required a search term
# $3 required a boolean value
# $4 option set a comment at the end of line
if [ ! -n ${1:-} ]; then
echo "update_param_boolean() requires the file path as the first argument"
return 1;
fi
if [ ! -n ${2:-} ]; then
echo "update_param_boolean() requires the search term as the second argument"
return 1;
fi
if [ ! -n ${3:-} ]; then
echo "update_param_boolean() requires the boolean value as the third argument"
return 1;
fi
if [ -n ${4:-} ]; then
local comments="# $4"
fi
VALUE=`lower $3`
case $3 in
yes|no|on|off|true|false)
grep -q $2 $1 && sed -i "s/^#*\($2\).*/\1 $3/" $1 || echo "$2 $3 ${comments:-}" >> $1
;;
*)
echo "I dont think this $3 is a boolean"
return 1
;;
esac
}
update_param() {
# $1 required a file path
# $2 required a search term
# $3 required a string to replace
# $4 option set a comment at the end of line
if [ ! -n ${1:-} ]; then
echo "update_param() requires the file path as the first argument"
return 1;
fi
if [ ! -n ${2:-} ]; then
echo "comment_param() requires the search term as the second argument"
return 1;
fi
if [ ! -n ${3:-} ]; then
echo "comment_param() requires a string value as the third argument"
return 1;
fi
if [ -n ${4:-} ]; then
local comments="# $4"
fi
grep -q $2 $1 && sed -i "s/^#*\($2\).*/$3 $2/g" $1 || echo "$3 $2 ${comments:-}" >> $1
}