PWD/CWD in ksh prompt howto?

PWD/CWD in ksh prompt howto?

Post by omi.. » Wed, 19 Jan 2000 04:00:00



Hi all,

  For the life of me I can't sucessfully get my
active working directory to be displayed as my
prompt.

I'm trying to get a prompt like :

HostnaMe: /usr/export/home/omitka/docs >

which changes to suit whatever directory i'm
working in.

--------------------------------------------
Platform: solaris 7

what i've tried in my .profile :

#
# shell prompt init
#
PS1="`uname -n`:`pwd` > "
export PS1

also :
#
# re-write CDfor shell pwd init.
#
function _cd {

es=$?
PS1=$($PWD)
return $es

Quote:}

alias cd=_cd

neither of these work.
Thanks in adv for all input.

Regards,
Omi.

Sent via Deja.com http://www.deja.com/
Before you buy.

 
 
 

PWD/CWD in ksh prompt howto?

Post by Matthew Land » Wed, 19 Jan 2000 04:00:00



> Hi all,

>   For the life of me I can't sucessfully get my
> active working directory to be displayed as my
> prompt.

> I'm trying to get a prompt like :

> HostnaMe: /usr/export/home/omitka/docs >

> which changes to suit whatever directory i'm
> working in.

> --------------------------------------------
> Platform: solaris 7

> what i've tried in my .profile :

> #
> # shell prompt init
> #
> PS1="`uname -n`:`pwd` > "
> export PS1

Use $PWD instead of `pwd`

PS1="`uname -n`:\$PWD"
export PS1

With the PS1 var, there is no need for the function below since the PS1
already has the $PWD.  The new alias using the _cd function will only
be causing overhead.  Not to mention it is not correct, and will change
your PS1 to plain old "/directory/structure"; is that what you want? Try:

function _cd {

ex=$?
PS1=$PWD
return $es

Quote:}

alias cd='_cd'

Here is my PS1 and I find it handy in MANY ways (not sure if it works
outside of AIX).  It is nice that if you use su or tn elsewhere, your
prompt follows all that stuff, and shows the immediate return code.  No
more `echo $?` after all the commands to find out the return code:


 - Matt

> also :
> #
> # re-write CDfor shell pwd init.
> #
> function _cd {

> es=$?
> PS1=$($PWD)
> return $es
> }

> alias cd=_cd

--

  AIX and HACMP Certified Specialist   | |  / \ |\| |  \. ,_|  ` o O '
  / Comments, views, and opinions \    | |_/ ^ \|   | ) | |       x    
  \ are mine alone, not IBM's.    /    |___|/~\_\_|\|__/|_|     \___/

 
 
 

PWD/CWD in ksh prompt howto?

Post by Chri » Wed, 19 Jan 2000 04:00:00


:
: Hi all,
:
:   For the life of me I can't sucessfully get my
: active working directory to be displayed as my
: prompt.

Take the time and have a look at the FAQs for this group. You'll be
surprised what you'll find in there.

Chris

 
 
 

PWD/CWD in ksh prompt howto?

Post by Ken Pizzi » Thu, 20 Jan 2000 04:00:00



>  For the life of me I can't sucessfully get my
>active working directory to be displayed as my
>prompt.
>PS1="`uname -n`:`pwd` > "
>export PS1

As you've probably noticed, this just gives you the pwd that was
in effect at the time that this statement is executed by the
shell, not a pwd which gets updated as you cd around.  Try this
instead:
   export PS1=`uname -n`:'$PWD > '
Parameter substitution is done on the contents of the $PS1
variable whenever ksh is emits that prompt.

>also :
>#
># re-write CDfor shell pwd init.
>#
>function _cd {

>es=$?
>PS1=$($PWD)
>return $es
>}

>alias cd=_cd

That approach should work, but you have some bugs in the script;
try this instead:

  alias cd=_cd

                --Ken Pizzini

 
 
 

PWD/CWD in ksh prompt howto?

Post by omi.. » Thu, 20 Jan 2000 04:00:00


For any one reading this.
the ticks matter. i.e.  " ' " is not the same as " ` "

as thus:
PS1=' $PWD > '

is not the same syntactically as...

PS1=` $PWD > `

