将csh提示设置为多行和特殊字符的命令的原始输出?

huangapple go评论57阅读模式
英文:

Set csh prompt to raw output of a command with multiple lines and special characters?

问题

我正在尝试将我的csh提示设置为特定命令的原始输出。我知道如何处理简单的输出;例如:

set prompt=`echo propmt%|sed "s/pm/mp/"`

... 这会得到一个提示:

prompt%

但是我想要的提示更复杂。首先,它有多行。我知道如何直接做到这一点(即不通过命令的输出):

set prompt="This is a line\nThis is another\nAnd this is the last% "

... 结果是:

This is a line
This is another
And this is the last%

但是,如果我尝试通过命令执行相同的操作:

set prompt=`echo "This is a line\
This is another\
And this is the last% "`

... 提示变成:

This

... 甚至不是 This is a line;只是 This

此外,我想要使用的特定命令将输出不仅多行,还会有一堆其他字符,我认为 csh 和/或 set 将以我不打算的方式解释这些字符。具体来说:

cowsay -f ghostbusters "BOOOOOOOOOOOOOOOOOOOOOOOOO \
? \
? You are using CEEEEEE SHELLLLLLLLLLLLLLLLL\
? \
? Be my guest, but you have no one to blame but\
? YOURSELLLLLLLLLLLLLLLLLLLLLLF"; echo; echo "% "

... 这会输出:

 _________________________________________
/ BOOOOOOOOOOOOOOOOOOOOOOOOO              \
|                                         |
| You are using CEEEEEE                   |
| SHELLLLLLLLLLLLLLLLL                    |
|                                         |
| Be my guest, but you have no one to     |
\ blame but YOURSELLLLLLLLLLLLLLLLLLLLLLF /
 -----------------------------------------
          \
           \
            \          __---__
                    _-       /--______
               __--( /     \ )XXXXXXXXXXX\v.
             .-XXX(   O   O  )XXXXXXXXXXXXXXX-
            /XXX(       U     )        XXXXXXX\
          /XXXXX(              )--_  XXXXXXXXXXX\
         /XXXXX/ (      O     )   XXXXXX   \XXXXX\
        /XXXXX/   /            XXXXXX   \__ \XXXXX
       /XXXXX__/          XXXXXX         \__---->
 ---___  XXX__/          XXXXXX      \__         /
   \-  --__/   ___/\  XXXXXX            /  ___--/=
    \-\    ___/    XXXXXX              '--- XXXXXX
       \-\/XXX\ XXXXXX                      /XXXXX
         \XXXXXXXXX   \                    /XXXXX/
          \XXXXXX      >                 _/XXXXX/
            \XXXXX--__/              __-- XXXX/
             -XXXXXXXX---------------  XXXXXX-
                \XXXXXXXXXXXXXXXXXXXXXXXXXX/
                  ""VXXXXXXXXXXXXXXXXXXV""
%

这就是我想要作为我的csh提示的样子。在这一点上,我觉得有必要指出我不是在开玩笑。

我想我可以忘记运行一个命令来生成它,而是手动将其转换为适用于 set prompt 的单行字符串,但是:

  1. 这看起来很麻烦;
  2. 我想能够快速轻松地将提示更改为类似但不同的内容。

所以,我希望有一种告诉 set prompt "只使用 (任意命令) 的原始输出;不要将任何内容解释为特殊字符或其他什么" 的方法。有这样的方法吗?或者有其他建议来实现我想要的目标吗?谢谢。

英文:

I am trying to set my csh prompt to the raw output of a particular command. I know how to do that for simple outputs; for example:

set prompt=`echo propmt%|sed "s/pm/mp/"`

... results in a prompt of:

prompt%

But the prompt I want is more complex. First, it has multiple lines. I know how to do that directly (i.e. not via the output of a command):

set prompt="This is a line\nThis is another\nAnd this is the last% "

... results in:

This is a line
This is another
And this is the last%

But if I try to do that same thing through a command:

set prompt=`echo "This is a line\
This is another\
And this is the last% "`

... the prompt becomes just:

This

... not even This is a line; just This.

On top of that, the specific command that I want to use will output not just multiple lines, but also a bunch of other characters that I assume csh and/or set will interpret in ways that I don't intend. Specifically:

cowsay -f ghostbusters "BOOOOOOOOOOOOOOOOOOOOOOOOO \
? \
? You are using CEEEEEE SHELLLLLLLLLLLLLLLLL\
? \
? Be my guest, but you have no one to blame but\
? YOURSELLLLLLLLLLLLLLLLLLLLLLF"; echo; echo "% "

... which outputs:

 _________________________________________
/ BOOOOOOOOOOOOOOOOOOOOOOOOO              \
|                                         |
| You are using CEEEEEE                   |
| SHELLLLLLLLLLLLLLLLL                    |
|                                         |
| Be my guest, but you have no one to     |
\ blame but YOURSELLLLLLLLLLLLLLLLLLLLLLF /
 -----------------------------------------
          \
           \
            \          __---__
                    _-       /--______
               __--( /     \ )XXXXXXXXXXX\v.
             .-XXX(   O   O  )XXXXXXXXXXXXXXX-
            /XXX(       U     )        XXXXXXX\
          /XXXXX(              )--_  XXXXXXXXXXX\
         /XXXXX/ (      O     )   XXXXXX   \XXXXX\
         XXXXX/   /            XXXXXX   \__ \XXXXX
         XXXXXX__/          XXXXXX         \__---->
 ---___  XXX__/          XXXXXX      \__         /
   \-  --__/   ___/\  XXXXXX            /  ___--/=
    \-\    ___/    XXXXXX              '--- XXXXXX
       \-\/XXX\ XXXXXX                      /XXXXX
         \XXXXXXXXX   \                    /XXXXX/
          \XXXXXX      >                 _/XXXXX/
            \XXXXX--__/              __-- XXXX/
             -XXXXXXXX---------------  XXXXXX-
                \XXXXXXXXXXXXXXXXXXXXXXXXXX/
                  ""VXXXXXXXXXXXXXXXXXXV""
%

That's what I want as my csh prompt. At this point, I feel obligated to note that I am not kidding.

I suppose I could forget about running a command to generate it, and instead just manually converting it to a single-line string that's properly escaped for set prompt, but:

  1. That seems like a pain;
  2. I'd like to be able to quickly and easily change the prompt to similar but different things.

So, I'm hoping for some way to tell set prompt "Just use the raw output of (arbitrary-command); don't interpret any of it as special characters or whatever". Is there a way? Or any alternate suggestions to accomplish what I want? Thanks.

答案1

得分: 1

也许最好使用 precmd 别名来实现这个:

alias precmd 'cowsay -f ghostbusters << EOF \
line 1 \
\
line 2 \
\
line 3 \
EOF';

根据csh(1)的说明:

precmd 在每个提示打印之前运行。例如,如果执行以下命令:

alias precmd date

那么 date(1) 会在Shell提示每个命令之前运行。

precmd 可以设置为做任何事情,但应谨慎使用。

或者,您也可以将其放在不同的文件中以避免所有反斜杠:

alias precmd 'source ~/cowsay'

# ~/cowsay
cowsay -f ghostbusters << EOF
line 1

line 2

line 3
EOF
英文:

Maybe it's better to use the precmd alias for this:

alias precmd &#39;cowsay -f ghostbusters &lt;&lt; EOF \
line 1 \
\
line 2 \
\
line 3 \
EOF&#39;

From csh(1):

 precmd  Runs just before each prompt is printed.  For example, if one
         does

               &gt; alias precmd date

         then date(1) runs just before the shell prompts for each command.

         There are no limits on what precmd can be set to do, but
         discretion should be used.

Or you can make it a different file to avoid all the backslashes:

alias precmd &#39;source ~/cowsay&#39;

# ~/cowsay
cowsay -f ghostbusters &lt;&lt; EOF
line 1

line 2

line 3
EOF

huangapple
  • 本文由 发表于 2023年1月9日 06:48:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75051842.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定