如何向bash添加内部命令?
我不是指别名或功能.我实际上想要为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风格的预原型函数声明 – 我保持它们与当前代码保持一致以保持一致性. 代码只是调用函数来获取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: ] (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |