hatunina’s blog

メモと日記です

接続先サーバによってTerminalの背景を変える

概要

devやproductionごとにTerminalの背景を変えることで作業ミスを防ぎたい

環境

$ sw_vers
ProductName:    Mac OS X
ProductVersion: 10.14.4
BuildVersion:   18E226

手順

  1. 接続先サーバによってTerminalの背景を変えるシェルスクリプトを書く
    1.1. 接続先サーバによって指定する背景を設定する
    1.2. 指定した背景はAppleScriptで変更可能 -> AppleScriptは/usr/bin/osascriptで実行可能
  2. 作成したシェルスクリプトsshコマンドのエイリアスに設定する

シェルスクリプト

#!/bin/bash


# Terminalの背景を変える関数
 set_profile() {
 /usr/bin/osascript -e "tell application \"Terminal\" to set current settings of first window to settings set \"$1\""
 }


# 接続先によって背景を指定する。SSHを終了するとHomebrew背景に戻る。
if [[ "$@" == *dev* ]]; then
 set_profile "Red Sands"
 ssh $@
 set_profile "Homebrew"
 elif [[ "$@" == *production* ]]; then
 set_profile "Ocean"
 ssh $@
 set_profile "Homebrew"
 else
 ssh $@
 fi

エイリアス

~/.bash_profile に追記

alias ssh="/usr/local/bin/ssh-change-theme"

確認

変更を適用

$ source ~/.bash_profile

sshしてみて確認する。

$ ssh hoge.dev.huga.co.jp

f:id:hatunina:20190707003732p:plain

$ ssh hoge.production.huga.co,jp

f:id:hatunina:20190707003807p:plain

参考

labs.septeni.co.jp maku77.github.io