-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils
More file actions
executable file
·134 lines (115 loc) · 3.67 KB
/
utils
File metadata and controls
executable file
·134 lines (115 loc) · 3.67 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
#!/usr/bin/env bash
#
# ############################################################################
# Project: xSHELL (none)
# File...: utils
# Created: Thursday, 2021/05/20 - 00:26:27
# Author.: Fabiano Matos, fgm (fabiano.matoz@gmail.com)
# ~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~~·~·~·~·~·~·~·~
# Last Modified: Tuesday, 2025/03/04 - 15:36:05
# Modified By..: @fbnmtz, (fabiano.matoz@gmail.com)
# ~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~~·~·~·~·~·~·~·~
# Version: 1.0.12.517
# ~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~~·~·~·~·~·~·~·~
# Description:
# >
# ############################################################################
# HISTORY:
#
_xLIB_UTILS_=true;
# using A && B || C
# # running cmds create a block {} and use 'true'; inside.
# # This will avoid executing the part after || by accident when the code between && and || fails.
# shellcheck disable=SC2120,2015,2016
# ~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~~·~·~·~·~·~·~·~
# methods
# ~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~~·~·~·~·~·~·~·~
getPath(){
# DEPRECATED: moved to system as xsys.path
for tool in "$@"; do
if which "$tool" &> /dev/null; then
which "$tool"
return 0
fi
done
return 1
}
# function to return a basepath from a file
# @param $1 -> file: /etc/hosts
# @return path to file -> /etc
basepath(){
# DEPRECATED: moved to system as xsys.baspath
echo "$1" | rev | cut -d '/' -f2-30 | rev
}
# prints a table with supported colors
rainbow(){
for (( i = 0; i < 8; i++ )); do
echo -e "\033[0;3${i}m FG: (0;3$i); \033[1;3${i}m FG-Light: (1;3$i) -> \033[0;4${i}m BG: (0;4$i); \033[1;4${i}m BG-Light: (1;4$i) \033[0m "
done
}
# ternary operator
# tern "expr" "true value" "false value"
tern(){
if eval "$1"; then echo "$2"; else echo "$3"; fi
}
# function to check if current user is ROOT
isROOT?(){
if [ $UID = 0 ] ; then
echo true
else
echo false
fi
}
# function to require ROOT user
requireROOT(){
_xLASTUSER_=$(last | head -n1 | awk '{print $1}')
if [ $UID = 0 ] ; then
return 0
else
echo "Error! You need to be ROOT to run '$APP'."
exit 1
fi
}
# print msg with dots
dots() {
local pad=$(printf "%0.1s" "."{1..60})
printf " * %s%*.*s" "$1" 0 $((60-${#1})) "$pad"
return 0
}
# search and return a git config (local or global)
# getGit user.name author.name
getGit(){
local git_cmd="git config" _git_config
for c in "$@" ; do
_git_config=$($git_cmd "$c")
[ -n "$_git_config" ] && { echo "$_git_config" ; break; }
_git_config=$($git_cmd --global "$c")
[ -n "$_git_config" ] && { echo "$_git_config" ; break; }
done
}
# column replacement
awkColumn(){
xrequirements awk cut
local arg ARGS
local char="$(echo $1 | cut -d '=' -f2)"; shift
while read -r arg; do
ARGS+=($arg)
done
echo -e "${ARGS[@]}" | awk -v FS="$char" -v OFS="\t" '
{
for (i = 1; i <= NF; i++) {
col_width[i] = (length($i) > col_width[i]) ? length($i) : col_width[i];
data[NR, i] = $i;
}
row_count = NR;
col_count = (NF > col_count) ? NF : col_count;
}
END {
for (i = 1; i <= row_count; i++) {
for (j = 1; j <= col_count; j++) {
printf "%-*s%s", col_width[j], data[i, j], (j < col_count ? OFS : "");
}
print "";
}
}'
}