1

I've currently got a bash script which checks for some dependencies and then executes some code which generates a pdf.

At the moment when the dependencies are checked my output looks like this:

CHECKING DEPENDENCIES:
4.3.33(1)-release.................................OK
wkhtmltopdf 0.12.1 (with patched qt)..............OK
jq-1.4............................................OK

The number of periods between the dependency name and the OK are hardcoded. As a result anyone running the script with different versions will have the OK incorrectly justified as below.

CHECKING DEPENDENCIES:
4.3.33(1)-release.................................OK
wkhtmltopdf 0.11.0..............OK
jq-1.4............................................OK

I'm thinking that it may be easiest to create a separate function which is passed the name of the dependency and if it's ok or not. The function then prints out the name, the number of dots and the OK or NOK.

What I'm stumbling on is how to perform that dot count, and if there's an easier way to do it.

TheMightyLlama
  • 1,243
  • 1
  • 19
  • 51

4 Answers4

2

You can use this pure BASH utility for formatted output:

# max width of your terminal
maxlen=$(tput cols)

# initialize a string with $maxlen dots
printf -v fill "%${maxlen}s" ""
fill="${fill// /.}"

# function for printing it 
printfmt() { printf '%s%0.'$(($maxlen - ${#1} - ${#2} ))'s%s\n' "$1" "$fill" "$2"; }

Testing:

printfmt "wkhtmltopdf 0.11.0 foo bar baz" "OK"
wkhtmltopdf 0.11.0 foo bar baz................................................OK
printfmt "wkhtmltopdf 0.11.0 foo bar" "NOK"
wkhtmltopdf 0.11.0 foo bar...................................................NOK
printfmt "wkhtmltopdf 0.11.0" "OK"
wkhtmltopdf 0.11.0............................................................OK
anubhava
  • 761,203
  • 64
  • 569
  • 643
1

tput cols (shows characters per line) is what you are looking for. Using that you can easily find out how many dots you need to add by substracting the words.

  • Although all the other answers seemed to provide working solutions, I chose to implement a variation on this: https://gist.github.com/TheMightyLlama/261b296d66b3a042b743 – TheMightyLlama Mar 13 '15 at 16:32
  • @TheMightyLlama: You didn't really need that much long code. Instead of `maxlen=80` just replace `maxlen=$(tput cols)` in my answer below and get the same output. – anubhava Mar 13 '15 at 16:38
1

printf is available in Bash

To be more flexible in the output of numbers and strings, the printf command allows format modifiers. These are specified between the introductory % and the character that specifies the format:

printf "%50s\n" "This field is 50 characters wide..."
Amir Afghani
  • 37,814
  • 16
  • 84
  • 124
  • I see that `printf "BashVersion%50s\n" "OK"` gives me the correct output minus the periods. Can I replace `s\n` for periods? – TheMightyLlama Mar 13 '15 at 15:51
1

This way is sort of cheating, because it overwrites lines, so it won't look nice if you capture the output in a file. But it works fine for display purposes:

dotfmt=$(printf .%.0s {1..50})%s$(tput cr)%s\\n
#for each dependency:
# ...
  printf dotfmt "$ok" "$name" 

A cleaner solution is to compute the number of dots.

printf %s "$name"
if ((${#name} < 50)); then
  printf .%.0s $(seq $((50-${#name})))
fi
echo "$ok"
rici
  • 234,347
  • 28
  • 237
  • 341