如何在Linux终端中只获取有限的命令输出



我试图只获取命令的前5行。通常的方法是使用head选项,但它并不适用于所有命令。下面是一些例子。

  1. head选项适用于nping命令
rajkumar:~/automation$ nping | head -5
Nping 0.7.60 ( https://nmap.org/nping )
Usage: nping [Probe mode] [Options] {target specification}

TARGET SPECIFICATION:
Targets may be specified as hostnames, IP addresses, networks, etc.
rajkumar:~/automation$ nping | wc -l
119
rajkumar:~/automation$
  1. 但是,head选项不适用于iperf3命令,该命令需要一些预定义的命令行参数,如果找不到,则会输出错误。如何克服这一点
rajkumar:~/automation$ iperf3 | head -5
Usage: iperf [-s|-c host] [options]
iperf [-h|--help] [-v|--version]

Server or Client:
-p, --port      #         server port to listen on/connect to

>>>>>>>>>>>>  TRIMMED OUTPUT  <<<<<<<<<<<<<<<<<

[KMG] indicates options that support a K/M/G suffix for kilo-, mega-, or giga-

iperf3 homepage at: http://software.es.net/iperf/
Report bugs to:     https://github.com/esnet/iperf
iperf3: parameter error - must either be a client (-c) or server (-s)  <<<< Error given by iperf3 command
rajkumar:~/automation$

您混淆了stderr(标准错误)和stdout(标准输出)。

这将起作用:

iperf3 2>&1 | head -5

CCD_ 8将CCD_ 9重定向到CCD_。

最新更新