hope this saves some greif some where.

cheerz,
Omi.



> Hi all,

>   For the life of me I can't sucessfully get my
> active working directory to be displayed as my
> prompt.

> I'm trying to get a prompt like :

> HostnaMe: /usr/export/home/omitka/docs >

> which changes to suit whatever directory i'm
> working in.

> --------------------------------------------
> Platform: solaris 7

> what i've tried in my .profile :

> #
> # shell prompt init
> #
> PS1="`uname -n`:`pwd` > "
> export PS1

> also :
> #
> # re-write CDfor shell pwd init.
> #
> function _cd {

> es=$?
> PS1=$($PWD)
> return $es
> }

> alias cd=_cd

> neither of these work.
> Thanks in adv for all input.

> Regards,
> Omi.

> Sent via Deja.com http://www.deja.com/
> Before you buy.

Sent via Deja.com http://www.deja.com/
Before you buy.
 
 
 

PWD/CWD in ksh prompt howto?

Post by brian hile » Sat, 22 Jan 2000 04:00:00


omi...@my-deja.com wrote:
> I'm trying to get a prompt like :
> HostnaMe: /usr/export/home/omitka/docs >

Use my script, which functions as a front-end to create an appropriate
prompt, It's a little rough around the edges, but will suffice
robustly for the purposes that you have in mind:

. mkprompt
mkprompt --hostname ': ' --cwd '> '

The advantage to you is that a prompt will be constructed (you may
then unset or delete "mkprompt" -- it is then not further accessed)
that will attempt to ellide the pathname of your cwd by using the
"~/..." notation:

HostnaMe: ~/omitka/docs >

#! /bin/echo error: only source
#*TAG:50241 13:Oct 18 1998:0644:mkprompt:
# Author: Brian Hiles <b...@iname.com>
# Copyright: (c) 1998
# Date: 1998/10/18
# Description: construct a custom PS1 prompt
# Name: mkprompt
# Sccs: @(#)mkprompt.sh 1.3 1998/02 b...@iname.com (Brian Hiles)
# Usage: mkprompt [option|string]...
# Version: 1.03

