LoginSignup
2
2

More than 5 years have passed since last update.

端末を開いたときにデフォルトでbyobu・cowsayを起動したい

Last updated at Posted at 2013-10-17

端末を開いたときにデフォルトでbyobucowsayを起動させたい。

要件

  1. 端末を開いたときにbyobuのウィンドウ上でcowsay出力
  2. ウィンドウを分割したときにはcowsay出力しない

実装

.bashrc
if [ -z "$(ls /tmp | grep hogehoge)" ]; then 
    if [ $SHLVL -eq 1 ]; then
        byobu
    elif [ $SHLVL -eq 2 ]; then 
        cowsay -f tux `fortune`
        touch /tmp/hogehoge
    fi
fi
alias exit='exitexit'
function exitexit() {
    if [ $SHLVL -eq 1 ]; then
        rm /tmp/hogehoge
    fi
    command exit
}

要件1を満たすために$SHLVLを用いて子シェルからcowsayが呼び出すようにしておく。また、このときに一時ファイル /tmp/hogehogeを生成しておく。これを用いて、ウィンドウ分割によってシェルからcowsayが呼び出されないように条件分岐して 要件2を満たす。そして、exitにはエイリアスを被せて親シェルを終了させるときに一時ファイルも消去するようにしておく。

実行結果

Welcome to Ubuntu 12.04.3 LTS (GNU/Linux 3.2.0-54-generic x86_64)

 \* Documentation:  https://help.ubuntu.com/
 _____________________________________
/ You have a deep appreciation of the \
\ arts and music.                     /
 -------------------------------------
   \
    \
        .--.
       |o_o |
       |:_/ |
      //   \ \
     (|     | )
    /'\_   _/`\
    \___)=(___/

[amayaw9 ~]$ 

疑問

変数を用いる方法も試したけど定義するシェルが違うと参照できない?他に、もっとスマートな方法があれば教えてエロい人!?

参考

Qiita Qiita / コマンドラインにイラストを

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2