ECHO扩大PS1 [英] Echo expanded PS1

查看:140
本文介绍了ECHO扩大PS1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个运行在几个目录相同的命令( fgit )的shell脚本。对于每个目录,我想它显示当前的提示+将在那里运行命令。我如何获得对应的德codeD字符串(扩) PS1 ?例如,我的默认PS1是

I have a shell script that runs the same command in several directories (fgit). For each directory, I would like it to show the current prompt + the command which will be run there. How do I get the string that corresponds to the decoded (expanded)PS1? For example, my default PS1 is

${debian_chroot:+($debian_chroot)}\[\e[1;32m\]\u\[\e[0m\]@\[\e[1;32m\]\h\[\e[0m\]:\[\e[1;34m\]\w\[\e[0m\]$(__git_ps1 ' (%s)')$

和我想呼应结果提示用户名@主机名:/路径$ ,preferably(但不一定)与漂亮的色彩。粗略地看一下说明书猛砸没有透露任何确切的答案,而回声-e $ PS1 只判断的颜色。

and I'd like to echo the resulting prompt username@hostname:/path$, preferably (but not necessarily) with the nice colors. A cursory look at the Bash manual didn't reveal any definite answer, and echo -e $PS1 only evaluates the colors.

推荐答案

开源软件的一大优势是源,没错,是开放的: - )

One great advantage of open source software is that the source is, well, open :-)

猛砸本身不提供此功能,但有一个你可以用它来提供一个集各种技巧(如替换 \\ U $ USER 等)。然而,这需要大量的功能重复,并确保code是保持同步的任何庆典确实在未来。

Bash itself does not provide this functionality but there are various tricks you can use to provide a subset (such as substituting \u with $USER and so on). However, this requires a lot of duplication of functionality and ensuring that the code is kept in sync with whatever bash does in future.

如果你想获得的所有的提示变量的力量(你不介意让你的手脏有位编码(如果你很介意,你怎么在这里? )),它很容易添加到shell本身。

If you want to get all the power of prompt variables (and you don't mind getting your hands dirty with a bit of coding (and, if you do mind, why are you here?)), it's easy enough to add to the shell itself.

如果您下载庆典的code(我期待在版本4.2),有一个 y.tab.c的文件,其中包含德code_prompt_string()功能:

If you download the code for bash (I'm looking at version 4.2), there's a y.tab.c file which contains the decode_prompt_string() function:

char *decode_prompt_string (string) char *string; { ... }

这是评估 PSX 变量提示的功能。为了让此功能将提供给shell本身的用户(而不仅仅是使用的的外壳),可以按照以下步骤添加一个内部命令 evalps1

This is the function that evaluates the PSx variables for prompting. In order to allow this functionality to be provided to users of the shell itself (rather than just used by the shell), you can follow these steps to add an internal command evalps1.

首先,将支持/ mkversion.sh ,这样你就无法与真正的庆典等的FSF可以拒绝所有的知识进行保修目的:-)简单地改变一行(我加了 -pax 位):

First, change support/mkversion.sh so that you won't confuse it with a "real" bash, and so that the FSF can deny all knowledge for warranty purposes :-) Simply change one line (I added the -pax bit):

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

二,改变`内建/ Makefile.in添加一个新的源文件。这需要许多步骤

Second, change `builtins/Makefile.in to add a new source file. This entails a number of steps.

(一) $(SRCDIR)/evalps1.def 加入 DEFSRC 的结束。

(二) evalps1.o 添加到年底 OFILES

(三)添加所需的依赖关系:

(c) Add the required dependencies:

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

三,添加建宏/ evalps1.def 文件本身,这是当您运行被执行code中的 evalps1 命令:

Third, add the builtins/evalps1.def file itself, this is the code that gets executed when you run the evalps1 command:

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 )用很简单的功能在年底获得并去code PS1

The bulk of that is the GPL licence (since I modified it from exit.def) with a very simple function at the end to get and decode PS1.

最后,刚刚建立的顶级目录的东西:

Lastly, just build the thing in the top level directory:

./configure
make

庆典的可执行文件,似乎可以被重命名为 paxsh ,但我怀疑这会不会成为为prevalent作为其祖先: - )

The bash executable that appears can be renamed to paxsh, though I doubt it will ever become as prevalent as its ancestor :-)

和运行它,你可以看到它在行动:

And running it, you can see it in action:

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: ]

当你把 PSX 变量之一进入提示,呼应 $ PS1 只是给你的变量,而 evalps1 命令计算它,并输出结果。

When you put one of the PSx variables into the prompt, echoing $PS1 simply gives you the variable, while the evalps1 command evaluates it and outputs the result.

现在,理所当然,使得code变为庆典来增加内部命令可能会被一些人认为是矫枉过正,但如果你想要的完美评价 PS1 ,它肯定是一个选项。

Now, granted, making code changes to bash to add an internal command may be considered by some to be overkill but, if you want an perfect evaluation of PS1, it's certainly an option.

这篇关于ECHO扩大PS1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