-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVlib.sh
executable file
·95 lines (80 loc) · 1.89 KB
/
Vlib.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
93
94
. Vglobals
# check if element exists in array
# arg1 element
# arg2,3,4...$# array
function EXISTS_IN_ARRAY()
{
if (($# < 2)); then
echo "EXISTS_IN_ARRAY: at least 2 args must be provided";
exit 1;
fi
# element 1 is our tested parameter
for idx in {2..$#}; do
if [ "${@[1]}" = "${@[idx]}" ]; then
return 0;
fi
done
return 1;
}
# picks prefered interface
# arg1 {int(internal), ext(external)}
# returns preffered interface if exists
# ext -> int -> NULL
function PICK_WIRELESS_INTERFACE()
{
if [[ $1 != "int" && $1 != "ext" ]]; then
echo "PICK_WIRELESS_INTERFACE: invalid argument" >&2;
exit 1;
fi
interfaces=($(ip link show | grep -e "^[1-9]\+: wlan" | cut -d ' ' -f 2 | tr ':' '\0'));
if [ $1 = "int" ]; then
if EXISTS_IN_ARRAY $BUILT_IN_INTERFACE $interfaces; then
echo "$BUILT_IN_INTERFACE";
else
echo "NULL";
fi
else
# find interface, which is not internal
choice="NULL";
for inter in $interfaces; do
if [ "$inter" != "$BUILT_IN_INTERFACE" ]; then
choice="$inter";
break;
fi
done
echo "$choice";
fi
}
# ensures cache folder exists
# arg1 file name in cache folder
# arg2 default value if file doesn't exist
# returns full path to file with provided filename in PATH_TO_CACHE_FOLDER
function REQUIRE_CACHE()
{
if [ "$#" -ne 2 ]; then
echo "REQUIRE_CACHE: file name and default value must be provided";
exit 1;
fi
if ! [ -d "$PATH_TO_CACHE_FOLDER" ]; then
mkdir -p "$PATH_TO_CACHE_FOLDER";
if ! [ -d "$PATH_TO_CACHE_FOLDER" ]; then
echo "REQUIRE_CACHE: couldn't create cache folder";
exit 1;
fi
fi
local file="$PATH_TO_CACHE_FOLDER/$1";
if ! [ -f "$file" ]; then
echo "$2" > "$file";
if ! [ -f "$file" ]; then
echo "REQUIRE_CACHE: couldn't create cache file";
exit 1;
fi
fi
echo "$PATH_TO_CACHE_FOLDER/$1";
}
#prints arguments to both notify-send and echo
function NOTIFY_FULL()
{
echo "$@";
notify-send "$@";
}