0

I am trying to print a output with specific format, where output is in a enclosed box. I am using "|" and "-" for creating it. The output I print in between the "|" are variables with Different number of characters. This is messing my format.

I have tried the below code

nodename=$(hostname)
cpu=$(cat /proc/cpuinfo | awk '/^processor/{print $3}' | wc -l)
echo " ------------------------------------------------------------------"  
echo "|                                                                  |" 
echo "|          This is Bash Scripting                                  |" 
echo "|                                                                  |" 
echo "|  1 - This Machine Name is $nodename                              |" 
echo "|  2 - This Machine has $cpu CPU's                                 |"
echo "|                                                                  |" 
echo " -------------------------------------------------------------------" 

actual Result

" ------------------------------------------------------------------"  
"|                                                                  |" 
"|          This is Bash Scripting                                  |" 
"|                                                                  |" 
"|  1 - This Machine Name is test                              |" 
"|  2 - This Machine has 8 CPU's                                 |"
"|                                                                  |" 
" -------------------------------------------------------------------" 

Expected result

" ------------------------------------------------------------------"  
"|                                                                  |" 
"|          This is Bash Scripting                                  |" 
"|                                                                  |" 
"|  1 - This Machine Name is test                                   |" 
"|  2 - This Machine has 8 CPU's                                    |"
"|                                                                  |" 
" -------------------------------------------------------------------" 
Anger Density
  • 332
  • 1
  • 3
  • 17
Chand
  • 300
  • 3
  • 13
  • 2
    [Bash add trailing spaces to justify string](https://unix.stackexchange.com/q/354092), [How do I print some text in bash and pad it with spaces to a certain width?](https://stackoverflow.com/q/6345429/608639), [Padding characters in printf](https://stackoverflow.com/q/4409399/608639), [Formatting output with printf: truncating or padding](https://stackoverflow.com/q/36121180/608639), etc. – jww Aug 23 '19 at 13:21
  • 1
    `grep -c processor /proc/cpuinfo`. One process instead of three. You don't need `awk` to extract the processor number when all you are doing is counting how many there are. – chepner Aug 23 '19 at 13:28

1 Answers1

4

Use printf instead of echo:

printf "|  1 - This Machine Name is %-39s|\n" "$nodename"

You can specify the right space padding with %-39s

chepner
  • 497,756
  • 71
  • 530
  • 681
oliv
  • 12,690
  • 25
  • 45