如何从命令行获取当前终端的名称?

TuK*_*Ksn 31 command-line

是否有可能通过命令获取终端类型?

如果我正在使用gnome-terminal,输出应该是gnome-terminal或类似的。获得终端的版本也很好。

更新

ps -aux | grep `ps -p $$ -o ppid=` 
Run Code Online (Sandbox Code Playgroud)

将输出如下内容:

user     4239  0.0  0.7 292708 15744 pts/8    Sl   11:39   0:02 xfce4-terminal
user     4800  0.0  0.0   6176   820 pts/0    S+   12:23   0:00 grep --color=auto  4239
Run Code Online (Sandbox Code Playgroud)

这也适用于 xterm,但我如何只获得名称(xfce4-terminal在这种情况下)?

ter*_*don 33

原版

一种方法是获取当前 shell 会话的父进程,并从那里获取终端的名称。

  1. 获取当前 shell 进程的父进程。bash 变量$$是您当前 shell 的 PID,因此我们可以将其作为对ps( -p $$)的查询,并要求它打印父进程的 PID ( -o ppid=,尾随=是为了避免打印列标题):

    $ ps -p $$ -o ppid=
    544
    
    Run Code Online (Sandbox Code Playgroud)

    所以,我的 shell 的父进程的 PID 是544.

  2. 获取与该 PID 关联的进程并打印其命令行

    $ ps -p 544 o args=
    /usr/bin/python /usr/bin/terminator
    
    Run Code Online (Sandbox Code Playgroud)

    上面的输出将取决于您使用的终端仿真器,我使用的是terminator.

  3. 将所有内容组合在一个命令中

    ps -p $(ps -p $$ -o ppid=) o args=
    
    Run Code Online (Sandbox Code Playgroud)
  4. 使用它来获取版本

    $(ps -p $(ps -p $$ -o ppid=) o args=) --version
    terminator 0.97
    
    Run Code Online (Sandbox Code Playgroud)
  5. 添加一个小函数~/.bashrc,返回您正在使用的终端仿真器的名称和版本(这适用于大多数常见的终端仿真器):

    $ ps -p $$ -o ppid=
    544
    
    Run Code Online (Sandbox Code Playgroud)

    您现在可以获取终端的名称并将您喜欢的任何选项传递给它(例如--version.

使用不同终端的一些示例:

  1. xterm

    $ which_term
    XTerm(297)
    
    Run Code Online (Sandbox Code Playgroud)
  2. terminator

    $ which_term 
    terminator 0.97
    
    Run Code Online (Sandbox Code Playgroud)
  3. rxvt, 这个没有-V,-version--version标志,所以不打印版本信息。

    $  which_term
    rxvt  1:2.7.10-5
    
    Run Code Online (Sandbox Code Playgroud)
  4. gnome-terminal.

    $ which_term
    gnome-terminal  3.10.1-1
    
    Run Code Online (Sandbox Code Playgroud)
  5. konsole

    $ which_term
    Qt: 4.8.6
    KDE Development Platform: 4.11.3
    Konsole: 2.11.3
    
    Run Code Online (Sandbox Code Playgroud)
  6. lxterminal

    $ which_term
    lxterminal  0.1.11-4
    
    Run Code Online (Sandbox Code Playgroud)
  7. xfce4-terminal

    $ which_term
    xfce4-terminal 0.6.2 (Xfce 4.10)
    
    Copyright (c) 2003-2012
        The Xfce development team. All rights reserved.
    
    Written by Benedikt Meurer <benny@xfce.org>
    and Nick Schermer <nick@xfce.org>.
    
    Please report bugs to <http://bugzilla.xfce.org/>.
    
    Run Code Online (Sandbox Code Playgroud)

新的和改进的

不过,上述方法并不那么值得信赖。当您在su与另一个用户 ing之后运行您的 shell或当您的终端别名为某物和其他各种情况时,它会窒息。由于我们显然在这里使用 X 程序,因此更好的方法可能是使用类似xdotool(installable with sudo apt-get install xdotool) 来获取信息:

perl -lpe 's/\0/ /g' /proc/$(xdotool getwindowpid $(xdotool getactivewindow))/cmdline
Run Code Online (Sandbox Code Playgroud)

以上将打印用于启动当前活动窗口的命令行。由于您的终端可能会处于活动状态,这就是它将显示的命令。这意味着对于大多数终端模拟器,您可以安全地假设返回的第一个字段是终端名称:

$ which_term 
lxterminal 
Run Code Online (Sandbox Code Playgroud)

这意味着获取版本是微不足道的。例如

$ dpkg -l $(which_term) | awk '/^ii/{print $3}'
0.1.11-4
Run Code Online (Sandbox Code Playgroud)

不是这样gnome-terminal

$ which_term 
/usr/lib/gnome-terminal/gnome-terminal-server 
Run Code Online (Sandbox Code Playgroud)

terminator

$ which_term
/usr/bin/python /usr/bin/terminator 
Run Code Online (Sandbox Code Playgroud)

所以,我们可以让它更复杂一点(这里有一些 bashisms,这个是不可移植的):

which_term(){
    term=$(perl -lpe 's/\0/ /g' \
           /proc/$(xdotool getwindowpid $(xdotool getactivewindow))/cmdline)

    ## Enable extended globbing patterns
    shopt -s extglob
    case $term in
        ## If this terminal is a python or perl program,
        ## then the emulator's name is likely the second 
        ## part of it
        */python*|*/perl*    )
         term=$(basename "$(readlink -f $(echo "$term" | cut -d ' ' -f 2))")
         version=$(dpkg -l "$term" | awk '/^ii/{print $3}')
         ;;
        ## The special case of gnome-terminal
        *gnome-terminal-server* )
          term="gnome-terminal"
        ;;
        ## For other cases, just take the 1st
        ## field of $term
        * )
          term=${term/% */}
        ;;
     esac
     version=$(dpkg -l "$term" | awk '/^ii/{print $3}')
     echo "$term  $version"
}
Run Code Online (Sandbox Code Playgroud)

这适用于我测试过的所有情况。

  • 您可以使用`$PPID` 来获取父进程的PID。 (2认同)

nyu*_*a7h 11

basename "$(cat "/proc/$PPID/comm")"
Run Code Online (Sandbox Code Playgroud)

$PPID是 shell 的父进程的 PID。comm意味着命令。它可能是也可能不是完整路径,因此basename如果需要,我们使用剥离路径。

注意事项

这些可能也至少适用于其他一些答案。

  • comm技术上是argv[0],实际上可以是任意字符串。但总的来说,对于这种特殊情况,您应该能够依赖它。

  • 如果您通过 SSH 连接或使用或类似的东西tmux,这将无法按预期工作screen


Avi*_*Raj 9

尝试这个,

ps -aux | grep `ps -p $$ -o ppid=` | awk 'NR==1{print $11}'
Run Code Online (Sandbox Code Playgroud)

或者

ps -aux | grep `ps -p $$ -o ppid=` | awk 'NR==1{print $NF}'
Run Code Online (Sandbox Code Playgroud)