I'm working on a user menu so that when a user logs in all they get is this menu; on this menu is a selection of reboot option. I want one of the options to be reboot the system if no one is logged in. I want a search to be carried out to see if anyone has logged in. I think this would be a good start:# who | wc if the answer were 0 users I'd want the system to reboot. NOTE: I know it won't be because the user is logged in using the menu. How can I find out if there's no other user logged in besides the one accessing the menu?
I want to reboot the system once a search had been done to see that no-one is logged in
reboot;wc;who
This seems like what you're looking for. The issue as I see it is that there's always going to be at least 1 user that's logged in, i.e. the one accessing the menu, so you're looking for when the number of user's is 1. $ who | awk '{print $1}' | sort -u | wc -l1This takes the output of who and cuts just the first column off, i.e. the usernames, and then sorts it uniquely, and then counts it.But IMO, it's a bit more complicated than that. Looking at the situation in this manner is ignoring if this user has any logged in SSH sessions, for example. So you probably want to check to see if the above output from who specifies that it's only :0 devices or if there are any pts/# types of lines. Examples# represents the login for the user's desktop on display :0saml     :0           2014-12-01 01:56 (:0)# represents user's terminals or SSH connectionssaml     pts/0        2014-12-01 01:57 (:0)NOTE: Notice how the pts/0 line shows which display it's associated with, :0.So something like this seems more appropriate:num_pts=$(who | grep 'pts/' | wc -l)num_usr=$(who | awk '{print $1}' | sort -u | wc -l)if [ $num_pts -eq 0 ] && [ $num_usr -eq 1 ]; then echo OK to logout;fiExampleHere's a simulated run where I have 2 accounts logged in:$ whosaml     :0           2014-12-01 01:56 (:0)saml     pts/0        2014-12-01 01:57 (:0)saml     pts/1        2014-12-01 02:13 (:0)saml     pts/2        2014-12-03 04:22 (:0)saml     pts/3        2014-12-05 08:14 (:0)samtest  pts/4        2014-12-05 08:14 (localhost)Running the commands manually produces this:$ echo $num_usr2$ echo $num_pts5$ if [ $num_pts -eq 0 ] && [ $num_usr -eq 1 ]; then echo OK to logout;fi$If I manually set the 2 variables, however, to simulate that I'm logged into :0 and have no terminals open:$ num_usr=1$ num_pts=0$ if [ $num_pts -eq 0 ] && [ $num_usr -eq 1 ]; then echo OK to logout;fiOK to logoutThis can be condensed and is just to show you an approach.