#!/bin/bash # 'getCurrentStat to sort' simultaneously on many target using bash V5+. # Sample of distributing complex bash commands through many hosts, using ssh # then datas are get/retrieved then formated remotelly, finally all outputs, # are simply sorted to be printed out. # Note: As all output are sent on only one line, race condition mixing # output of different target on same line seem unlikely possible, # for more robust solution see the 'getCurrentStat bash for sh' alternative. # (C) 2023-2024 Felix Hauri - felix@f-hauri.ch # Licensed under terms of GPL v3. www.gnu.org ## Note: Host list is ignored if arguments (list of user@hosts) are submited!! ## To use an external file replace the following by: exec {hostList} [varname] local p=000$(( ${1}00000/$2 )); # shellcheck disable=SC2059 disable=SC2086 printf ${3+-v} $3 %.2f ${p::-3}.${p: -3} } textSize(){ # usage: textSize [varname] local i=$(( $1 >= 1<<50 ? 5 : $1 >= 1<<40 ? 4 : $1 >= 1<<30 ? 3 : $1 >= 1<<20 ? 2 : $1 > 1023 ? 1 : 0 )) a=(K M G T P) # shellcheck disable=SC1105 disable=SC2210 disable=SC2035 (( i > 4 ? i+=-2 : 0 )) && a=( "${a[@]:2}" ) && set -- $(( $1>>20 )) "$2" local r=00$(( 1000 * $1 / ( 1024**i ) )) # shellcheck disable=SC2059 disable=SC2086 printf -v $2 %.2f%s ${r::-3}.${r: -3} ${a[i]} } collectCpuMemDiskStat() { # Collect infos, format and print them. local order=$1 shopt -s extglob read -r upt idl