开源软件的一大优势是源代码是开放的:-)
如果您下载的代码为bash
(我正在看4.2版本),有一个y.tab.c
文件包含decode_prompt_string()
功能:
char *decode_prompt_string (string) char *string; { ... }
您可以尝试提取它(以及任何所需的支持例程)并构建一个为您完成工作的可执行文件。尽管从粗略的尝试来看,这些支持例程似乎很多,所以这可能是一项艰巨的任务。
除此之外,你可能还可以“欺骗”bash
为您扩展它,例如:
expPS1=$(echo xyzzyplughtwisty | bash -i 2>&1
| grep xyzzyplughtwisty
| head -1
| sed 's/xyzzyplughtwisty//g')
现在,为了便于阅读,我将其放在多行中,但它是在一行上完成的。
它的作用是运行一个交互式实例bash
,传递(希望是)无效命令。
因为它是交互式的,所以它会打印提示,因此我抓住带有命令字符串的第一行并删除该命令字符串。剩下的应该就是提示了。
在我的系统上,这是我得到的:
pax> expPS1=$(echo xyzzyplughtwisty | bash -i 2>&1 | grep xyzzyplughtwisty | head -1 | sed 's/xyzzyplughtwisty//g')
pax> echo "[$expPS1]"
[pax> ]
pax>
然而,这对于多行提示有问题,并且实际上会给您常规提示而不是当前的 shell 提示。
如果你想做的话适当地,它可能涉及添加一点bash
本身。以下是添加内部命令的步骤evalps1
.
首先,改变support/mkversion.sh
这样你就不会将它与“真实”混淆bash
,这样 FSF 就可以出于保修目的否认所有知识:-) 只需更改一行(我添加了-pax
bit):
echo "#define DISTVERSION \"${float_dist}-pax\""
其次,更改“builtins/Makefile.in”以添加新的源文件。这需要执行多个步骤。
(a) Add $(srcdir)/evalps1.def
到最后DEFSRC
.
(b) Add evalps1.o
到最后OFILES
.
(c) 添加所需的依赖项:
evalps1.o: evalps1.def $(topdir)/bashtypes.h $(topdir)/config.h \
$(topdir)/bashintl.h $(topdir)/shell.h common.h
第三,添加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
#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;
}
其中大部分是 GPL 许可证(因为我从exit.def
)最后有一个非常简单的函数来获取和解码PS1
.
最后,只需在顶级目录中构建该内容即可:
./configure
make
The bash
出现的可以重命名为paxsh
,尽管我怀疑它是否会像它的祖先一样流行:-)
运行它,您可以看到它的实际效果:
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
添加内部命令可能被某些人认为是矫枉过正,但是,如果您想要准确评估PS1
,这当然是一个选择。