I have some code like this:
while true
do
who | cut -d' ' -f1 | sort | grep -v "erik" > "/home/erik/logintester/users.txt"
lines=$(< "/home/erik/logintester/users.txt" wc -l)
echo "$lines"
done
It's supposed to echo a "1" whenever there is another person logged in, but instead I get 1's and 0's mixed in. Is there a fix to this problem? Any suggestions are highly appreciated.
users.txt
from the case wherewc -l
is wrong. Though you should really be usingmktemp
to ensure that each copy of this script has its own, unique copy of the temporary file, if you're going to use one.sort -u
. Alsogrep -v "erik"
is a bad regex because it will filter out other users that include "erik", like "erika", "erikssen", etc. you may want to grep with something like "^erik$".