Skip to main content
Solved

Command to check if the user is logged in?

  • November 2, 2016
  • 4 replies
  • 32 views

chrisdaggett
Forum|alt.badge.img+7

I am making a simple bash script. I want to use an IF statement that checks to make sure the user is logged in before it runs (else Exit).

Anyone know what command line to use to make sure the computer is not at the login window, or that a user is logged in?

Thanks in advance.

Best answer by maxbehr

Agree, I'm sure there is probably a more elegant way but I just use

who | grep "console"

if that returns anything then you know a user is logged in.

4 replies

Forum|alt.badge.img+16
  • Valued Contributor
  • November 2, 2016

You can gather that information from who. There might be a better way to do it, though.


Forum|alt.badge.img+5
  • New Contributor
  • Answer
  • November 2, 2016

Agree, I'm sure there is probably a more elegant way but I just use

who | grep "console"

if that returns anything then you know a user is logged in.


iJake
Forum|alt.badge.img+23
  • Contributor
  • November 2, 2016
currentUser=$(stat -f %Su "/dev/console")

if [[ "$currentUser" == "root" ]]; then
exit
fi

chrisdaggett
Forum|alt.badge.img+7
  • Author
  • Valued Contributor
  • November 2, 2016

Thanks all :).