I am writing a bash shell script to display if a process is running or not.
So far, I got this:
printf "%-50s %s\n" $PROC_NAME [UP]
The code gives me this output:
JBoss [DOWN]
GlassFish [UP]
verylongprocessname [UP]
I want to pad the gap between the two fields with a '-' or '*' to make it more readable. How do I do that without disturbing the alignment of the fields?
The output I want is:
JBoss ------------------------------------------- [DOWN]
GlassFish --------------------------------------- [UP]
verylongprocessname ----------------------------- [UP]
This one is even simpler and execs no external commands.
Simple Console Span/Fill/Pad/Padding with automatic scaling/resizing Method and Example.
Example:
create-console-spanner "loading graphics module" "[success]"
Now here is a full-featured-color-character-terminal-suite that does everything in regards to printing a color and style formatted string with a spanner.
To print a color, that's simple enough:
paint-format "&red;This is %s\n" red
And you might want to get bold later on:paint-format "&bold;%s!\n" WOW
The
-l
option to thepaint-format
function measures the text so you can do console font metrics operations.The
-v
option to thepaint-format
function works the same asprintf
but cannot be supplied with-l
Now for the spanning!
paint-span "hello " . " &blue;world"
[note: we didn't add newline terminal sequence, but the text fills the terminal, so the next line only appears to be a newline terminal sequence]and the output of that is:
hello ............................. world
Explanation:
printf '\055%.0s' {1..40}
- Create 40 dashes(dash is interpreted as option so use escaped ascii code instead)
"$PROC_NAME ..."
- Concatenate $PROC_NAME and dashes| head -c 40
- Trim string to first 40 charsPure Bash. Use the length of the value of 'PROC_NAME' as offset for the fixed string 'line':
This gives
Pure Bash, no external utilities
This demonstration does full justification, but you can just omit subtracting the length of the second string if you want ragged-right lines.
Unfortunately, in that technique, the length of the pad string has to be hardcoded to be longer than the longest one you think you'll need, but the padlength can be a variable as shown. However, you can replace the first line with these three to be able to use a variable for the length of the pad:
So the pad (
padlimit
andpadlength
) could be based on terminal width ($COLUMNS
) or computed from the length of the longest data string.Output:
Without subtracting the length of the second string:
The first line could instead be the equivalent (similar to
sprintf
):or similarly for the more dynamic technique:
You can do the printing all on one line if you prefer:
Trivial (but working) solution: