Shell prompt that is based on location in filesystem

我们两清 提交于 2019-12-17 19:52:38

问题


I have to work within three main directories under the root filesystem - home/username, project, and scratch. I want my shell prompt to display which of these top level directories i am in.

Here is what I am trying to do:

top_level_dir ()
{
    if [[ "${PWD}" == *home* ]]
    then
        echo "home";
    elif [[ "${PWD}" == *scratch* ]]
    then
        echo "scratch";
    elif [[ "${PWD}" == *project* ]]
    then
        echo "project";
    fi

}

Then, I export PS1 as:

export PS1='$(top_level_dir) : '

Unfortunately this is not working as I want. I get home : for my prompt when I am in my home directory, but if I switch to scratch or projects then the prompt does not change. I do not understand bash scripting very well so I would appreciate any help to correct my code.


回答1:


You can hook into cd to change the prompt every time you are changing the working directory. I've asked myself often how to hook into cd but I think that I now found a solution. What about adding this to your ~/.bashrc?:

#
# Wrapper function that is called if cd is invoked
# by the current shell
#
function cd {
    # call builtin cd. change to the new directory
    builtin cd $@
    # call a hook function that can use the new working directory
    # to decide what to do
    color_prompt
}

#
# Changes the color of the prompt depending
# on the current working directory
#
function color_prompt {
    pwd=$(pwd)
    if [[ "$pwd/" =~ ^/home/ ]] ; then
        PS1='\[\033[01;32m\]\u@\h:\w\[\033[00m\]\$ '
    elif [[ "$pwd/" =~ ^/etc/ ]] ; then
        PS1='\[\033[01;34m\]\u@\h:\w\[\033[00m\]\$ '
    elif [[ "$pwd/" =~ ^/tmp/ ]] ; then
        PS1='\[\033[01;33m\]\u@\h:\w\[\033[00m\]\$ '
    else
        PS1='\u@\h:\w\\$ '
    fi
    export PS1
}


# checking directory and setting prompt on shell startup
color_prompt



回答2:


Please try this method instead and tell us how it works e.g. how your prompt changes in your home directory, your project or scratch directory, and other directories besides those. Tell us what error messages you see as well. The problem lies within it.

Tell me also how you run it, if it's by script, by direct execution, or through a startup script like ~/.bashrc.

top_level_dir ()
{
    __DIR=$PWD
    case "$__DIR" in
    *home*)
        echo home
        ;;
    *scratch*)
        echo scratch
        ;;
    *project*)
        echo project
        ;;
    *)
        echo "$__DIR"
        ;;
    esac
}

export PS1='$(top_level_dir) : '
export -f top_level_dir

If it doesn't work, try changing __DIR=$PWD to __DIR=$(pwd) and tell us if it helps too. I also would like to confirm if you're really running bash. Note that there are many variants of sh like bash, zsh, ksh, and dash and the one installed and used by default depends on every system. To confirm that you're using Bash, do echo "$BASH_VERSION" and see if it shows a message.

You should also make sure that you're running export PS1='$(top_level_dir) : ' with single quotes and not with double quotes: export PS1="$(top_level_dir) : ".



来源:https://stackoverflow.com/questions/18243946/shell-prompt-that-is-based-on-location-in-filesystem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!