bash - 如何向 bash 添加内部命令?

标签 bash command

我指的不是别名或函数。我实际上想向 bash 源代码添加一个全新的内部命令。

这样,当我编译它时,它会将我的新命令内置到 shell 本身。

执行此操作涉及哪些步骤?

最佳答案

这实际上是对 a more specific question 的回答的一部分(如何将评估的 PS1 输出到一个变量)我认为这个过程应该有自己的问题和答案,一个更概括为修改 bash 源代码的过程。

以下是添加内部命令 evalps1 的步骤,它旨在为您提供与主提示字符串相同的输出。它基于版本 4.2 代码库。

第 1 步。

首先,更改 support/mkversion.sh,这样您就不会将它与“真正的”bash 混淆,这样 FSF 就可以否认所有知识保修目的:-) 只需更改一行(我添加了 -pax 位):

echo "#define DISTVERSION \"${float_dist}-pax\""

该脚本创建 version.h 并影响从 bash 获取版本信息的所有不同方式:--version$BASH_VERSION 变量等等。

第 2 步:

更改 builtins/Makefile.in 以将新的源文件添加到组合中。这需要执行多个步骤。

(a) 将 $(srcdir)/evalps1.def 添加到 DEFSRC 的末尾。此 def 文件包含有关内部命令以及实现代码的信息。

(b) 将 evalps1.o 添加到 OFILES 的末尾。这只会导致您的代码与 bash 链接。

(c) 添加需要的依赖:

evalps1.o: evalps1.def $(topdir)/bashtypes.h $(topdir)/config.h \
           $(topdir)/bashintl.h $(topdir)/shell.h common.h

第 3 步:

添加 builtins/evalps1.def 文件本身,这是运行 evalps1 命令时执行的代码:

This file is evalps1.def, from which is created evalps1.c.
It implements the builtin "evalps1" in Bash.

Copyright (C) 1987-2009 Free Software Foundation, Inc.

This file is part of GNU Bash, the Bourne Again SHell.

Bash is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Bash is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Bash.  If not, see <http://www.gnu.org/licenses/>.

$PRODUCES evalps1.c

$BUILTIN evalps1
$FUNCTION evalps1_builtin
$SHORT_DOC evalps1
Outputs the fully interpreted PS1 prompt.

Outputs the PS1 prompt, fully evaluated, for whatever nefarious purposes
you require.
$END

它的大部分是 GPL 许可证(因为我从 exit.def 修改了它),如上所示,最后有一个非常简单的函数来获取和解码 PS1,使用与 shell 本身相同的函数:

#include <config.h>
#include "../bashtypes.h"
#include <stdio.h>
#include "../bashintl.h"
#include "../shell.h"
#include "common.h"

int
evalps1_builtin (list)
     WORD_LIST *list;
{
  char *ps1 = get_string_value ("PS1");
  if (ps1 != 0)
  {
    ps1 = decode_prompt_string (ps1);
    if (ps1 != 0)
    {
      printf ("%s", ps1);
    }
  }
  return 0;
}

在这种情况下,代码本身非常简单,尽管它使用了我认为不必要的复杂缩进/大括号规则和 K&R 风格的预原型(prototype)函数声明——为了保持一致性,我将它们与当前代码保持一致。

代码只是调用函数获取PS1 环境变量,然后调用另一个解码提示字符串的函数。最后,它将解码后的字符串输出到标准输出。

当然,您可以根据您的特定需要用自己的代码替换该代码(同时将命令和文件名从 evalps1 更改为其他名称)。

第 4 步:

只需在顶级目录中构建:

./configure
make

出现在该顶级目录中的 bash 可执行文件可以重命名为 paxsh(例如),但我怀疑它是否会像它的祖先一样流行:-)

然后运行它,您可以看到它在运行,从真正的 bash shell 开始:

pax> mv bash paxsh

pax> ./paxsh --version
GNU bash, version 4.2-pax.0(1)-release (i686-pc-linux-gnu)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

pax> ./paxsh

pax> echo $BASH_VERSION
4.2-pax.0(1)-release

pax> echo "[$PS1]"
[pax> ]

pax> echo "[$(evalps1)]"
[pax> ]

pax> PS1="\h: "

paxbox01: echo "[$PS1]"
[\h: ]

paxbox01: echo "[$(evalps1)]"
[paxbox01: ]

关于bash - 如何向 bash 添加内部命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10063417/

相关文章:

linux - bash:在循环中创建许多描述符

Angular CLI 命令问题 : "Unknown option: ' --spec'"

command-line - 大于命令行中的值

Java - 使用选项执行程序,例如 ls -l

objective-c - NsTask执行终端命令的问题

linux - 如何计算程序文件中的行数(多个),过滤一些文件和一些行?

linux - 使用 HEREDOC 的 OPENSSL 自动应答密码

bash - 如何在 Ubuntu 的终端中自动运行应用程序?

linux - 如何阅读 Linux 手册页?

具有多个命令和可重入的 BASH 变量