forked from areyer/ffs-make-gateway
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathensureline.sh
48 lines (41 loc) · 890 Bytes
/
ensureline.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
ensureline() {
LINE="$1"
FILE="$2"
if [ ! -e "$FILE" ]; then
touch "$FILE" || return 1
fi
egrep -q '^'"$LINE"'$' "$FILE" || echo "$LINE" >> "$FILE" || return 1
}
ensureline_exp() {
LINE="$1"
FILE="$2"
if [ ! -e "$FILE" ]; then
touch "$FILE" || return 1
fi
fgrep -q "$LINE" "$FILE" || echo "$LINE" >> "$FILE" || return 1
}
ensureline_tr() {
LINE="$1"
FILE="$2"
if [ ! -e "$FILE" ]; then
touch "$FILE" || return 1
fi
egrep -q '^'"$LINE"'$' "$FILE" || echo "$LINE" | tr -d '\\' >> "$FILE" || return 1
}
ensureline_insert() {
LINE="$1"
FILE="$2"
if [ ! -e "$FILE" ]; then
touch "$FILE" || return 1
fi
egrep -q '^'"$LINE"'$' "$FILE" || sed -i "/^exit 0/i$LINE" "$FILE" || return 1
}
replaceline() {
LINE="$1"
LINE2="$2"
FILE="$3"
if [ ! -e "$FILE" ]; then
return 1
fi
sed -i "s/^$LINE/$LINE2/" "$FILE" || return 1
}