V
V
VN2022-02-07 12:53:01
bash
VN, 2022-02-07 12:53:01

How to get boxed output with line wrapping?

There is a script that displays system info

VAR=$(echo -e "$MEM\n$HDD\n$CPU")
echo "$VAR2"

conclusion
Memory Usage: 277/1983MB (13.97%)
Disk Usage: 5/16GB (35%)
CPU Load: 0.00

I want to create a frame around the output, for this I use the function
banner() {
    msg="# $* #"
    edge=$(echo "$msg" | sed 's/./#/g')
    echo "$edge"
    echo "$msg"
    echo "$edge"
}

banner $VAR

Conclusion
#############################################################################
# Memory Usage: 277/1983MB (13.97%) Disk Usage: 5/16GB (35%) CPU Load: 0.00 #
#############################################################################


Unable to output line by line

Answer the question

In order to leave comments, you need to log in

2 answer(s)
X
xotkot, 2022-02-07
@kavabangaungava

I want to create a frame around the output, for this I use the function
msg="# $* #"

something is not at all
like an option, the size can be controlled through column
MEM="Memory Usage: 277/1983MB (13.97%)"
HDD="Disk Usage: 5/16GB (35%)"
CPU="CPU Load: 0.00"
VAR="$MEM\n$HDD\n$CPU"
banner() {
  v="[email protected]"
  x=$(echo -e "$v" |awk '{if(length>max)max=length}END{for(i=1;i<max+5;i++){printf "#"}}')
  echo "$x"
  echo -e "$v" |awk '{print "# "$0" #"}' |column -t -s: -o: |column -t -s'#' -o'#'
  echo "$x"
}
banner "$VAR"

conclusion
#####################################
# Memory Usage: 277/1983MB (13.97%) #
# Disk Usage  : 5/16GB (35%)        #
# CPU Load    : 0.00                #
#####################################

although if it is possible to initially control the type of receiving MEM, HDD, CPU, then it will be easier to write data through the same csv and format its output with appropriate programs, for example csview
MEM="Memory,277/1983MB,13.97%"
HDD="Disk,5/16GB,35%"
CPU="CPU,0.00,"
echo -e "$MEM\n$HDD\n$CPU" |csview -H --style Reinforced

conclusion:
┏────────┬────────────┬────────┓
│ Memory │ 277/1983MB │ 13.97% │
│ Disk   │ 5/16GB     │ 35%    │
│ CPU    │ 0.00       │        │
┗────────┴────────────┴────────┛

S
Saboteur, 2022-02-08
@saboteur_kiev

MEM="Memory Usage: 277/1983MB (13.97%)"
HDD="Disk Usage: 5/16GB (35%)"
CPU="CPU Load: 0.00"

banner() {
max=$(echo -e "$1"|awk '{if(length>max)max=length}END{printf max}')
echo -e "$1"|awk 'BEGIN{for(i=1;i<'$max'+4;i++){printf "#"};print "#"}{printf "# %'$max's #\n", $0}END{for(i=1;i<'$max'+4;i++){printf "#"};print "#"}'
}

banner "$MEM\n$HDD\n$CPU"

$ banner "$MEM\n$HDD\n$CPU"
#####################################
# Memory Usage: 277/1983MB (13.97%) #
#          Disk Usage: 5/16GB (35%) #
#                    CPU Load: 0.00 #
#####################################

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question