可能重复:
有没有任何方法从bash脚本中“评估”PS1、PS2等?
尽管我可以使用替代方法获取当前PS1的所有元素,但我确实希望能够重用它的定义,而不是使用这些替代方法。
例如,
=====================================
PS1 element --> Alternate means
=====================================
\u --> $USER
\h --> $HOSTNAME
\w --> $PWD
...
=====================================我可以很好地使用脚本中的“备用方法”列,但我不想使用。例如,在我的PS1中,我通过终端转义序列使用粗体蓝色,我希望能够通过计算PS1来简单地重用。
发布于 2012-04-08 05:17:30
开源软件的一个巨大优点是,源代码是开放的:-)
如果下载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位):
echo "#define DISTVERSION \"${float_dist}-pax\""其次,更改“`builtins/Makefile.in”以添加一个新的源文件。这需要采取若干步骤。
(a)将$(srcdir)/evalps1.def添加到DEFSRC末尾。
(b)将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出现的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进行准确的评估,这当然是一种选择。
https://stackoverflow.com/questions/10060500
复制相似问题