Open In App

Shell Script to Find How Many Terminals Has User Logged-In

Here we are going to see Find How Many Terminals Has User Logged In. Using who command to fetch the user list and then using grep command we can find the number of Terminal that the user has logged in the Linux. We can use LOGNAME or UID to identify the user. UID is a unique User ID assigned to every user that logged in the system, it is an integer value. The LOGNAME is the unique username of the user it can be alphanumeric.

We can use the following command to know the username of the current and its user ID:



For Username/LOGNAME

echo $LOGNAME

Output:



Displaying username

For UID(User ID):

id -u

Here, -u represents that we are interested in user ID.

Output:

Displaying user ID

Approach :

Below is the implementation:

#! /bin/bash

# Taking input from user
echo "Enter LOGNAME OR UID"
read input

# checking if input is a UID or LOGNAME
if [[ $input ]] && [ $input -eq $input 2>/dev/null ]
  
  # If input is UID
  then
    echo "Number of terminals are "
    cat /etc/passwd | grep $input -c 
  
  # If input is LOGNAME
  else
        cat /etc/passwd>userlist
        echo "Number of terminals are "
        grep -c $input userlist
fi

Output:

Using LOGNAME

Using UID

Article Tags :