【问题标题】:i'm trying to write a bash script and i don't get the result displayed in the terminal我正在尝试编写一个 bash 脚本,但我没有在终端中显示结果
【发布时间】:2015-01-20 13:56:48
【问题描述】:

我正在尝试显示文件中的第 3 到第 7 行,但我在使用此命令的终端中没有显示任何内容:

head -n 7 /etc/passwd | tail -n +3

我希望在终端中看到结果。

【问题讨论】:

  • 当我尝试这个命令时我什么也得不到!!

标签: linux bash shell scripting terminal


【解决方案1】:

你可以试试这个方法

head -n 7 /etc/passwd | tail -n 5

例如:

seq 20 | head -n 7 | tail -n 5

输出:

3
4
5
6
7

说明:

head -n 7       -- print the  first 7 lines ( so 1..7 printed)
tail -n 5       -- print last 5 lines ( so skipped first two lines 3..7 printed )

【讨论】:

    【解决方案2】:
    head -n 7 /etc/passwd | tail -n +3
    

    似乎正是你想让它为我做的事情。您是否验证了 /etc/passwd 的内容?如果您没有读取文件的权限或文件为空,您将不会得到任何输出。

    我会检查脚本中的其他地方。将脚本的第一行更改为:

    #!/bin/bash -v
    

    让它回显命令,这样您就可以确定您认为正在执行的实际是什么。

    【讨论】:

      【解决方案3】:

      你得到什么比?显示文件输出,但选择另一个文件;)

      有时只更改命令会有所帮助,尝试两次 HEAD 或 TAIL 以获得相同的结果。

      【讨论】:

        猜你喜欢
        • 2021-02-06
        • 1970-01-01
        • 2016-01-30
        • 2021-10-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-12
        • 1970-01-01
        相关资源
        最近更新 更多