如何精确返回bash脚本的输出值具有命令

时间:2019-07-03 13:27:01

标签: python bash shell ubuntu ubuntu-18.04

我有以下命令:

$ snmpnetstat -v2c -c public -Cs -Cp udp 10.10.0.32 

具有终端输出:

udp:
           198 total datagrams received
            65 datagrams to invalid port
             0 datagrams dropped due to errors
           265 output datagram requests

我想编写一个返回以下内容的bash脚本:

bash脚本的期望输出:

Received Datagrams: 198 
Invalid port:65 
Dropped datagrams: 0 
Datagram requests: 256

我从开始:

#!/bin/bash
    rs="$(snmpnetstat -v2c -c public -Cs -Cp udp 10.10.0.32)"
ReceivedDatagrams=$(echo $rs | cut -d"/" -f1)
InvalidPort=$(echo $rs | cut -d"/" -f2)
DroppedDatagrams=$(echo $rs | cut -d"/" -f3)
DatagramRequests=$(echo $rs | cut -d"/" -f4)

echo "Received Datagrams:$ReceivedDatagrams Invalid port:$InvalidPort Dropped datagrams:$DroppedDatagrams Datagram requests:$DatagramRequests"

输出为:

    zsz@bme-ib112-05:~/bash_scripts$ ./script.sh         
Received Datagrams:udp: 242 total datagrams received 37 datagrams to invalid port 0 datagrams dropped due to errors 638 output datagram requests Invalid port:udp: 242 total datagrams received 37 datagrams to invalid port 0 datagrams dropped due to errors 638 output datagram requests Dropped datagrams:udp: 242 total datagrams received 37 datagrams to invalid port 0 datagrams dropped due to errors 638 output datagram requests Datagram requests:udp: 242 total datagrams received 37 datagrams to invalid port 0 datagrams dropped due to errors 638 output datagram requests

输出值不断重复,而不是我想要的。

3 个答案:

答案 0 :(得分:4)

一个简单的awk解决方案,正如Shellter指出的那样,消除了所有这些额外过程的开销:

$ cat udp.out
udp:
           198 total datagrams received
            65 datagrams to invalid port
             0 datagrams dropped due to errors
           265 output datagram requests

$ awk '
/received/ {printf "Received Datagrams: %s\n", $1}
/invalid/  {printf "Invalid port: %s\n"      , $1}
/dropped/  {printf "Dropped datagrams: %s\n" , $1}
/requests/ {printf "Datagram requests: %s\n" , $1}
' udp.out

Received Datagrams: 198
Invalid port: 65
Dropped datagrams: 0
Datagram requests: 265

答案 1 :(得分:3)

如果您确实不希望将值分配给变量,并且严格希望重新格式化输出,则可以使用awk编写更干净的代码,如下所示:

awk '
/total datagrams/                 { print "Received Datagrams:", $1 }
/datagrams to invalid port/       { print "Invalid port:", $1 }
/datagrams dropped due to errors/ { print "Dropped datagrams:", $1 }
/output datagram requests/        { print "Datagram requests:", $1 } 
' 

从某种意义上说,awk正在为您进行所有解析,并将(tr -s)挤出所有空白。我相信它使代码更易于阅读。

答案 2 :(得分:2)

我无法重新创建snmpnetstat命令的确切输出,因此我只是将您的终端输出复制/粘贴到文件tmp中。

以下脚本使用您的逻辑来完成您要求的工作

我建议您接受Shellter和Ed Morton的建议,并仔细研究报价变量如何工作以禁止在空格/换行符中进行扩展,并使用Awk作为更健壮且易于扩展的解决方案。

#!/bin/bash

rs="$(cat tmp)"

# You have to quote "$rs" so newlines don't break
ReceivedDatagrams="$(echo "$rs" | cut -d$'\n' -f2 | tr -s ' '| cut -d' ' -f2)"
InvalidPort="$(echo "$rs" | cut -d$'\n' -f3 | tr -s ' '| cut -d' ' -f2)"
DroppedDatagrams="$(echo "$rs" | cut -d$'\n' -f4 | tr -s ' '| cut -d' ' -f2)"
DatagramRequests="$(echo "$rs" | cut -d$'\n' -f5 | tr -s ' '| cut -d' ' -f2)"

echo "Received Datagrams:$ReceivedDatagrams"
echo "Invalid port:$InvalidPort"
echo "Dropped datagrams:$DroppedDatagrams"
echo "Datagram requests:$DatagramRequests"