2011-05-07 2 views
1

나는 연결하려는 호스트에 따라 쉘의 색상을 변경하기 위해 다음 스크립트를 작성했습니다. 그리고 그것이 효과가 있지만, 일을하는 더 좋은 방법이 있는지 궁금합니다. 특히, 프롬프트 색상을 변경하는 더 많은 교차 셸 방식이 있습니까? 또한 호스트 이름 (grep/ack 외부)에 정규식을 적용하는 더 나은 방법이 있습니까?ssh의 별명을 지정하는 Bash 스크립트. 이렇게하는 것이 더 나은/최적의 방법입니까?

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

날 당신이 생각, 감사 알려주십시오 :

두 경우 모두, 여기에 코드입니다!

- 매트

+0

'PS1'을 사용하여 색상을 변경하는 방법에 대해 읽었지 만, 변경되는 내용, 'tput'과의 관련성 및 크로스 쉘 방식을 정확히 이해하지 못했습니다. http://www.cyberciti.biz/faq/bash-shell-change-the-color-of-my-shell-prompt-under-linux-or-unix/ –

+0

실제로 'PS1'에서 색상 이스케이프 코드를 사용할 수 있습니다. 프롬프트 자체의 색상을 변경하지만 전체 터미널의 색상을 변경하는 것은 약간 다릅니다. –

+0

변경하려는 _something_의 색상 만 찾고 있습니다. 그것이 배경인지, 프롬프트인지, 제목 표시 줄인지 여부는 아무런 차이가 없습니다. 그러나, 나는 단지 1 개의 전화를 가지고 그 전화가 가능한 한 많은 셸에서 작동하도록하고 싶습니다. 'PS1' 루트를 추천 해 주시겠습니까? –

답변

3

PS1은 당신이 어디에 있는지에 대한 시각적 표시를 제공하기 위해 상대적으로 비 침습적하지만 눈에 잘 띄는 장소로 아주 잘 작동합니다. 또한 셸의 나머지 부분을 손대지 않은 채로 남겨 둡니다. 다른 색상으로 다른 유형의 파일을 보여주는 것과 같이 셸에서 색상을 활성화하는 경우 중요합니다.

예를 들어 우리는 DEV 등 자극, UAT, STG,

예를 들어, 서로 다른 색상을 사용 & 상자에이 스타일을 적용

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

그래서 이것은

FOO (이 예에서)을 적색 고체 배경이
[501]:[/home/matt] 
[[email protected]] FOO $ 

같은 2 라인 프롬프트를 제공한다.

PS1은 sh (및 변형) 기능 btw입니다.

+2

그리고 ~/.ssh/config에'SendEnv PS1 = ....'을 포함시킬 수 있습니다 – sehe

+0

정말 고마워요! –

관련 문제