how to cut substring from linux command “who”

2019-09-08 05:04发布

command who returns list of users logged to server

[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)

the point is to assign to variable, value of username($1) where terminal($2) is result from command

ps aux | grep screen

标签: linux bash
2条回答
太酷不给撩
2楼-- · 2019-09-08 05:46

question asked question answered:

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;

:-)

查看更多
乱世女痞
3楼-- · 2019-09-08 06:00

As per your comment I expand my solution. You want to say:

ptw=$(ps aux | awk '/screen/ {print $7}')
while IFS=read -r user res _;
do 
    [ "$res" = "$PTS" ] && echo "logged as $user"
done < <(who)
查看更多
登录 后发表回答