如何从linux命令“谁”切割子串

时间:2015-07-01 07:58:43

标签: linux bash

命令 who 返回登录到服务器的用户列表

[admin@DB01ATK ~]$ who
adm_drodmann pts/3        2015-07-01 08:57 (10.129.12.77)
adm_ssmith   pts/4        2015-07-01 02:11 (10.129.12.76)
adm_kholdman pts/2        2015-06-30 23:08 (10.129.12.45)

重点是分配给变量,用户名($ 1)的值,其中终端($ 2)是命令的结果

ps aux | grep screen

2 个答案:

答案 0 :(得分:0)

问题问题回答:

PTS=$(awk '{print $7}' <<< $(ps aux | grep screen) )
who | while  read CMD;
   do 
     res=$(awk '{print $2}' <<< "$CMD")
     if [ "$res" = "$PTS" ]
     then 
       echo "logged as $(awk '{print $1}' <<< "$CMD")"
     fi  
   done;

: - )

答案 1 :(得分:0)

根据your comment,我扩展了我的解决方案。你想说:

ptw=$(ps aux | awk '/screen/ {print $7}')
while IFS=read -r user res _;
do 
    [ "$res" = "$PTS" ] && echo "logged as $user"
done < <(who)