In: Computer Science
Write a script that checks each minute and reports on who logs in and who logs out.
You can follow the steps below (not the only way):
1). ANSWER :
GIVENTHAT :
Write a script that checks each minute and reports on who logs in and who logs out.
Below is algo used for bash script :-
steps :
0. Infinite while loop :
1. save current "who"output into a file "A"
2. wait for configured scconds(10)
3. move file "A" to "B"(new file)
4. saw current "who" output into file "A"(overwrite)
5. detect change "diff" of files and output to console in formatted
6. ctrl-c quit
code :-
fileOne ="/tmp/a.md"
fileTwo="/tmp/b.md"
clear
while true;do
echo"\n\tWaiting..\n"
for i in $(seq 1 10);
do
# echo $i
sleep 1
done
echo "Diff..\n"
mv $fileOne $fileTwo
who >$fileOne
echo "User(s) LoggedIn"
diff $fileOne $fileTwo | grep "<" | cut-d ' ' -f
1,2,3
echo "\nUser(s) LoggedOut"
diff $fileOne $fileTwo | grep ">" | cut-d ' ' -f
1,2,3
sleep 1
who > $fileOne
done
Sample Output :-
Waiting..
Diff..
User(s) LoggedIn
<b0users01ttys010
<b0user02 ttys011
User(s) LoggedOut
>b93033 organ90