Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将Bash脚本转换为别名ssh。这样做的更好/最佳方式?_Bash_Unix_Cross Platform_Alias - Fatal编程技术网

将Bash脚本转换为别名ssh。这样做的更好/最佳方式?

将Bash脚本转换为别名ssh。这样做的更好/最佳方式?,bash,unix,cross-platform,alias,Bash,Unix,Cross Platform,Alias,我编写了以下脚本,以根据尝试连接到的主机更改shell的颜色。而且,尽管它有效,我想知道是否有更好的方法来做事情?具体来说,是否有更跨shell的方式来更改提示颜色?另外,是否有更好的方法将正则表达式应用于主机名(在grep/ack之外) 无论哪种情况,代码如下: function ssh() { #save all args (makes it easier to pass to ssh later) local all_args=$* #save path to

我编写了以下脚本,以根据尝试连接到的主机更改shell的颜色。而且,尽管它有效,我想知道是否有更好的方法来做事情?具体来说,是否有更跨shell的方式来更改提示颜色?另外,是否有更好的方法将正则表达式应用于主机名(在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作为一个相对非侵入性但突出的位置,可以提供一个视觉指示器,指示你在哪里。它还使shell的其余部分保持不变,这对于以其他方式(例如以不同颜色显示不同类型的文件)激活shell中的颜色非常重要

例如,我们将这种风格应用于盒子,并为产品、uat、stg、开发等使用不同的颜色

e、 g

这会给出一个2行提示,如

[501]:[/home/matt]
[matt@mybox] FOO $ 
其中,FOO具有纯红背景(在本例中)


PS1是一种sh(和变体)功能。顺便说一句。

我读过一些关于使用
PS1
改变颜色的文章,但是,无可否认,我并不完全理解它到底在改变什么,它与
tput
的关系,以及它是如何交叉的。您确实可以在
PS1
中使用颜色转义码来更改提示符本身的颜色,但是更改整个终端的颜色有点不同;无论是背景、提示、标题栏等都没有区别。但是,我只希望有一个调用,并让该调用在尽可能多的shell上工作。你会推荐
PS1
路线吗?我当然会推荐
PS1
路线。我自己也使用它,虽然并不是为了这个目的,多年来它在Solaris、AIX和一些Linux发行版上为我提供了很好的服务。
[501]:[/home/matt]
[matt@mybox] FOO $