BASH - 将多个命令输出到列中

时间:2017-11-14 11:52:14

标签: linux bash shell output multiple-columns

我正在尝试编写一个运行各种命令的脚本,并将每个命令的结果输出到一列,但是我无法将输出显示在列中。

#!/bin/bash

# Get GBP Neighbour NAMES
NEIGHBOR=$(vtysh -c 'show ip bgp neighbors' | grep Incoming | awk '{print $7}')

# Get IPs of BGP neighbours
IP=$(vtysh -c 'show ip bgp summary' | awk '{print $1}' | head -n -2 | tail -n +6)

# Get Up/Down time
TIME=$(vtysh -c 'show ip bgp summary' | awk '{print $9}' | head -n -2 | tail -n +6)

# Get State/PfxRcd
STATE=$(vtysh -c 'show ip bgp summary' | awk '{print $10}' | head -n -2 | tail -n +6)

如何在以下列中分隔输出:

NEIGHBOR | IP | TIME | STATE

提前致谢!

修改

感谢@RomanPerekhrest的帮助!

工作脚本:

#!/bin/bash

# Get GBP Neighbour NAMES
NEIGHBOR=$(vtysh -c 'show ip bgp neighbors' | grep "Incoming" | awk '{print $7}')
# Get IPs of BGP neighbours
IP=$(vtysh -c 'show ip bgp summary' | awk '{print $1}' | head -n -2 | tail -n +6)
# Get Up/Down time
TIME=$(vtysh -c 'show ip bgp summary' | awk '{print $9}' | head -n -2 | tail -n +6)
# Get State/PfxRcd
STATE=$(vtysh -c 'show ip bgp summary' | awk '{print $10}' | head -n -2 | tail -n +6)
# Generate Titles for table
TITLE=$(paste -d'|' <(echo "NEIGHBOR") <(echo "IP") <(echo "UPTIME") <(echo "STATE"))
# Generate table
LIST=$(paste -d'|' <(echo "$NEIGHBOR") <(echo "$IP") <(echo "$TIME") <(echo "$STATE"))
# Merge titles and table
OUTPUT=$(echo -e "$TITLE" && echo -e "$LIST")
# Display output
echo -e ""
echo -e "------------------------------------------------"
echo -e "NEIGHBORS AND STATUS"
echo -e "------------------------------------------------"
echo -e ""
echo -e " $OUTPUT" | column -s'|' -t # Echo output and colmnize it
echo -e ""

这会生成表格并平等标注标题和内容。在最终的回显中设置| column -s'|' -t非常重要,因为如果没有,则不能正确对齐标题​​。

1 个答案:

答案 0 :(得分:5)

使用 paste 命令:

paste -d'|' <(echo "$NEIGHBOR ") <(echo "$IP") <(echo "$TIME") <(echo "$STATE")