0

Okay, so i have strange problem with the following piece of code:

who > tmp
cat tmp | awk '{print $1}' | sort | uniq > tmp
ps aux | grep -Fvf tmp

It is supposed to list processes of all users not logged in at the moment. Problem is it sometimes works, and sometimes doesn't and I have no idea what causes it. I can enter exactly same commands and I get 2 different results. I've narrowed the problem to 2nd line > tmp redirect, where it saves proper user list or wipes the file completely and I have no idea why it happens.

PS. I know it may not be proper solution for the task, but it's what I came up with during limited time I was given.

matszwecja
  • 6,357
  • 2
  • 10
  • 17

1 Answers1

1

It's probably a timing issue: you're reading from and truncating the file in the same pipeline.

The simple solution is to not use temp files at all:

ps aux | grep -Fvf <(who | awk '{print $1}' | sort -u)
glenn jackman
  • 238,783
  • 38
  • 220
  • 352