#01
function mkprompt # [option|string]...
# construct a custom PS1 prompt
{       set -o noglob
        typeset IFS TERM=${TERM:-dumb} bell=$(print \\007) esc=$(print \\033)
        typeset ext ipost ipre nl='
' ps1 wpost wpre xport xterm
        [[ $#$1 = @(0|1--) ]] &&
        {       # no arguments: output code suitable for re-input
                [[ $PS1 = *\${HOSTNAME}* ]] &&
                print -r ': ${HOSTNAME:=$(hostname)}'
                [[ $PS1 = *SECONDS* ]] &&
                print "SECONDS=\$(date '+%H*3600+%M*60+%S')" &&
                print 'typeset -Z2 _h _m _s\ntypeset -i _t'
                [[ $PS1 = *PWD* ]] &&
                print 'TILDE=\~'
                [[ $PS1 = *\${SHLVL}* ]] && print 'typeset -i10 -x SHLVL' \
                "\nalias -x ksh='SECONDS=\$SECONDS SHLVL=${SHLVL:-0}+1 ksh'"
                [[ " $(typeset +x) " = *' 'PS1' '* ]] &&
                print -n 'typeset -x TILDE '
                print -r -- "PS1='$PS1'"
                return 0
        }
        #XXX unused options: fgqvy
        [[ $#$1 = 1@(-h|--help) ]] &&
        {       print -r -- \
'-a|--alert     - sound a bell
-b|--bold       - turn on bold video
-c|--cwd        - show current working directory
-d|--date       - show date
-e|--exmark     - show an exclamation mark
-h|--history    - show history number
-i|--icon       - set title of icon with subsequent PS1 text
XXX+i|--unicon  - terminate title of icon
-j|--newline    - show a newline
-k|--blink      - turn on blinking video
-l|--level      - show shell level
-m|--dim        - turn on dim video
-n|--hostname   - show hostname
-o|--status     - show exit status of previous command
-p|--prompt     - show prompt character (dollar sign or hash mark)
-r|--reverse    - show reverse video
-s|--standout   - turn on reverse video
-t|--time       - show 24-hour time
-u|--user       - show username
-w|--window     - set title of window (XXXand icon?) with subsequent PS1 text
XXX+w|--unwindow        - terminate title of window
-x|--export     - export PS1 to subshells
-z|--zero       - zero out prompt (use with --window/--icon options.)
XXX-z|--zero    - terminate title of window
Without arguments "mkprompt" outputs shell code to recreate the current prompt.'
                return 0
        }
        while (($#))
        do      case $1 in
                (--)    break ;;
                (-a|--alert)
                        ATTR_BELL=${ATTR_BELL#$esc} \
                        : ${ATTR_BELL:=$(tput -T$TERM bel)}
                        ps1="$ps1${ATTR_BELL:-$bell}" ;;
                (-b|--bold)
                        ATTR_SMSO=${ATTR_SMSO#$esc} \
                        : ${ATTR_SMSO:=$(tput -T$TERM smso)}
                        ps1="$ps1$esc$ATTR_SMSO" ;;
                (+b|--unbold)
                        ATTR_RMSO=${ATTR_RMSO#$esc} \
                        : ${ATTR_RMSO:=$(tput -T$TERM rmso)}
                        ps1="$ps1$esc$ATTR_RMSO" ;;
                (-c|--cwd)
                        ext="$ext${nl}TILDE=\\~" \
                        ps1="$ps1\${TILDE[(1-0\${PWD%%@([!/]*|\$HOME*)}1)]" \
                        ps1="$ps1:-}\${PWD#\$HOME}" ;;
                (-d|--date)
                        # Warning: not dynamically calculated each display
                        ps1="$ps1$(date +%D)" ;;
                (-e|--exmark)
                        ps1="$ps1!!" ;;
                (-h|--history)
                        ps1="$ps1!" ;;
                (-i|--icon)
                        #if [[ $1 = --icon=* ]]
                        #then   xterm=${1#--icon=}
                        #else   xterm=${1#-i}
                                #[[ -z $xterm ]] && xterm=$2 shift
                        #fi
                        case $xterm in
                        (630?(-*))
                                ipre= ipost= ;; #XXX
                        (dtterm?(-*)|sun-cmd?(-*))
                                ipre=$esc]L ipost=$esc ;;
                        (hpterm?(-*))
                                ipre=$esc\&f-1k\${#PS1}D ipost=' ' ;;
                        (xterm?(-*))
                                ipre=$esc]1\; ipost=$bell ;;
                        esac
${DIAG:+print -ru2 [ $0: xterm: $xterm ]
_inst="$(print -nr -- "$ipre"|od -c|head -1)"
print -ru2 [ preamble: ${_inst#0000000  } ]
_inst="$(print -nr -- "$ipost"|od -c|head -1)"
print -ru2 [ postamble: ${_inst#0000000  } ]}
                        ;;
                (-j|--newline)
                        ps1="$ps1$nl" ;;
                (-k|--blink)
                        ATTR_BLINK=${ATTR_BLINK#$esc} \
                        ATTR_SGR0=${ATTR_SGR0#$esc} \
                        : ${ATTR_BLINK:=$(tput -T$TERM blink)} \
                        ${ATTR_SGR0:=$(tput -T$TERM sgr0)}
                        ps1="$ps1$esc$ATTR_BLINK" ;;
                (-l|--level)
                        ext="$ext${nl}typeset -i10 -x SHLVL" \
                        ext="$ext${nl}alias -x ksh='SHLVL=\${SHLVL:-0}+1 ksh'" \
                        ps1="$ps1\${SHLVL}" ;;
                (-m|--dim)
                        ATTR_DIM=${ATTR_DIM#$esc} \
                        ATTR_SGR0=${ATTR_SGR0#$esc} \
                        : ${ATTR_DIM:=$(tput -T$TERM dim)} \
                        ${ATTR_SGR0:=$(tput -T$TERM sgr0)}
                        ps1="$ps1$esc$ATTR_DIM" ;;
                (+m|--undim)
                        ATTR_SGR0=${ATTR_SGR0#$esc} \
                        : ${ATTR_SGR0:=$(tput -T$TERM sgr0)}
                        ps1="$ps1$esc$ATTR_SGR0" ;;
                (-n|--hostname)
                        # Ksh93 does command substitution within PS1;
                        # ksh88 only implements variable substitution.
                        ps1="$ps1\${HOSTNAME}" \
                        : ${HOSTNAME:=$(hostname)} ;;
                (-o|--status)   # cannot as yet suppress display if $? equals 0
                        ps1="$ps1[\$?]" ;; #XXX no [, ] !!
                (-p|--prompt)
                        # Warning: not dynamically calculated each display
                        if [[ $(LANG=C /bin/id) = uid=0\(* ]]
                        then    ps1="$ps1#"
                        else    ps1="$ps1\\$"
                        fi ;;
                (-r|--reverse)
                        ATTR_REV=${ATTR_REV#$esc} \
                        ATTR_SGR0=${ATTR_SGR0#$esc} \
                        : ${ATTR_REV:=$(tput -T$TERM rev)} \
                        ${ATTR_SGR0:=$(tput -T$TERM sgr0)}
                        ps1="$ps1$esc$ATTR_REV" ;;
                (+r|--unreverse)
                        ATTR_SGR0=${ATTR_SGR0#$esc} \
                        : ${ATTR_SGR0:=$(tput -T$TERM sgr0)}
                        ps1="$ps1$esc$ATTR_SGR0" ;;
                (-s|--standout)
                        ATTR_SMSO=${ATTR_SMSO#$esc} \
                        ATTR_RMSO=${ATTR_RMSO#$esc} \
                        : ${ATTR_SMSO:=$(tput -T$TERM smso)} \
                        ${ATTR_RMSO:=$(tput -T$TERM rmso)}
                        ps1="$ps1$esc$ATTR_SMSO" ;;
                (+s|--unstandout)
                        ATTR_RMSO=${ATTR_RMSO#$esc} \
                        : ${ATTR_RMSO:=$(tput -T$TERM rmso)}
                        ps1="$ps1$esc$ATTR_RMSO" ;;
                (-t|--time)
                        ext="$ext${nl}typeset -\${xport}Z2 _h _m _s" \
                        ext="$ext${nl}typeset -i\$xport _t" \
                        ext="$ext${nl}SECONDS=\$(date '+%H*3600+%M*60+%S')" \
                        ext="$ext${nl}TILDE=\\~" \
                        ps1="$ps1\${TILDE[1+((_t=SECONDS)==(_s=_t%60)==" \
                        ps1="$ps1(_m=_t/60%60)==(_h=_t/3600%24))]}" \
                        ps1="$ps1\$_h:\$_m:\$_s" ;;
                        #ps1="$ps1(_m=(_t/60)%60)==(_h=(_t/3600)%24))]}" \
                (-u|--user)
                        ps1="$ps1\${LOGNAME:-\$USER}" ;;
                (-w|--window)
                        #if [[ $1 = --window=* ]]
                        #then   xterm=${1#--window=}
                        #else   xterm=${1#-w}
                                #[[ -z $xterm ]] && xterm=$2 shift
                        #fi
                        case $xterm in
                        (630?(-*))
                                wpre="$esc[?\${#PS1};0v" wpost= ;;
                        (dtterm?(-*)|sun-cmd?(-*))
                                wpre=$esc]l wpost=$esc ;;
                        (hpterm?(-*))
                                wpre=$esc\&f0k\${#PS1}D wpost=' ' ;;
                        (xterm?(-*))
                                wpre=$esc]2\; wpost=$bell ;;
                        esac
${DIAG:+print -ru2 [ $0: xterm: $xterm ]
_inst="$(print -nr -- "$wpre"|od -c|head -1)"
print -ru2 [ preamble: ${_inst#0000000  } ]
_inst="$(print -nr -- "$wpost"|od -c|head -1)"
print -ru2 [ postamble: ${_inst#0000000  } ]}
                        ;;
                (-z|--zero) #XXX will not work!
                        ps1= ;; #XXX ps1="$ps1\$PS1=" ??
                (--*)   print -ru2 "$0: error: unknown option \"$1\""
                        return 2 ;;
                (-??*)  #XXX convert into builtins:
                        eval "shift
                        set -- $(print -r -- "${1#-}" |
                        sed 's/./-& /g; s/ -$//') \"\$@\""
                        continue ;;
                (-?)    print -ru2 "$0: error: unknown option \"$1\""
                        return 2 ;;
                (*)     ps1="$ps1$1" ;;
                esac
                shift
        done
        # just in case: turn off all video attributes
        ATTR_SGR0=${ATTR_SGR0#$esc} : ${ATTR_SGR0:-$(tput -T$TERM sgr0)}
        # ksh88 bug: even a quoted "[" cannot be matched in patterns!
        [[ $ps1 = *$esc* && $ps1 != *$esc$ATTR_SGR0 ]] &&
        ps1="$ps1$esc$ATTR_SGR0"
        [[ -n $ps1 && $ps1 != *' ' ]] && ps1="$ps1 "
        # uncomment line below to explicate constructed PS1 and return:
        #print -n "$ps1" | od -c >&2 && return
        # uncomment line below to work around broken shells (Solaris 2.5.x):
        #eval '$ext'; ${xport:+export TILDE} PS1='$ps1'
        trap "eval '$ext'; ${xport:+typeset -x TILDE} PS1='$ps1'" EXIT
        return 0

}

#02 EMBEDDED MAN-PAGE FOR "scr2man"
: '
#++
NAME
        mkprompt - construct a custom PS1 prompt in ksh

SYNOPSIS
        mkprompt option|string ...
        mkprompt

DESCRIPTION
        Mkprompt constructs a PS1 prompt for use under ksh(1) and other shells
        that perform variable substitution on PS1 and implement arrays. There
        is no requirement for any other invocation of mkprompt thereafter.

        None of the options insert any whitespace, etc, into PS1. To do so,
        apply an explicit space character (which has to be protected by quotes)
        as a command line parameter. However, one space is appended if PS1 not
        already terminated by whitespace.

        Note that PS1 is not automatically exported to subshells by mkprompt,
        even if the "--level" option is active.

        From the SunOS 4.x terminfo(5) man page:

                "If your terminal has one or more kinds of display attri-
                butes, these can be represented in a number of different
                ways. You should choose one display form as standout mode
                (see curses(3V)), representing a good, high contrast, easy-
                on-the-eyes, format for highlighting error messages and
                other attention getters. (If you have a choice, reverse-
                video plus half-bright is good, or reverse-video alone; how-
                ever, different users have different preferences on dif-
                ferent terminals.)"

        URL of xterm FAQ. #XXX

OPTIONS
        -a|--alert      - Sound a bell (if term supports capability.)
        -b|--bold       - Turn on bold video (if term supports capability.)
        -c|--cwd        - Show current working directory.
        -d|--date       - Show date.
        -e|--exmark     - Show an exclamation mark.
        -h|--history    - Show history number.
        -i|--icon       - Set title of icon with prompt string.
        -j|--newline    - Show a newline.
        -k|--blink      - Turn on blinking video (if term supports capability.)
        -l|--level      - Show shell level.
        -m|--dim        - Turn on dim video (if term supports capability.)
        -n|--hostname   - Show hostname.
        -o|--status     - Show exit status of previous command.
        -p|--prompt     - Show prompt character (dollar sign or hash mark.)
        -r|--reverse    - Show reverse video (if term supports capability.)
        -s|--standout   - Turn on reverse video
...

read more »

 
 
 

1. pwd as prompt (was: pwd as prompt )

Oops. Sorry for posting the reply twice - finger trouble meant that
half the first message got lost.

Either that or the caffeine to blood ratio got too low :-}

Regards

--
------------------------------------------------------------------------------
Bruce Horrocks                       Windows NT -
Surrey, England                      "WOMAN PREGNANT FOR 65 YEARS GIVES

------------------------------------------------------------------------------

2. NEC Printer Won't

3. how to get cwd/pwd as shell prompt

4. Question about Intel 82557 Ethernet Pro

5. actual pwd in ksh-prompt

6. fixed com4 and sls1.03

7. Changing prompt in KSH to display PWD

8. groff-related mailing lists (fwd)

9. ksh & perl approach to pruning long pwd prompts

10. How do I put `pwd` in sh or ksh prompt?

11. pwd-prompt in ksh

12. How do I put `pwd` in sh or ksh prompt?

13. Prompt with pwd in ksh?