繁体   English   中英

Bash脚本别名ssh。 更好/最佳的方式来做到这一点?

[英]Bash script to alias ssh. Better/optimal way to do this?

我编写了以下脚本,根据我要连接到的主机来更改外壳的颜色。 而且,尽管它可行,但我想知道是否有更好的方法来做事情? 具体来说,是否还有一种更跨外壳的方式来更改提示色? 另外,是否有更好的方法将正则表达式应用于主机名(在grep / ack之外)?

无论哪种情况,下面都是代码:

function ssh() {

    #save all args (makes it easier to pass to ssh later)
    local all_args=$*

    #save path to ssh exec in current $PATH
    local ssh_path=$(which ssh)

    # host is second to last arg. see ssh -h
    local host=${@:(-2):1}


    #### color codes for tput ####
    # setaf=foreground, setab=background
    # 0 Black
    # 1 Red
    # 2 Green
    # 3 Yellow
    # 4 Blue
    # 5 Magenta
    # 6 Cyan
    # 7 White
    # sgr0 reset
    ##############################

    #### Or if you're on a Mac ####
    # you can use an AppleScript to
    # change to a different Terminal
    # setting. I use Pro (white/black)
    # by default, but jump to a custom
    # one called 'mpowell-md' which
    # is a shade of red when connecting
    # to mpowell-md
    ###############################

    case $host in

            # can use basic regex here
            *mpowell\-md*)
                    # osascript -e "tell application \"Terminal\" to set current settings of first window to settings set named \"mpowell-md\""
                    tput setaf 1;#red
            ;;

            # default case
            *)
                    # could default to setting it back to Pro, etc...
                    # osascript -e "tell application \"Terminal\" to set current settings of first window to settings set named \"Pro\""
            ;;
    esac

    #run and wait for ssh to finish
    eval "$ssh_path $all_args"


    tput sgr0;#reset
    #osascript -e "tell application \"Terminal\" to set current settings of first window to settings set named \"Pro\""
}

让我知道您的想法,谢谢!

-马特

PS1在相对无创但突出的地方效果很好,可以直观地指示您的位置。 它还使外壳的其余部分保持不变,如果您以其他方式在外壳中激活了颜色(例如以不同的颜色显示不同类型的文件),则这很重要。

例如,我们将这种样式应用于框并针对产品,uat,stg,dev等使用不同的颜色

例如

PS1="[\!]:[\w]\n[\u@\h] \[\033[1m\]\[\033[41m\] $SOME_VARIABLE \[\033[0m\] $ "

所以这给出了两行提示

[501]:[/home/matt]
[matt@mybox] FOO $ 

其中FOO具有稳定的红色背景(在此示例中)。

PS1是sh(和变体)功能。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM