-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconvert_val
executable file
·207 lines (192 loc) · 4.46 KB
/
convert_val
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/bin/bash
#
# License
#
# Copyright (C) 2025 David Valin [email protected]
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# Convert various time and memory units.
usage()
{
echo "usage $1 <unit>"
echo "-h --help --usage: This help message"
echo "--value <value>: Value to be converted."
echo "--from_unit <u>: Unit of value to be converted"
echo " Default K if --time_val is not specified, otherwise ns"
echo "--time_val: Units being worked with are time vals"
echo "--to_unit <u>: Unit to convert to."
echo " Default B if --time_val is not specified, otherwise s"
echo "If just --from_unit is passed, program returns the unit value. EG. --from_unit K will return 1000B"
echo "If --value and --from_unit, program returns the value in the base unit."
echo "If --value, --from_unit and --to_unit is passed, then the program will convert <value><unit> to"
echo " the unit designated in --to_unit"
echo ""
echo "Example usage:"
echo "$ $1 --from_unit M --value 1024 --to_unit G"
echo "1G"
echo "$ $1 --from_unit M --value 1024 --to_unit K"
echo "1024000K"
echo "$ $1 --from_unit Mi --value 1024 --to_unit Ki"
echo "1048576Ki"
echo "$ $1 --from_unit Mi --value 1024 --to_unit Gi"
echo "1Gi"
echo "$ $1 --from_unit Mi"
echo "1000000B"
echo "$ $1 --time_val --from_unit s --value 10 --to_unit ms"
echo "10000ms"
echo "$ $1 --time_val --from_unit s --value 60 --to_unit m"
echo "1m"
echo "$ $1 --time_val --from_unit s"
echo "1000000000ns"
exit 1
}
if [[ $1 == "" ]]; then
usage $0
fi
NOARG_OPTS=(
"help"
"time_val"
"usage"
)
ARG_OPTS=(
"to_unit"
"value"
"from_unit"
)
opts=$(getopt \
--longoptions "$(printf "%s," "${NOARG_OPTS[@]}")" \
--longoptions "$(printf "%s:," "${ARG_OPTS[@]}")" \
--name "$(basename "$0")" \
--options "hc:" \
-- "$@"
)
eval set --$opts
if [ $? -ne 0 ]; then
exit 1
fi
#
# Set the unit defaults.
#
value=1
unit_val=""
cnv_to=""
rval=""
from_unit=""
to_unit=""
time_val=0
while [[ $# -gt 0 ]]; do
case "$1" in
-h | --usage | --help)
usage $0
exit 0
;;
--to_unit)
to_unit=$2
shift 2
;;
--time_val)
time_val=1
shift 1
;;
--value)
value=$2
shift 2
;;
--from_unit)
from_unit=$2
shift 2
;;
--)
break
;;
*)
echo Unknown option $1
usage $0
;;
esac
done
if [[ $to_unit == "" ]]; then
if [ $time_val -eq 0 ]; then
to_unit="B"
else
to_unit="ns"
fi
fi
if [[ $from_unit == "" ]]; then
if [ $time_val -eq 0 ]; then
from_unit="K"
else
from_unit="s"
fi
fi
cnvt_to_bytes()
{
lunit=$1
if [[ $lunit == "B" ]] || [[ $lunit == "b" ]]; then
base_value=1
else
if [[ $lunit =~ [K,M,G,T] ]] && [[ $lunit != *"i"* ]]; then
base_value=`numfmt --from=si 1${lunit}`
if [[ $? -ne 0 ]]; then
echo Error: Invalid unit $lunit
exit 1
fi
else
#
# Assumption is iec and convert to appropriate value
#
lunit=`echo "${lunit^^}" | sed "s/I//g"`
base_value=`numfmt --from=iec 1${lunit}`
if [[ $? -ne 0 ]]; then
echo Error: Invalid unit $lunit
exit 1
fi
fi
fi
echo $base_value
}
cnvt_unit_to_nsec()
{
in_unit=$1
if [[ $in_unit == "h"* ]]; then
echo 3600000000000
elif [[ $in_unit == "ms"* ]]; then
echo 1000000
elif [[ $in_unit == "m"* ]]; then
echo 60000000000
elif [[ $in_unit == "s"* ]]; then
echo 1000000000
elif [[ $in_unit == "u"* ]]; then
echo 1000
elif [[ $in_unit == "n"* ]]; then
echo 1
else
echo Error: Invalid unit $in_unit
exit 1
fi
}
if [ $time_val -eq 0 ]; then
to_base_unit=$(cnvt_to_bytes $from_unit)
converting_to=$(cnvt_to_bytes $to_unit)
cnv_value=`echo \(${value}*${to_base_unit}\)/$converting_to | bc`
echo ${cnv_value}${to_unit}
else
time_base_unit=$(cnvt_unit_to_nsec $from_unit)
time_cnvt_to=$(cnvt_unit_to_nsec $to_unit)
cnv_value=`echo \(${value}*${time_base_unit}\)/$time_cnvt_to | bc`
echo ${cnv_value}${to_unit}
fi
exit 0