我的任務:
寫一個程序,在一定時間間隔後輸出系統中使用者的信息:誰進入、誰離開。
我嘗試透過比較兩個文件來以不同的方式做到這一點。我不知道我做得怎麼樣。comm
我知道這裡不太好。我嘗試使用diff
,但她到控制台的輸出對我來說是無法理解的,當我嘗試時diff -q
,我只得到這一行Files 1.txt and 2.txt differ
請幫我。
#!/bin/bash
while (true) do
who > 1.txt
sleep 10s
who > 2.txt
(comm -13 1.txt 2.txt) > 3.txt
(comm -23 2.txt 1.txt) > 4.txt
echo IN :
cat 3.txt
echo OUT :
sleep 10s
cat 4.txt
echo [______________________________________________________________]
done
upd:或者更好的是這樣的..idk
#!/bin/bash
while (true) do
who > 1.txt
sleep 10s
who > 2.txt
(comm -13 1.txt 2.txt) > 3.txt
echo IN :
cat 3.txt
echo OUT :
sleep 10s
who > 2.txt
(comm -23 3.txt 2.txt) > 4.txt
cat 4.txt
echo [______________________________________________________________]
done
答案1
#!/bin/bash
who >1.txt
sleep 10
who >2.txt
awk 'NR==FNR{a[$1];next}!($1 in a){print $1}' 1.txt 2.txt >3.txt
awk 'BEGIN{print "Below are the New users logged in now "}{print $0}' 3.txt
echo "=========================="
awk 'NR ==FNR {a[$1];next}!($1 in a){print $1}' 2.txt 1.txt > 4.txt
awk 'BEGIN{print "Below are the user who logged out now"}{print $0}' 4.txt