Posted on 11-02-2016 07:51 AM
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.
Solved! Go to Solution.
Posted on 11-02-2016 08:06 AM
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.
Posted on 11-02-2016 08:06 AM
currentUser=$(stat -f %Su "/dev/console")
if [[ "$currentUser" == "root" ]]; then
exit
fi
Posted on 11-02-2016 07:56 AM
You can gather that information from who
. There might be a better way to do it, though.
Posted on 11-02-2016 08:06 AM
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.
Posted on 11-02-2016 08:06 AM
currentUser=$(stat -f %Su "/dev/console")
if [[ "$currentUser" == "root" ]]; then
exit
fi
Posted on 11-02-2016 08:08 AM
Thanks all :).