我的作品中有以下脚本:
#!/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
该脚本运行良好,但需要一些格式。我现在得到的输出是:
----------------------------------------------------------------------------------------------
| 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 | | | |
我希望将其视为:
----------------------------------------------------------------------------------------------
| 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 | | | |
忽略|
对齐。我可以照顾那个。所以基本上,我希望job_name
在单列下有变量的多值/多行输出。
答案1
一种方法如下,我们使用nroff
和tbl
。我们首先tbl
根据数据生成预处理器的代码。
由于我无权访问您的实用程序,因此我根据以下内容对您的程序行为进行了建模,然后您必须将其合并到您的案例中:
#!/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 '.'
结果:
+------------------+---------+--------------+-----------+----------+------+
|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 | | | |
+------------------+---------+--------------+-----------+----------+------+