简体   繁体   English

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

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

I wrote the following script to change the color of the shell depending on which host I'm trying to connect to. 我编写了以下脚本,根据我要连接到的主机来更改外壳的颜色。 And, although it works, I'm wondering if there's a better way to do things? 而且,尽管它可行,但我想知道是否有更好的方法来做事情? Specifically, is there a more cross-shell way of changing the prompt color? 具体来说,是否还有一种更跨外壳的方式来更改提示色? Also, is there a better approach to applying a regex to the host name (outside of grep/ack)? 另外,是否有更好的方法将正则表达式应用于主机名(在grep / ack之外)?

Either case, here's the code: 无论哪种情况,下面都是代码:

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\""
}

Let me know what you think, and thanks! 让我知道您的想法,谢谢!

- Matt -马特

PS1 works quite well as a relatively non invasive but prominent place to give a visual indicator on where you are. PS1在相对无创但突出的地方效果很好,可以直观地指示您的位置。 It also leaves the rest of the shell untouched which is important if you have colours active in the shell in other ways (like showing files of different types in different colours). 它还使外壳的其余部分保持不变,如果您以其他方式在外壳中激活了颜色(例如以不同的颜色显示不同类型的文件),则这很重要。

For example we apply this style to boxes & use different colours for prod, uat, stg, dev etc 例如,我们将这种样式应用于框并针对产品,uat,stg,dev等使用不同的颜色

eg 例如

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

so this gives a 2 line prompt like 所以这给出了两行提示

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

where FOO has a solid red background (in this example). 其中FOO具有稳定的红色背景(在此示例中)。

PS1 is a sh (and variants) feature btw. PS1是sh(和变体)功能。

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

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