庆典:如何评价PS​​1,PS2,...? [英] bash: How to evaluate PS1, PS2, ...?

查看:104
本文介绍了庆典:如何评价PS​​1,PS2,...?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  回声扩大PS1

有什么办法来评价 PS1 PS2 等从一个bash脚本内?

Is there any way to 'evaluate' PS1, PS2, etc from within a bash script?

虽然,我可以使用备用手段得到我的电流 PS1的所有元素,我真的很希望能够重用,而不是使用这些替代方法的定义。

Although, I can use alternate means to get all elements of my current PS1, I would really like to be able to reuse its definition instead of using these alternate means.

例如,

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

我很可能使用替代方法列在我的剧本,但我不想。在我的 PS1 ,我举个例子,通过终端转义序列使用大胆的蓝色,我想这是能够简单地通过评估重用 PS1

I could very well use the 'alternate means' column in my script, but I don't want to. In my PS1, I, for example, use bold blue color via terminal escape sequences which I'd like to be able to simply reuse by evaluating PS1.

推荐答案

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

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

如果您下载庆典的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; { ... }

您可以尝试提取(使用任何必要的支持例程一起并生成可执行该做的工作适合你。虽然,从一个粗略的尝试,这些支持例程似乎是很多,所以这可能是一个艰巨的任务

You can try to extract that (along with any needed support routines and build an executable which did the job for you. Although, from a cursory try, those support routines seem to be a lot, so this may be a hard task.

除此之外,你或许可以绝招庆典与类似扩张为你:

Other than that, you can probably "trick" bash into expanding it for you with something like:

expPS1=$(echo xyzzyplughtwisty | bash -i 2>&1
         | grep xyzzyplughtwisty
         | head -1
         | sed 's/xyzzyplughtwisty//g')

现在我已经把那多行以增强可读性,但它是在一行上完成的。

Now I've put that across multiple lines for readability but it was done on one line.

这样做是运行的一个交互式实例庆典,传球(希望是什么)的命令无效。

What this does is run an interactive instance of bash, passing (what hopefully is) an invalid command.

由于它的互动,它打印的提示,所以我抓住与它的命令字符串的第一行,并删除命令字符串。什么是遗留下来的应该是提示。

Because it's interactive, it prints the prompt so I grab the first line with the command string on it and remove that command string. What's left over should be the prompt.

在我的系统,这是我得到:

On my system, this is what I get:

pax> expPS1=$(echo xyzzyplughtwisty | bash -i 2>&1 | grep xyzzyplughtwisty | head -1 | sed 's/xyzzyplughtwisty//g')

pax> echo "[$expPS1]"
[pax> ]

pax> 

不过,这与多行提示的问题,实际上给你定期提示,而不是当前shell之一。

However, this has problems with multi-line prompts and will actually give you your regular prompt rather than the current shell one.

如果你想这样做的正常,的可能涉及添加一点点地庆典本身。下面是添加一个内部命令的步骤 evalps1

If you want to do it properly, it may involve adding a little bit to bash itself. Here are the 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 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: ]

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

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

这篇关于庆典:如何评价PS​​1,PS2,...?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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