簡體   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