-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbash-lib-misc
118 lines (106 loc) · 2.19 KB
/
bash-lib-misc
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
#!/bin/bash
#
# Misc bash utility functions
#
# Dependency: bash-lib-logging
#
source lib/bash-lib-logging
__initMiscLibrary()
{
(( ${__MISC_LIB_INITIALIZED__} )) && return
readonly __MISC_LIB_INITIALIZED__=1
}
push_dir()
{
local directoryName=${1}
if [ -z "${directoryName}" ]; then
abort "Direcotry name not provide."
fi
if [ ! -d "${directoryName}" ]; then
abort "Direcotry doesn't exist."
fi
pushd ${directoryName} > /dev/null 2>&1
if (( $? )); then
abort "Failed to push directory [${directoryName}] on to stack."
fi
}
pop_dir()
{
popd > /dev/null 2>&1
if (( $? )); then
abort "Possible coding error."
fi
}
createDirectory()
{
local directoryName=${1}
if [ -z "${directoryName}" ]; then
abort "Direcotry name not provide."
fi
if [ ! -d "${directoryName}" ]; then
mkdir -p ${directoryName}
if (( $? )); then
abort "Failed to create directory [${directoryName}]"
fi
fi
}
clearDirectory()
{
local directoryName=${1}
if [ -z "${directoryName}" ]; then
abort "Direcotry name not provide."
fi
if [ ! -d "${directoryName}" ]; then
abort "Direcotry doesn't exist."
fi
if [ -d "${directoryName}" ]; then
if (( $(ls -A ${WRK_DIR} | wc -l) > 0 )); then
log "Work directory is not empty. Clearing it..."
push_dir ${directoryName}
rm -rf ./*
local error=${?}
pop_dir
if (( ${error} )); then
abort "Failed to clear directory [${directoryName}]"
fi
fi
else
createDirectory ${directoryName}
fi
}
#
# NOTE: doesn't work right...
#
getFullPath()
{
local file=${1}
local fullPath=""
if [ -z "${file}" ]; then
abort "Fire/directory name not provide."
fi
if [ -d "${file}" ]; then
pushd "${file}" > /dev/null
fullPath=$(pwd)
popd
elif [ -f "${file}" ]; then
pushd "$(dirname ${file})" > /dev/null
fullPath="$(pwd)/$(basename ${file})"
popd
else
abort "File/directory [${file}] doesn't exist."
fi
echo "${fullPath}"
}
#
# Get host's IP address
#
getHostIP()
{
case "$OSTYPE" in
linux-gnu) ip route get 8.8.8.8 | awk '{print $7}' ;;
darwin*) ipconfig getifaddr $(route get 8.8.8.8 | awk '/interface: / {print $2; }') ;;
*) echo "Unsupported OS: $OSTYPE. Unable to get host IP address" ;;
esac
}
__initMiscLibrary
#::END::