linux – 基于文件系统中的位置的Shell提示

linux – 基于文件系统中的位置的Shell提示,第1张

概述我必须在根文件系统下的三个主目录中工作 – home / username,project和scratch.我希望我的 shell提示符显示我所在的这些顶级目录. 这是我想要做的: top_level_dir (){ if [[ "${PWD}" == *home* ]] then echo "home"; elif [[ "${PWD}" == *scr 我必须在根文件系统下的三个主目录中工作 – home / username,project和scratch.我希望我的 shell提示符显示我所在的这些顶级目录.

这是我想要做的:

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

然后,我导出PS1为:

export PS1='$(top_level_dir) : '

不幸的是,这不符合我的要求.我回到家:因为我在我的主目录中的提示,但如果我切换到临时或项目,则提示不会改变.我不太了解bash脚本,所以我很感激任何帮助来纠正我的代码.

解决方法 您可以挂钩到cd以在每次更改工作目录时更改提示.我经常问自己如何挂入cd,但我认为我现在找到了解决方案.把它添加到〜/ .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='\[3[01;32m\]\u@\h:\w\[3[00m\]$'    elif [[ "$pwd/" =~ ^/etc/ ]] ; then        PS1='\[3[01;34m\]\u@\h:\w\[3[00m\]$'    elif [[ "$pwd/" =~ ^/tmp/ ]] ; then        PS1='\[3[01;33m\]\u@\h:\w\[3[00m\]$'    else        PS1='\u@\h:\w\$'    fi    export PS1}# checking directory and setting prompt on shell startupcolor_prompt
总结

以上是内存溢出为你收集整理的linux – 基于文件系统中的位置的Shell提示全部内容,希望文章能够帮你解决linux – 基于文件系统中的位置的Shell提示所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/1023533.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存