我有一个shell脚本,它在多个目录(fgit)中运行相同的命令。对于每个目录,我希望它显示当前的提示符+将在那里运行的命令。如何获得与解码的(展开的)PS1对应的字符串?例如,我的默认PS1是
${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)')$我想响应得到的提示username@hostname:/path$,最好(但不一定)用漂亮的颜色。粗略地看一看巴什手册并没有发现任何明确的答案,echo -e $PS1只对颜色进行评估。
发布于 2016-05-10 12:02:41
由于Bash4.4,您可以使用@P扩展:
首先,我使用myprompt将提示字符串放在变量read -r中,并在这里引用-doc:
read -r myprompt <<'EOF'
${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)')$
EOF要打印提示符(如果它是PS1,它将被解释),请使用展开${myprompt@P}
$ printf '%s\n' "${myprompt@P}"
gniourf@rainbow:~$
$(实际上,有一些\001和\002字符来自\[和\],您在这里看不到,但是如果您尝试编辑这个帖子,您可以看到它们;如果键入命令,您也会在终端中看到它们)。
为了消除这些问题,Dennis在bash邮件列表中发送的技巧是使用read -e -p,以便由readline库解释这些字符:
read -e -p "${myprompt@P}"这将提示用户,并正确解释myprompt。
对于这篇文章,Greg回答说,您最好把\001和\002从字符串中去掉。实现这一点的方式如下:
myprompt=${myprompt@P}
printf '%s\n' "${myprompt//[$'\001'$'\002']}"在这篇文章中,Chet回答说,您也可以使用set +o emacs +o vi完全关闭行编辑。所以这也适用于:
( set +o emacs +o vi; printf '%s\n' "${myprompt@P}" )发布于 2013-01-09 09:29:17
开源软件的一个巨大优点是,源代码是开放的:-)
Bash本身并不提供此功能,但您可以使用各种技巧来提供子集(例如用\u替换$USER等等)。但是,这需要大量的功能复制,并确保代码与bash今后所做的任何工作保持同步。
如果您想要获得提示变量的所有功能(并且您不介意用一点代码来弄脏您的手(如果您很介意,为什么会在这里?),那么添加到shell本身就很容易了。
如果下载bash的代码(我看的是4.2版),就会有一个y.tab.c文件,其中包含decode_prompt_string()函数:
char *decode_prompt_string (string) char *string; { ... }这是计算用于提示的PSx变量的函数。为了允许将此功能提供给shell本身的用户(而不仅仅是shell所使用的),您可以按照以下步骤添加内部命令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: ]当您将其中一个PSx变量放入提示符中时,回显$PS1只会给出变量,而evalps1命令则对其进行计算并输出结果。
当然,对bash进行代码更改以添加内部命令可能会被一些人认为是过火的,但是,如果您想要对PS1进行完美的评估,这当然是一种选择。
发布于 2010-08-10 20:37:41
为什么不自己处理$PS1转义替换呢?一系列替代办法,如:
p="${PS1//\\u/$USER}"; p="${p//\\h/$HOSTNAME}"顺便说一句,zsh有解释快速转义的能力。
print -P '%n@%m %d'或
p=${(%%)PS1}https://stackoverflow.com/questions/3451993
复制相似问题