1

I have bash script which creates different users and password. The passwords and users are stored in variables.

    cat << EOT
+---------------------------------------+
| Linux Logins                          |
+---------------+-----------------------+
| User          | Password              |
+---------------+-----------------------+
| $test      | $testpw               |
+---------------+-----------------------+

EOT

The Problem with this is that depending on the length of the stored variables the table gets messed up like this:

+---------------------------------------+
| Linux Logins                          |
+---------------+-----------------------+
| User          | Password              |
+---------------+-----------------------+
| michael         | helolopk8712t76               |
+---------------+-----------------------+

This is because the tabs are hardcoded. How can I solve this in a different way? I know there is awk and column to do so. I want it dynamic as in mysql console if that is possible.

patrick_
  • 156
  • 2
  • 12
  • 1
    I believe this is not possible with here docs. Probably the way to go is using `printf` as described in [Padding characters in printf](https://stackoverflow.com/q/4409399/1983854) – fedorqui Oct 04 '17 at 13:16

3 Answers3

3

I think you mean to say that your problem is due to the fact that your tabs are not hardcoded. But tabs are not really flexible enough to do what you want. You could do:

cat << EOT            
+---------------------------------------+              
| Linux Logins                          |              
+---------------+-----------------------+              
| User          | Password              |              
+---------------+-----------------------+              
| $(printf %-14s "$test")| $(printf %-22s "$testpw")|          
+---------------+-----------------------+              

EOT
Cyrus
  • 84,225
  • 14
  • 89
  • 153
William Pursell
  • 204,365
  • 48
  • 270
  • 300
  • @patrick_ Sorry, I had cut-n-pasted from a function and was using `$1` and `$2` instead of `$test` and `$testpw`. I should have left the original answer with the function definition! – William Pursell Oct 04 '17 at 16:23
  • Thanks this seems to work with passwords up to 22 chars. More than 22 chars is not realistic so this solution works perfect. Thank you very much. – patrick_ Oct 05 '17 at 07:16
  • could also be done with `printf -v` which is more efficient because avoids to call a subshell an to pipe result in expansion – Nahuel Fouilleul Oct 05 '17 at 07:31
1

variables can be padded with printf

printf -v test   %20s "$test"
printf -v testpw %20s "$testpw"

or for left align

printf -v test   %-20s "$test"
printf -v testpw %-20s "$testpw"

or to trunc after a specific length (19)

printf -v test   %-20.19s "$test"
printf -v testpw %-20.19s "$testpw"
Nahuel Fouilleul
  • 18,726
  • 2
  • 31
  • 36
0

Great answers. If it's of use to anyone, my table assigns colours and right-aligns the values. To use: create a file e.g. table.sh, set the permissions chmod u+x table.sh and run the file ./table.sh

#!/usr/bin/env bash

# Define colours
CYAN='\033[0;36m' 
GREEN='\033[0;32m' 
LIGHTRED='\033[1;31m' 
MAGENTA='\033[1;35m'
NC='\033[0m' # No Colour 
YELLOW='\033[1;33m' 

# Hard code variable values for this POC 
critical=0
high=2
medium=12
low=112
notScanned=10
total=136

# Print table with values right-aligned & colour-coded 

echo
printf "${NC}+--------------------------------+\n"
printf "${NC}|  ECR Vulnerabilities           |\n"
printf "${NC}+--------------------------------+\n"
printf "${NC}|  Critical      |${LIGHTRED} $(printf '%8.5s\n' ${critical})${NC}      |\n"
printf "${NC}+--------------------------------+\n"
printf "${NC}|  High          |${MAGENTA} $(printf '%8.5s\n' ${high})${NC}      |\n"
printf "${NC}+--------------------------------+\n"
printf "${NC}|  Medium        |${YELLOW} $(printf '%8.5s\n' ${medium})${NC}      |\n"
printf "${NC}+--------------------------------+\n"
printf "${NC}|  Low           |${CYAN} $(printf '%8.5s\n' ${low})${NC}      |\n"
printf "${NC}+--------------------------------+\n"
printf "${NC}|  Not Scanned   |${NC} $(printf '%8.5s\n' ${notScanned})${NC}      |\n"
printf "${NC}+--------------------------------+\n"
printf "${NC}|  Total         |${GREEN} $(printf '%8.5s\n' ${total})${NC}      |\n"
printf "${NC}+--------------------------------+\n"