0

I have the following script in works:

#!/bin/bash
IFS=$(echo -en "\n\b")

echo -e "----------------------------------------------------------------------------------------------"
echo -e "| Job  Name                      | Enabled | Client Names    | Retention | Schedule    | Type |"
echo -e "----------------------------------------------------------------------------------------------"

vcenter_name=$(cat /usr/local/vdr/etc/vcenterinfo.cfg | grep vcenter-hostname | cut -d '=' -f 2)
# To extract job names:
job_name=$(mccli group show --recursive=true | grep -i "/$vcenter_name/VirtualMachines" | awk -F/. '{print $(NF-2)}')

for i in $job_name
do
enabled=$(mccli group show --name=/$vcenter_name/VirtualMachines/$i | grep Enabled | awk '{print $NF}')
client_name=$(mccli group show-members --name=/vcenter-prod.happycow.local/VirtualMachines/$i | awk '{print $3}' | tail -n +4 | awk -F/ '{print $NF}')
printf "| %-27s | %7s | %7s | %10s | %7s | %12s |\n" "$i" "$enabled" "$client_name"
done

The script runs great but needs some formatting. The output I get now is:

----------------------------------------------------------------------------------------------
| Job  Name                      | Enabled | Client Names    | Retention | Schedule    | Type |
----------------------------------------------------------------------------------------------
| Backup With Space              |    true |   Space |            |         |              |
| Disk-Level                     |    true |         |            |         |              |
| Linux-VM                       |    true |         |            |         |              |
| Partial                        |    true |         |            |         |              |
| Prod-Backup                    |   false | VM-B
VM-D |            |         |              |
| Same-Host                      |    true |         |            |         |              |
| Temp                           |    true | esxi02
ESXi-6.5
ESXi6GA |            |         |              |

I would like to see it as:

----------------------------------------------------------------------------------------------
| Job  Name                      | Enabled | Client Names    | Retention | Schedule    | Type |
----------------------------------------------------------------------------------------------
| Backup With Space              |    true |   Space |            |         |              |
| Disk-Level                     |    true |         |            |         |              |
| Linux-VM                       |    true |         |            |         |              |
| Partial                        |    true |         |            |         |              |
| Prod-Backup                    |   false | VM-B    |            |         |              |
|                                            VM-D    |            |         |              |
| Same-Host                      |    true |         |            |         |              |
| Temp                           |    true | esxi02
|                                            ESXi6.5
|                                            ESXi6GA |            |         |              |

Ignore the | alignment. I can take care of that one. So basically, I would like to have the multi value / multi line output of job_name variable under a single column.

  • 1. a report script like this would be better written entirely in awk or perl (or anything else but sh or bash) - trying to do it in bash will result in an unreadable mess. BTW, perl has great built-in report writing capabilities (see man perlform). 2. we need to sample input, not just the desired output. 3. some basic bash/shell tips: use IFS=$'\n\b', not IFS=$(echo "\n\b"); there's no need to pipe cat into grep or grep into awk - both are capable of reading files without cat's help. e.g. vcenter_name=$(awk -F= '{print $2}' /usr/local/vdr/etc/vcenterinfo.cfg) – cas Aug 2 '17 at 3:56
  • Thanks. Still learning things in bash, thanks for the input on better scripting. :) – suhas savkoor Aug 2 '17 at 18:21
1

One approach is the following where we make use of nroff and tbl. We first generate the code for the tbl preprocessor based on the data.

Since I don't have access to your utilities so I have modeled your program behavior on the following, which you'd have to then incorporate in your case:

#!/bin/bash

 NL=$'\n'
TAB=$'\t'

header=( "Job  Name" "Enabled" "Client Names" "Retention" "Schedule" "Type" )

job_name=( "Backup With Space" "Disk-Level" "Linux-VM" "Partial" "Prod-Backup"   "Same-Host" "Temp"                             )
 enabled=(  true                true         true       true      false           true        true                              )
  client=(  Space               ""           ""         ""        "VM_B${NL}VM_D" ""          "esxio2${NL}ESXi-6.5${NL}ESXi6GA" )   

{
echo .TS
echo allbox,center,tab\($'\t'\)\;
echo l r l l l l l.
echo "$(IFS=$'\t'; echo "${header[*]}")"
for (( i=0; i<${#enabled[@]}; i++ ))
do
var=${client[$i]//$NL/$NL$TAB$TAB}
printf '%s\t%s\t%s\t\t\t\n' "${job_name[$i]}" "${enabled[$i]}" "${var}"
done
echo .TE
} | tbl - | nroff -Tascii -ms | grep '.'

Results:

+------------------+---------+--------------+-----------+----------+------+
|Job  Name         | Enabled | Client Names | Retention | Schedule | Type |
+------------------+---------+--------------+-----------+----------+------+
|Backup With Space |    true | Space        |           |          |      |
+------------------+---------+--------------+-----------+----------+------+
|Disk-Level        |    true |              |           |          |      |
+------------------+---------+--------------+-----------+----------+------+
|Linux-VM          |    true |              |           |          |      |
+------------------+---------+--------------+-----------+----------+------+
|Partial           |    true |              |           |          |      |
+------------------+---------+--------------+-----------+----------+------+
|Prod-Backup       |   false | VM_B         |           |          |      |
+------------------+---------+--------------+-----------+----------+------+
|                  |         | VM_D         |           |          |      |
+------------------+---------+--------------+-----------+----------+------+
|Same-Host         |    true |              |           |          |      |
+------------------+---------+--------------+-----------+----------+------+
|Temp              |    true | esxio2       |           |          |      |
+------------------+---------+--------------+-----------+----------+------+
|                  |         | ESXi-6.5     |           |          |      |
+------------------+---------+--------------+-----------+----------+------+
|                  |         | ESXi6GA      |           |          |      |
+------------------+---------+--------------+-----------+----------+------+
  • Thanks for all the suggestions. I was able to get this done by adding a loop for the client list and add "" to ignore additional values. Thanks. – suhas savkoor Aug 2 '17 at 18:20
  • +1. nice, i was thinking of doing something similar with markdown but got distracted by real life. – cas Aug 2 '17 at 18:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.