如何判斷是否正在tty下使用screen/tmux - Linux

Table of Contents

我通常是用"tty"這個指令判斷是否正在tty下

例如

1. 在tty(1~6)時(假設在tty1)

user@local_host:~$ tty
/dev/tty/1 => tty


2. 在x11中使用gnome-termial時

user@local_host:~$ tty
/dev/pts/7 => pts

3. 用putty遠端到遠端Linux伺服器時

user@remote_host:~$ tty
/dev/pts/10 => pts

4. 在gnome-terminal中使用screen/tmux時

user@local_host:~$ tty
/dev/pts/8 => pts

不過

5. 在tty(1~6)中使用screen/tmux時

user@local_host:~$ tty
/dev/pts/8 => 恩...好希望它顯示的是tty = =


我希望在情況5.中, 能判斷出"我正在tty使用screen/tmux"

然而此時"tty"這個指令似乎就沒用了

請問有甚麼其他判斷方法嗎

(除了印出$TERM之外

因為不論是在x或tty中attach screen, $TERM的內容都是一樣的

還有我不希望手動調整$TERM的內容)

--

All Comments

Erin avatarErin2011-09-23
$WINDOW
Puput avatarPuput2011-09-26
alias tmux='export TMUXTTY=`tty`; tmux'
Puput avatarPuput2011-09-27
不知道上述作法是否符合需求.
Quanna avatarQuanna2011-10-01
你現在是需要在被控制程式中能取得目前控制之screen/tmux
Edward Lewis avatarEdward Lewis2011-10-02
嗎?被控制程式一定是shell或可能是任何程式?
Connor avatarConnor2011-10-07
我的shell指的是交談式的shell.
Todd Johnson avatarTodd Johnson2011-10-10
我大概懂了. 我的想法如下:有沒有X11,可以判斷 $DISPLAY
Eartha avatarEartha2011-10-12
你主要的問題應該是本來是在非 tty 下,後來又被tty的tmux
Xanthe avatarXanthe2011-10-13
attach(或相反情況,這裏我假設若前後條件一樣是沒問題)
Vanessa avatarVanessa2011-10-13
這裏產生的問題是新的tmux環境變數無法被舊的shell繼承
Mason avatarMason2011-10-16
#1EUR5Ips 有版友提到IPC(行程間通訊)做法,我認為還有個問
題,就是shell不知道哪個新的tmux才是新主人. 這點我倒是有
Donna avatarDonna2011-10-18
個第一時間想法,還沒去試,方法如下:把版友的方法的檔名
的差異部分,改用 tmux 的 pid,然後 shell 去 /proc 目錄
Cara avatarCara2011-10-19
搜尋某個 pid 的 fd 目錄下,有開啟相同的 pts 的就是自己
的新主人. 表達得有點亂,以上希望你能看懂,也希望能有幫助
Zenobia avatarZenobia2011-10-20
突然想到: 反過來看$DISPLAY 是不是在xserver下?
Isla avatarIsla2011-10-24
我不知道screen/tmux的差別 不過用$STY來判斷應該不會重覆
裡面也是有pid的資訊