一种方法是获取当前shell会话的父进程,然后从中获取终端的名称。
获取当前 shell 进程的父进程。Bash 变量 `$$` 是你当前 shell 的 PID,因此我们可以将其作为一个查询参数传递给 `ps`(`-p $$`),并要求它打印父进程的 PID (`-o ppid=`,末尾的 `=` 是为了避免打印列标题):
```sh
$ ps -p $$ -o ppid=
544
```
所以,我的shell的父进程的PID是544。
获取与该PID相关联的进程,并打印其命令行。
```
$ ps -p 544 o args=
/usr/bin/python /usr/bin/terminator
```
上述输出将取决于您使用的终端仿真器,我正在使用terminator。
将所有内容合并为一个命令
ps -p $(ps -p $$ -o ppid=) -o args=
使用该命令获取版本信息
$(ps -p $(ps -p $$ -o ppid=) o args=) --version
terminator 0.97
在~/.bashrc
中添加一个小函数,用于返回您正在使用的终端仿真器的名称和版本(适用于大多数常见终端仿真器):
which_term(){
term=$(ps -p $(ps -p $$ -o ppid=) -o args=);
found=0;
case $term in
*gnome-terminal*)
found=1
echo "gnome-terminal " $(dpkg -l gnome-terminal | awk '/^ii/{print $3}')
;;
*lxterminal*)
found=1
echo "lxterminal " $(dpkg -l lxterminal | awk '/^ii/{print $3}')
;;
rxvt*)
found=1
echo "rxvt " $(dpkg -l rxvt | awk '/^ii/{print $3}')
;;
*)
for v in '-version' '--version' '-V' '-v'
do
$term "$v" &>/dev/null && eval $term $v && found=1 && break
done
;;
esac
[ $found -eq 0 ] && echo "$term " $(dpkg -l $term | awk '/^ii/{print $3}')
}
现在您可以获取终端的名称,并向其传递任何您喜欢的选项(例如
--version
)。
以下是使用不同终端的一些示例:
1.
xterm
$ which_term
XTerm(297)
2.
terminator
$ which_term
terminator 0.97
3.
rxvt
,这个没有-V
,-version
或者--version
标志,所以没有打印版本信息。
$ which_term
rxvt 1:2.7.10-5
4.
gnome-terminal
。
$ which_term
gnome-terminal 3.10.1-1
5.
konsole
$ which_term
Qt: 4.8.6
KDE Development Platform: 4.11.3
Konsole: 2.11.3
6.
lxterminal
$ which_term
lxterminal 0.1.11-4
7.
xfce4-terminal
$ which_term
xfce4-terminal 0.6.2 (Xfce 4.10)
版权所有 (c) 2003-2012
Xfce开发团队。保留所有权利。
由Benedikt Meurer <benny@xfce.org>和Nick Schermer <nick@xfce.org>编写。
请将错误报告发送至<http://bugzilla.xfce.org/>。
新的和改进的
尽管上述方法不太可靠。当您在切换到另一个用户后运行shell或者终端被别名为其他内容时,它会出现问题。由于我们显然是在这里使用X程序,更好的方法可能是使用类似xdotool
(可通过sudo apt-get install xdotool
安装)来获取信息:
perl -lpe 's/\0/ /g' /proc/$(xdotool getwindowpid $(xdotool getactivewindow))/cmdline
以上将打印用于启动当前活动窗口的命令行。由于您的终端应该是活动状态,所以它将显示该命令。这意味着对于大多数终端仿真器,您可以安全地假设返回的第一个字段是终端名称:
$ which_term
lxterminal
这意味着获取版本是微不足道的。例如,
$ dpkg -l $(which_term) | awk '/^ii/{print $3}'
0.1.11-4
对于
gnome-terminal
来说并非如此:
$ which_term
/usr/lib/gnome-terminal/gnome-terminal-server
或者终结者:
$ which_term
/usr/bin/python /usr/bin/terminator
所以,我们可以让它变得更复杂一些(这里有一些bash特性,这个不是可移植的):
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"
}
这对我测试过的所有情况都有效。
窗口31457282没有与之关联的pid
(在Lubuntu上测试过) - TuKsn