-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbaseutils.sh
executable file
·170 lines (147 loc) · 2.48 KB
/
baseutils.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#
# Copyright (c) Sebastian Kucharczyk <kuchen@kekse.biz>
# https://kekse.biz/ https://github.com/kekse1/scripts/
# v0.4.1
#
#
mkcd()
{
if [ $# -eq 0 ]; then
echo "Syntax: mkcd <directory>" >&2
return 1
fi
mkdir -pv "$*" || return $?
cd "$*"
pwd
}
# like `Date.now()`: milliseconds
now()
{
echo "$((`date +%s%N`/1000000))"
}
# nanoseconds (you could also perfectly `alias` this)
nano()
{
echo "`date +%s%N`"
}
from()
{
if [[ $# -lt 2 ]]; then
echo "Syntax: below <from line> <input file>" >&2
return 1
elif [[ ! "$1" =~ ^[0-9]+$ ]]; then
echo "First parameter needs to be a positive integer." >&2
return 2
elif [[ $1 -lt 0 ]]; then # TODO: evtl. koennen negative auch sinnvoll sein?!!?
echo "First parameter may not be below zero." >&2
return 3
elif [[ -z "$2" ]]; then
echo "The input file parameter may not be empty." >&2
return 4
fi
local from=$1
shift
local file="$*"
if [[ ! -r "$file" ]]; then
echo "Your input file is not readable (or doesn't even exist)." >&2
return 5
fi
cat "$file" | tail -n$((`cat "$file" | wc -l`-$from+1))
}
#
#TODO#my *better* version of it..
#
extname()
{
local result="`basename "$1"`"
result=".${result#*.}"
echo "$result"
}
relative()
{
# relative path $from $to
echo TODO >&2
return 255
}
absolute()
{
[[ "${1::1}" == "/" ]] && return 0
return 1
}
line()
{
local w=`width`; if [[ $w -eq 0 ]]; then echo; return; fi
IFS=$'\n'; line="$*"; [[ -z "$line" ]] && line="$LINE"; [[ -z "$line" ]] && line="="
local i; local mod; for (( i=0; i<$w; ++i )); do
mod=$((${i}%${#line}));
echo -n "${line:${mod}:1}"
done; echo
}
width()
{
if [[ -n "$COLS" ]]; then
echo $COLS
elif [[ -n "$COLUMNS" ]]; then
echo $COLUMNS
else
tput cols 2>/dev/null
if [[ $? -ne 0 ]]; then
echo 0
return 1
fi
fi
}
height()
{
if [[ -n "$LINES" ]]; then
echo $LINES
else
tput lines 2>/dev/null
if [[ $? -ne 0 ]]; then
echo 0
return 1
fi
fi
}
eol()
{
echo TODO >&2
return 255
#using $count
}
pad()
{
echo TODO >&2
return 255
# am einfachsten `printf` nutzen, hm?
# sonst selbst machen.. ^_^
}
repeat()
{
echo TODO >&2
return 255
# `repeat $count $*`
# vs. stdin '-'!??
}
yesno()
{
echo TODO >&2
return 255
# ask as long as no real "y[es]/n[o]"!
}
lower()
{
echo "${*,,}"
}
upper()
{
echo "${*^^}"
}
#
confirm()
{
[[ -n "$1" ]] && echo -ne "$1 [Yes/No]? "
local confirm; read confirm; confirm="${confirm::1}"; confirm="${confirm,,}"
[[ "$confirm" != "y" ]] && return 1
return 0
}