bash:如何评价PS1、PS2……? [重复]

问题描述 投票:0回答:1

可能重复:
Echo 扩展 PS1

有没有办法在 bash 脚本中“评估”

PS1
PS2
等?

尽管我可以使用替代方法来获取当前

PS1
的所有元素,但我真的希望能够重用其定义,而不是使用这些替代方法。

例如,

=====================================
 PS1 element -->     Alternate means
=====================================
 \u          -->     $USER
 \h          -->     $HOSTNAME
 \w          -->     $PWD
 ...
=====================================

我可以很好地在我的脚本中使用“替代方法”列,但我不想这样做。例如,在我的

PS1
中,我通过终端转义序列使用粗体蓝色,我希望能够通过评估
PS1
来简单地重用它。

bash evaluate ps1
1个回答
15
投票

开源软件的一大优势是源代码是开放的:-)

如果您下载

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
,这当然是一个选择。

© www.soinside.com 2019 - 2024. All rights reserved.