首页 > 解决方案 > 来自“学习 Bash Shell,第 3 版”的示例练习问题。[纽汉,卡梅伦]

问题描述

来自第 2 章。“学习 Bash Shell”第 3 版,“fc 命令”:

“请记住,fc 在您编辑命令后实际上会运行这些命令。因此,最后命名的选择可能很危险。当您退出编辑器时,bash 将尝试执行您指定的范围内的所有命令。如果您已输入任何多行结构(就像我们将在第 5 章中介绍的那样),结果可能更加危险。尽管这些似乎是生成“即时 shell 程序”的有效方法,但更好的策略是直接输出fc -ln 对文件使用相同的参数;然后编辑该文件并在您对它们满意时执行命令:

$ fc -l cp > lastcommands

$ vi lastcommands

$ source lastcommands

在这种情况下,当您离开编辑器时,shell 将不会尝试执行该文件!”

我做了:

$ fc -l

这给出了输出:

1 回声测试1

2 回声测试2

3 回声测试3

4 回声测试4

所以我尝试了:

$ fc -l cp > lastcommands

但得到:

-bash: fc: history specification out of range

想知道为什么我得到超出范围的错误。文件“lastcommands”已创建,但为空。

标签: bash

解决方案


我试过了:

$ fc -l cp > lastcommands

但得到:

-bash: fc: history specification out of range

审查help fc(截断)

$ help fc
fc: fc [-e ename] [-lnr] [first] [last]
    Display or execute commands from the history list.

    … FIRST can be a
    string, which means the most recent command beginning with that
    string.
    …
     -l        list lines instead of editing

FIRST你作为 string传递cp。您的历史记录中似乎没有以 . 开头的命令cp

尝试使用绝对不会成为 bash 历史记录中的命令的唯一字符串:

fc -l NOTINYOURHISTORY

你会得到同样的错误。


推荐阅读