简体   繁体   中英

How to grep a file then print it out? Unix

while true

do


if($update)
then
    who | awk {'print$1'} > first_user_list #store original user list
    update=false
fi

who | awk {'print$1'} > updated_user_list

(diff first_user_list updated_user_list) | cut -c 3- > in_out_list
inOutVar='cat in_out_list' #<----here's my problem

length_first=$(wc -l < updated_user_list)
length_update=$(wc -l < first_user_list)

if [[  "$length_first" -lt "$length_update" ]]; then
    echo -e "$inOutVar" " has logged out"
    update=true
elif [ "$length_first" -gt "$length_update" ]; then
    echo -e "$inOutVar" " has logged in" 
    update=true
else
    echo No user has logged in/out in the last 3 seconds
fi

sleep 3
done

How would i go by printing out the users names who has logged out then " has logged out" eg.

"johnsmith has logged out"

Pretty new to unix, any help or suggestions would be great, thanks in advance :)x

Your issue is that you used quotes instead of backticks. Since you are setting the variable equal to a command you need to use `` or $():

#!/bin/sh
while true; do


if($update)
then
    who | awk {'print$1'} > first_user_list #store original user list
    update=false
fi

who | awk {'print$1'} > updated_user_list

(diff first_user_list updated_user_list) | cut -c 3- > in_out_list
inOutVar=`cat in_out_list` ## use `` or $(), not ''

length_first=$(wc -l < updated_user_list)
length_update=$(wc -l < first_user_list)

if [[  "$length_first" -lt "$length_update" ]]; then
    echo -e "$inOutVar" " has logged out"
    update=true
elif [ "$length_first" -gt "$length_update" ]; then
    echo -e "$inOutVar" " has logged in" 
    update=true
else
    echo No user has logged in/out in the last 3 seconds
fi

sleep 3

That works on my system.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM