首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Linux终端中只获取有限的命令输出?

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

Stack Overflow用户
提问于 2021-09-06 12:55:05
回答 1查看 35关注 0票数 0

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

  1. head选项适用于nping命令.

代码语言:javascript
复制
    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命令,如果找不到这些参数和输出错误,则需要一些预定义的命令行参数和输出错误。如何克服这一问题?

代码语言:javascript
复制
    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$
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-09-06 13:04:55

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

这将起作用:

代码语言:javascript
复制
iperf3 2>&1 | head -5

2>&1stderr重定向到stdout

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69074911

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档