I wrote a script to show down- and up-speed of my notebook with polybar. The problem I run into is to put the output of echo
in formation.
ATM my output looks like this (bash script loops in terminal) ...
WLAN0: ⬇️ 14 MiB/s ⬆️ 16 KiB/s
WLAN0: ⬇️ 60 B/s ⬆️ 0 B/s
WLAN0: ⬇️ 120 B/s ⬆️ 120 B/s
But I want it lined up, like this ...
WLAN0: ⬇️ 14 MiB/s ⬆️ 16 KiB/s
WLAN0: ⬇️ 60 B/s ⬆️ 0 B/s
WLAN0: ⬇️ 120 B/s ⬆️ 120 B/s
The essence of my code is the following simplified line ...
echo "yada: ⬇️ $string1 ⬆️ $string2"
The variables include a number and text (up to 10 chars max) each, depending on transfer speed.
So there should be at least 12 static fields between the two emoji.
But I have no clue how and I am hoping you can explain to me how to format some kind of variables width, with printf
I assume.
CodePudding user response:
Align left with printf
:
string1="14 MiB/s"; string2="16 KiB/s"
printf "yada: ⬇️ %-12s ⬆️ %-12s\n" "$string1" "$string2"
Output:
yada: ⬇️ 14 MiB/s ⬆️ 16 KiB/s
CodePudding user response:
Here you is how you can align columns:
#!/usr/bin/env bash
dnIcon=$'\342\254\207\357\270\217'
upIcon=$'\342\254\206\357\270\217'
nMiBs=14
sMiBs="$nMiBs MiB/s"
nKiBs=16
sKiBs="$nKiBs KiB/s"
printf 'WLAN0: %s %-10s %s %-10s\n' "$dnIcon" "$sMiBs" "$upIcon" "$sKiBs"
Sample output:
WLAN0: ⬇️ 14 MiB/s ⬆️ 16 KiB/s