using sed and pstree to display the type of terminal being used

前端 未结 4 1306
慢半拍i
慢半拍i 2021-01-28 14:02

I\'ve been trying to display the type of terminal being used as the name only. For instance if I was using konsole it would display konsole. So far I\'ve been using this command

4条回答
  •  太阳男子
    2021-01-28 14:23

    You could use

    ps -p "$PPID" -o comm=
    

    Or

    ps -p "$PPID" -o fname=
    

    If your shell does not have PPID variable set you could get it with

    ps -p "$(ps -p "$$" -o ppid= | sed 's|\s\+||')" -o fname=
    

    Another theory is that the parent process of the current shell that doesn't belong to the same tty as the shell could actually be the one that produces the virtual terminal, so we could find it like this as well:

    #!/bin/bash
    
    shopt -s extglob
    
    SHELLTTY=$(exec ps -p "$$" -o tty=)
    P=$$
    
    while read P < <(exec ps -p "$P" -o ppid=) && [[ $P == +([[:digit:]]) ]]; do
        if read T < <(exec ps -p "$P" -o tty=) && [[ $T != "$SHELLTTY" ]]; then
            ps -p "$P" -o comm=
            break
        fi
    done
    

提交回复
热议问题