Shell Script to Find How Many Users are Logged In

Now we will create a shell script using some above-mentioned commands to get user details. We are approaching the solution in a way that the user is asked for input by given suggestions. That input will be then used to check against the available cases, and then the matched case will be allowed to run.

Open gedit file:

Open any editor according to your preferences, we have used gedit editor because of its simple user interface and the color combination present.

gedit userAccounts.sh

Code:

Here in the userAccounts.sh we will write our code, and use switch cases to compare the user input. We have used commands like lslogins, who, groups, etc. which will help us to satisfy the user requirements. You could find the use of these commands more extended above. So, let us begin the script.

#!/bin/bash
#here we are going to develop a script for various options on user accounts
echo -e "\n
[ 1 ] for listing all the user accounts name \n
[ 2 ] for counting the number of logged-in user accounts  \n
[ 3 ] for listing the names of currently logged-in users\n
[ 4 ] for checking the groups to which the current user belong \n"

#Now take user input
read userInput

#Now we will use switch cases for various input operations
case $userInput in
    1)
    #syntax lslogins <option[=output field]>
    lslogins -o USER
    ;;
    2)
    #syntax who <option> <user optional>
    #grep used to filter
    who --count | grep users
    ;;
    3)
    #-q option is to count the number of users and print the logged-in users.
    # instead of -q, --count can also be used.
    # -v is used to exclude any pattern
    who -q | grep -v users
    ;;
    4)
    #syntax groups <option> [USERNAME]
    groups
    ;;
    *)
    echo -e "Please Enter Correct Input \n"
    ;;    
esac

Grant executable permissions

Executable permissions must be granted to the files to make them run or execute on the system. We could also use “777” instead of “+x” in the chmod command. Also please run the script as root to 

# chmod +x userAccounts.sh

Run the script

./userAccounts.sh

Example 1: 

./userAccounts.sh
1

Example 2: 

./userAccounts.sh
2

Example 3:

./userAccounts.sh
3

Hence, we were able to find out various login-related results using our shell script.

Shell Scripts to Find How Many Users are Logged In

Every operating system provides a feature of multiple user accounts. Linux-based operating systems have some commands or functionalities to check user accounts’ details and change them. This ability is mainly used by the admin account user that is the root user, to provide permissions and access to different users. The admin can also check how many users are currently logged in, how many are logged out, and the login time. Here in this article, we will explore all these ways and also write a shell script to complete these tasks efficiently.

Table of Content

  • Commands to get user-related information:
  • Shell Script to Find How Many Users are Logged In

Similar Reads

Commands to get user-related information:

1. id Command to Find How Many Users are Logged In...

Shell Script to Find How Many Users are Logged In

Now we will create a shell script using some above-mentioned commands to get user details. We are approaching the solution in a way that the user is asked for input by given suggestions. That input will be then used to check against the available cases, and then the matched case will be allowed to run....

Conclusion

In this article we discussed essential Linux commands used to manage and understand user accounts. These commands, such as ‘id,’ ‘groups,’ ‘getent,’ ‘lslogins,’ ‘users,’ ‘who,’ ‘w,’ ‘last,’ ‘lastb,’ and ‘lastlog,’ help administrators (root users) assign permissions and monitor user activities. The article also introduces a simple shell script that allows users to interactively explore user-related information. By providing practical examples and explanations, this article empowers users to efficiently manage and monitor user accounts on a Linux system....

Contact Us