繁体   English   中英

如何grep文件然后将其打印出来? Unix的

[英]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

我将如何打印出已注销然后“已注销”的用户名,例如。

“约翰史密斯已注销”

对于Unix来说还很陌生,任何帮助或建议都很好,在此先感谢:)x

您的问题是您使用引号而不是反引号。 由于将变量设置为等于命令,因此需要使用``或$():

#!/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

这适用于我的系统。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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