Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

print all terminals of a user except current

Tags:

bash

awk

I am trying to print all terminals of a user except the current terminal used for doing this check. To fetch the current terminal I am using tty command , which works fine if used alone. However when coupled with awk , its failing.

Why below command fail?

 -->ls -l /dev/pts/  |grep ${USER} |awk -v current_tty=$(tty) '{n=split(current_tty,a,"/")} $0 !~ a[n] {print $0}'
awk: fatal: cannot open file `tty' for reading (No such file or directory)

However when I replace $(tty) with $(echo /dev/pts/44) then same command worked.

 -->tty
/dev/pts/44

 -->ls -l /dev/pts/  |grep ${USER} |awk -v current_tty=$(echo /dev/pts/44) '{n=split(current_tty,a,"/")} $0 !~ a[n] {print $0}' 
crw--w---- 1 monk tty  136, 15 Feb  5 10:16 15
crw--w---- 1 monk tty  136, 19 Feb  5 10:16 19
crw--w---- 1 monk tty  136, 28 Feb  5 10:16 28
crw--w---- 1 monk tty  136, 35 Feb  5 10:22 35
crw--w---- 1 monk tty  136, 39 Feb  5 10:18 39
crw--w---- 1 monk tty  136, 43 Feb  5 10:18 43
crw------- 1 monk tty  136, 46 Feb  5 10:16 46
crw--w---- 1 monk tty  136, 48 Feb  5 10:16 48
crw--w---- 1 monk tty  136,  8 Feb  5 10:16 8

I ran $(tty) alone, its working

 -->echo $(tty)
/dev/pts/44

Alternate approach tried which resulted in same result:

ps -eaf |awk -v USER=${USER} -v current_terminal=$(tty) '$1==USER && $6 != "?"'
like image 359
monk Avatar asked Dec 14 '25 15:12

monk


1 Answers

find /dev/pts -mindepth 1 -maxdepth 1 \
    -type c \
    -user "$USER" \
  ! -path "$(tty)" \
    -print

That is to say, we're finding:

  • Direct children of /dev/pts
  • ...which are character devices (like all PTYs should be)...
  • ...and which are owned by the current user...
  • ...and which are not the current tty.
like image 78
Charles Duffy Avatar answered Dec 17 '25 09:12

Charles Duffy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!