shell位于linux系统中的什么位置,linux – 基于文件系统中的位置的Shell提示

我必须在根文件系统下的三个主目录中工作 – 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='\[\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

标签:bash,shell,unix,linux,command-prompt

来源: https://codeday.me/bug/20190612/1228138.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值