如何在KornShell中自定义显示提示以显示主机名和当前目录?

时间:2021-08-17 11:58:25

I am using KornShell (ksh) on Solaris and currently my PS1 env var is:

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

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

PS1 =“$ {HOSTNAME}:\ $ {PWD} \ $”

And the prompt displays: hostname:/full/path/to/current/directory $

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

However, I would like it to display: hostname:directory $

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

In other words, how can I display just the hostname and the name of the current directory, i.e. tmp or ~ or public_html etc etc?

换句话说,如何只显示主机名和当前目录的名称,即tmp或〜或public_html等等?

7 个解决方案

#1


From reading the ksh man page you want

从阅读你想要的ksh手册页

PS1="${HOSTNAME}:\${PWD##*/} \$ "

Tested on default ksh on SunOS 5.8

在SunOS 5.8上测试默认的ksh

#2


Okay, a little old and a little late, but this is what I use in Kornshell:

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

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

This makes a prompt that's equivalent to PS1="\u@\h:\w\n$ " in BASH.

这会在BASH中提示相当于PS1 =“\ u @ \ h:\ w \ n $”的提示。

For example:

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

I like a two line prompt because I sometimes have very long directory names, and they can take up a lot of the command line. If you want a one line prompt, just leave off the "\n" on the last print statement:

我喜欢两行提示,因为我有时会有很长的目录名,而且它们占用了很多命令行。如果您想要一行提示,请在最后一个打印语句中取消“\ n”:

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

That's equivalent to PS1="\u@\h:\w$ " in BASH:

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

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

It's not quite as easy as setting up a BASH prompt, but you get the idea. Simply write a script for PS1 and Kornshell will execute it.

它不像设置BASH提示那么容易,但你明白了。只需为PS1编写脚本,Kornshell就会执行它。


For Solaris and other Older Versions of Kornshell

I found that the above does not work on Solaris. Instead, you'll have to do it the real hackish way...

我发现上面的内容在Solaris上不起作用。相反,你必须以真正的hackish方式做到这一点......

  • In your .profile, make sure that ENV="$HOME/.kshrc"; export ENV is set. This is probably setup correctly for you.

    在.profile中,确保ENV =“$ HOME / .kshrc”;导出ENV已设置。这可能是你正确设置的。

  • In your .kshrc file, you'll be doing two things

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

    1. You'll be defining a function called _cd. This function will change to the directory specified, and then set your PS1 variable based upon your pwd.
    2. 您将定义一个名为_cd的函数。此函数将更改为指定的目录,然后根据您的pwd设置PS1变量。

    3. You'll be setting up an alias cd to run the _cd function.
    4. 您将设置别名cd以运行_cd功能。

This is the relevant part of the .kshrc file:

这是.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"

This will set your prompt as the equivelent BASH PS1="\u@\h:\w$ ". It isn't pretty, but it works.

这会将你的提示设置为等效的BASH PS1 =“\ u @ \ h:\ w $”。它不漂亮,但它的工作原理。

#3


ENV=~/.kshrc, and then in your .kshrc:

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"

Brad

#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'$

#6


and...

if you work between two shells for most of your effort [ksh and bourne sh] and desire a directory tracking display on your command line then PWD can be substituted easily in ksh and if you use /usr/xpg4/bin/sh for your sh SHELL, it will work there as well

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

#7


Try this:


PS1="\H:\W"

More information on: How to: Change / Setup bash custom prompt, I know you said ksh, but I am pretty sure it will work.

更多信息:如何:更改/设置bash自定义提示,我知道你说ksh,但我很确定它会工作。

#1


From reading the ksh man page you want

从阅读你想要的ksh手册页

PS1="${HOSTNAME}:\${PWD##*/} \$ "

Tested on default ksh on SunOS 5.8

在SunOS 5.8上测试默认的ksh

#2


Okay, a little old and a little late, but this is what I use in Kornshell:

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

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

This makes a prompt that's equivalent to PS1="\u@\h:\w\n$ " in BASH.

这会在BASH中提示相当于PS1 =“\ u @ \ h:\ w \ n $”的提示。

For example:

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

I like a two line prompt because I sometimes have very long directory names, and they can take up a lot of the command line. If you want a one line prompt, just leave off the "\n" on the last print statement:

我喜欢两行提示,因为我有时会有很长的目录名,而且它们占用了很多命令行。如果您想要一行提示,请在最后一个打印语句中取消“\ n”:

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

That's equivalent to PS1="\u@\h:\w$ " in BASH:

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

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

It's not quite as easy as setting up a BASH prompt, but you get the idea. Simply write a script for PS1 and Kornshell will execute it.

它不像设置BASH提示那么容易,但你明白了。只需为PS1编写脚本,Kornshell就会执行它。


For Solaris and other Older Versions of Kornshell

I found that the above does not work on Solaris. Instead, you'll have to do it the real hackish way...

我发现上面的内容在Solaris上不起作用。相反,你必须以真正的hackish方式做到这一点......

  • In your .profile, make sure that ENV="$HOME/.kshrc"; export ENV is set. This is probably setup correctly for you.

    在.profile中,确保ENV =“$ HOME / .kshrc”;导出ENV已设置。这可能是你正确设置的。

  • In your .kshrc file, you'll be doing two things

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

    1. You'll be defining a function called _cd. This function will change to the directory specified, and then set your PS1 variable based upon your pwd.
    2. 您将定义一个名为_cd的函数。此函数将更改为指定的目录,然后根据您的pwd设置PS1变量。

    3. You'll be setting up an alias cd to run the _cd function.
    4. 您将设置别名cd以运行_cd功能。

This is the relevant part of the .kshrc file:

这是.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"

This will set your prompt as the equivelent BASH PS1="\u@\h:\w$ ". It isn't pretty, but it works.

这会将你的提示设置为等效的BASH PS1 =“\ u @ \ h:\ w $”。它不漂亮,但它的工作原理。

#3


ENV=~/.kshrc, and then in your .kshrc:

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"

Brad

#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'$

#6


and...

if you work between two shells for most of your effort [ksh and bourne sh] and desire a directory tracking display on your command line then PWD can be substituted easily in ksh and if you use /usr/xpg4/bin/sh for your sh SHELL, it will work there as well

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

#7


Try this:


PS1="\H:\W"

More information on: How to: Change / Setup bash custom prompt, I know you said ksh, but I am pretty sure it will work.

更多信息:如何:更改/设置bash自定义提示,我知道你说ksh,但我很确定它会工作。