1

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.

3
  • Provide a copy of users.txt from the case where wc -l is wrong. Though you should really be using mktemp to ensure that each copy of this script has its own, unique copy of the temporary file, if you're going to use one. Commented Feb 27, 2017 at 16:56
  • If you want unique users you should use sort -u. Also grep -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$".
    – vdavid
    Commented Feb 27, 2017 at 17:04
  • @vdavid Thanks for the suggestion! I'll keep that in mind.
    – Erik
    Commented Feb 28, 2017 at 2:52

2 Answers 2

1

Assuming BOC's suggestion of a second process interfering is correct, you are contributing to the problem by using an unnecessary file (which provides a shared resource that allows the race condition to occur).

All you need is a single pipeline. (Capturing the output of the pipeline in order to call echo is also unnecessary.)

while true; do
    who | cut -d' ' -f1 | sort | grep -v "erik" | wc -l
done

You can simply it further by using a single call to awk (the sort doesn't seem to serv any purpose; the count is the same no matter what order the output is in):

while true; do
    who | awk '$1 != "erik" {count+=1} END {print count}'
done
2
  • sort allows to get unique users. I guess Erik forgot to pipe it to uniq or to call sort -u. Also awkis overkill, grep -c -v erik gives the same result and is probably more efficient.
    – vdavid
    Commented Feb 27, 2017 at 17:02
  • Thank you so much for your help!
    – Erik
    Commented Feb 28, 2017 at 15:54
1

you have a second instance of your code that is running at the same time. Check the running processes and kill the previous one.

The command

who | cut -d' ' -f1 | sort | grep -v "erik" > "/home/erik/logintester/users.txt"

is not atomic. It first create an empty file users.txt, then create all the processes for each commands of the pipe, then these processes write into the file. This gives a lapse of time where the file is empty. When you have two processes running the same code, there is a probability that one process displays the content of the empty file created by the other process.

Avoiding temporary file like suggested by @chepner is always a good idea.

2
  • It worked! Why does this happen? Will you be so kind as to edit your question why? Thank you so much for your help!
    – Erik
    Commented Feb 28, 2017 at 2:54
  • I would really like to accept both of your answers!
    – Erik
    Commented Feb 28, 2017 at 15:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.