【问题标题】:How to custom display prompt in KornShell to show hostname and current directory?如何在 KornShell 中自定义显示提示以显示主机名和当前目录?
【发布时间】:2010-11-13 09:18:09
【问题描述】:

我在 Solaris 上使用 KornShell (ksh),目前我的 PS1 env var 是:

PS1="${HOSTNAME}:\${PWD} \$ "

提示显示:hostname:/full/path/to/current/directory $

但是,我希望它显示:hostname:directory $

换句话说,我怎样才能只显示主机名和当前目录的名称,即tmp~public_html等?

【问题讨论】:

    标签: unix shell environment-variables customization ksh


    【解决方案1】:

    从阅读你想要的ksh man page

    PS1="${主机名}:\${密码##*/} \$"

    在 SunOS 5.8 上的默认 ksh 上测试

    【讨论】:

    【解决方案2】:

    好吧,有点老了,有点晚了,但这是我在 Kornshell 中使用的:

    PS1='$(print -n "`logname`@`hostname`:";if [[ "${PWD#$HOME}" != "$PWD" ]] then; print -n "~${PWD#$HOME}"; else; print -n "$PWD";fi;print "\n$ ")'
    

    这会产生一个相当于 BASH 中的 PS1="\u@\h:\w\n$ " 的提示。

    例如:

    qazwart@mybook:~
    $ cd bin
    qazwart@mybook:~/bin
    $ cd /usr/local/bin
    qazwart@mybook:/usr/local/bin
    $ 
    

    我喜欢两行提示,因为有时我的目录名很长,而且会占用大量命令行。如果你想要一个单行提示,只需在最后一个打印语句中去掉“\n”:

    PS1='$(print -n "`logname`@`hostname`:";if [[ "${PWD#$HOME}" != "$PWD" ]] then; print -n "~${PWD#$HOME}"; else; print -n "$PWD";fi;print "$ ")'
    

    这相当于 BASH 中的PS1="\u@\h:\w$ "

    qazwart@mybook:~$ cd bin
    qazwart@mybook:~/bin$ cd /usr/local/bin
    qazwart@mybook:/usr/local/bin$ 
    

    这并不像设置 BASH 提示那么简单,但你明白了。只需为PS1 编写一个脚本,Kornshell 就会执行它。


    适用于 Solaris 和其他旧版 Kornshell

    我发现上述方法在 Solaris 上不起作用。相反,你必须以真正的黑客方式来做......

    • 在您的.profile 中,确保ENV="$HOME/.kshrc"; export ENV 已设置。这可能对您来说设置正确。

    • 在您的 .kshrc 文件中,您将做两件事

      1. 您将定义一个名为_cd 的函数。此函数将切换到指定的目录,然后根据您的密码设置您的 PS1 变量。
      2. 您将设置一个别名 cd 来运行 _cd 函数。

    这是.kshrc文件的相关部分:

    function _cd {
       logname=$(logname)   #Or however you can set the login name
       machine=$(hostname)  #Or however you set your host name
       $directory = $1
       $pattern = $2        #For "cd foo bar"
    
       #
       # First cd to the directory
       # We can use "\cd" to evoke the non-alias original version of the cd command
       #
       if [ "$pattern" ]
       then
           \cd "$directory" "$pattern"
       elif [ "$directory" ]
       then
           \cd "$directory"
       else
           \cd
       fi
    
       #
       # Now that we're in the directory, let's set our prompt
       #
    
       $directory=$PWD
       shortname=${directory#$HOME}  #Possible Subdir of $HOME
    
       if [ "$shortName" = "" ]  #This is the HOME directory
       then
            prompt="~$logname"   # Or maybe just "~". Your choice
       elif [ "$shortName" = "$directory" ] #Not a subdir of $HOME
       then
            prompt="$directory"
       else
            prompt="~$shortName"
       fi
       PS1="$logname@$hostname:$prompt$ "  #You put it together the way you like
    }
    
    alias cd="_cd"
    

    这会将您的提示设置为等效的 BASH PS1="\u@\h:\w$ "。它不漂亮,但很有效。

    【讨论】:

    • 好的。已添加。
    • 谢谢:PS1='$(print -n "logname@hostname:"; [[ "${PWD#$HOME}" != "$PWD" ] ] && print -n "~${PWD#$HOME}" || print -n "$PWD";print "\n$ ")'
    【解决方案3】:

    ENV=~/.kshrc,然后在你的 .kshrc 中:

    function _cd {
      \cd "$@"
      PS1=$(
        print -n "$LOGNAME@$HOSTNAME:"
        if [[ "${PWD#$HOME}" != "$PWD" ]]; then
          print -n "~${PWD#$HOME}"
        else
          print -n "$PWD"
        fi
        print "$ "
      )
    }
    
    alias cd=_cd
    
    cd "$PWD"
    

    布拉德

    【讨论】:

      【解决方案4】:
      HOST=`hostname`
      PS1='$(print -n "[${USER}@${HOST%%.*} ";[[ "$HOME" == "$PWD" ]] && print -n "~" ||([[ "${PWD##*/}" == "" ]] && print -n "/" || print -n "${PWD##*/}");print "]$")'
      

      【讨论】:

        【解决方案5】:
        PS1=`id -un`@`hostname -s`:'$PWD'$
        

        【讨论】:

        • 不鼓励仅使用代码的答案,因为它们没有说明如何解决问题。请更新您的答案,以解释这如何改进该问题已有的其他已接受和赞成的答案。此外,这个问题已有 8 年历史,您的努力会受到最近有未回答问题的用户的赞赏。请查看How do I write a good answer
        【解决方案6】:

        还有……

        如果您的大部分精力都在两个 shell 之间工作 [ksh 和 bourne sh] 并希望在命令行上显示目录跟踪 那么 PWD 可以在 ksh 中轻松替换 如果你使用 /usr/xpg4/bin/sh 作为你的 sh SHELL,它也可以在那里工作

        【讨论】:

          【解决方案7】:

          试试这个:

          
          PS1="\H:\W"
          

          更多信息:How to: Change / Setup bash custom prompt,我知道你说的是 ksh,但我很确定它会起作用。

          【讨论】:

          • 抱歉,您输入的内容无法使用。我相当确定您输入的是 BASH 语法。谢谢你。
          • 该提示行在 OpenBSD 6 ksh 上或多或少地产生了所需的结果。我自己正在使用 PS1="\h:\W "。
          • 在 ksh 上也适用于我。
          猜你喜欢
          • 2016-11-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-05-20
          • 1970-01-01
          • 1970-01-01
          • 2010-09-21
          相关资源
          最近更新 更多