nutools/ucalc

135 lines
5.1 KiB
Bash
Executable File

#!/bin/bash
# -*- coding: utf-8 mode: sh -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
function display_help() {
uecho "$scriptname: Afficher une valeur dans plusieurs unités
USAGE
$scriptname value
Sans suffixe, la valeur est exprimée en octets. Sinon, elle peut être suffixée
pour spécifier l'unité dans laquelle est exprimée la valeur:
K,M,G,T -- Kibi (1024), Mibi (1024^2), Gibi (1024^3), Tebi (1024^4)
k,m,g,t -- Kilo (1000), Mega (1000^2), Giga (1000^3), Tera (1000^4)
s -- secteurs de 512 octets
S -- secteurs de 2048 octets
p -- pages de 4096 octets
c -- cylindres (si l'option -c est spécifiée)
b -- octets
OPTIONS
-u UNIT
Spécifier l'unité de value. Le suffixe qui est éventuellement sur value
est ignoré.
-o UNIT
Spécifier l'unité en sortie. Par défaut, afficher la valeur dans toutes
les unités supportées.
-c VALUE
Taille d'un cylindre en octets, pour permettre l'affichage des valeurs
en cylindres"
}
source "$(dirname "$0")/ulib/ulib" &&
urequire DEFAULTS ||
exit 1
unit=
ounit=
cyl=
parse_opts "${PRETTYOPTS[@]}" \
--help '$exit_with display_help' \
-u:,--unit: unit= \
-o:,--ounit: ounit= \
-c: cyl= \
@ args -- "$@" && set -- "${args[@]}" || die "$args"
value="${1:-0}"
awkrun -f unit="$unit" ounit="$ounit" cyl:int="$cyl" <<<"$value" '
function normunit(unit) {
if (unit ~ /^([kK][oO]|[kK][iI][bB]?)$/) return "K";
else if (unit ~ /^([mM][oO]|[mM][iI][bB]?)$/) return "M";
else if (unit ~ /^([gG][oO]|[gG][iI][bB]?)$/) return "G";
else if (unit ~ /^([tT][oO]|[tT][iI][bB]?)$/) return "T";
else if (unit ~ /^([kK][bB])$/) return "k";
else if (unit ~ /^([mM][bB])$/) return "m";
else if (unit ~ /^([gG][bB])$/) return "g";
else if (unit ~ /^([tT][bB])$/) return "t";
else if (unit ~ /^([BoO])$/) return "b";
else return unit;
}
function addvalue(value, suffix, values, maxl, count) {
array_add(values, value suffix)
count = length(value "")
if (count > maxl) maxl = count
return maxl
}
function convert(value, divisor, unit, rdivisor, runit, values, maxl, d, r) {
if (!rdivisor) {
rdivisor = 1
runit = "octets"
}
d = int(value / divisor)
r = value % divisor
if (r == 0) return addvalue(d, " " unit, values, maxl)
r = r / rdivisor
return addvalue(d, " " unit " (reste " r " " runit ")", values, maxl)
}
function printvalues(values, maxl, count, i, value, padlen) {
count = length(values)
for (i = 1; i <= count; i++) {
value = values[i]
match(value, /^[0-9]+/)
padlen = RSTART + RLENGTH - 1
while (maxl > padlen) {
value = " " value
padlen = padlen + 1
}
print value
}
}
{
if (! match($0, /^[0-9]+/)) {
exit 1
}
value = int(substr($0, RSTART, RLENGTH))
if (unit == "") unit = substr($0, RLENGTH + 1)
unit = normunit(unit)
if (unit == "K") value = value * 1024
else if (unit == "M") value = value * 1024 * 1024
else if (unit == "G") value = value * 1024 * 1024 * 1024
else if (unit == "T") value = value * 1024 * 1024 * 1024 * 1024
else if (unit == "k") value = value * 1000
else if (unit == "m") value = value * 1000 * 1000
else if (unit == "g") value = value * 1000 * 1000 * 1000
else if (unit == "t") value = value * 1000 * 1000 * 1000 * 1000
else if (unit == "s") value = value * 512
else if (unit == "S") value = value * 2048
else if (unit == "p") value = value * 4096
else if (cyl != 0 && unit == "c") value = value * cyl
else if (unit == "" || unit == "b") ;
else exit 2
ounit = normunit(ounit)
array_new(values)
maxl = 0
if (ounit == "b" || ounit == "") maxl = addvalue(value, " octets", values, maxl)
if (ounit == "K" || ounit == "") maxl = convert(value, 1024, "KiB", 0, "", values, maxl)
if (ounit == "M" || ounit == "") maxl = convert(value, 1024 * 1024, "MiB", 1024, "KiB", values, maxl)
if (ounit == "G" || ounit == "") maxl = convert(value, 1024 * 1024 * 1024, "GiB", 1024 * 1024, "MiB", values, maxl)
if (ounit == "T" || ounit == "") maxl = convert(value, 1024 * 1024 * 1024 * 1024, "TiB", 1024 * 1024 * 1024, "GiB", values, maxl)
if (ounit == "k" || ounit == "") maxl = convert(value, 1000, "KB", 0, "", values, maxl)
if (ounit == "m" || ounit == "") maxl = convert(value, 1000 * 1000, "MB", 1000, "KB", values, maxl)
if (ounit == "g" || ounit == "") maxl = convert(value, 1000 * 1000 * 1000, "GB", 1000 * 1000, "MB", values, maxl)
if (ounit == "t" || ounit == "") maxl = convert(value, 1000 * 1000 * 1000 * 1000, "TB", 1000 * 1000 * 1000, "GB", values, maxl)
if (ounit == "s" || ounit == "") maxl = convert(value, 512, "secteurs", 0, "", values, maxl)
if (ounit == "S" || ounit == "") maxl = convert(value, 2048, "secteurs de 2048 octets", 0, "", values, maxl)
if (ounit == "p" || ounit == "") maxl = convert(value, 4096, "pages de 4096 octets", 0, "", values, maxl)
if ((ounit == "c" || ounit == "") && cyl != 0) maxl = convert(value, cyl, "cylindres", 512, "secteurs", values, maxl)
printvalues(values, maxl)
}' || case $? in
1) die "$value: valeur incorrecte";;
2) die "$value: unité incorrecte/non supportée";;
esac