简体   繁体   中英

Bash Script - Check if user is logged in or not

I'm trying to write a script that checks if the user (that is sent as an argument) is logged in or not. I want the answer to return 0 to the shell if the user is logged in and 1 to the shell if the user is not logged in.

But I have run into some problem. I get the message "You need to enter a user" everytime i try to run the script even if I send a user as an argument.

#!/bin/bash

function checkUser   
{
  status=0  

  for u in $(who | awk '{print $1}' | sort | uniq)
  do
    if [ "$u" = "$1" ]; then
      status=1
    fi
  done

  if [ "$status" = "1" ]; then
    echo "$user is logged in."
    exit 0
  else
    echo "$user is not logged in."
    exit 1
  fi
}
if [[ $1 -eq 0 ]] ; then
  echo 'You need to enter a user'
  read u
else
  user=$1
fi

The if [[ $1 -eq 0 ]] check is outside the function body. That's why it doesn't have access to the parameter $1 of the function invocation. Also, you are not even calling the function anywhere.

Try this:

#!/bin/bash

set -eu

[ $# == 1 ] || {
    echo "Usage: $0 USERNAME"
    exit 1
}

USER=$1

who | awk '{print $1}' | sort | uniq | grep -q "^$USER\$" && {
    echo "$USER is logged in"
    exit 0
} || {
    echo "$USER is not logged in"
    exit 1
}

UPDATE: exit for correct exit code

You have to provide following to check whether user has provide argument or not.

if [ $# -eq 0 ] ; then

$# used for number of argument provided in command line

Your full code should look like this,

#!/bin/bash                                                                     

function checkUser {                                                            

        status=0                                                                
        for u in $(who | awk '{print $1}' | sort | uniq)                        
        do                                                                      
            if [ "$u" == "$1" ]; then                                           
                    return 0                                                    
            fi                                                                  
        done                                                                    
        return 1                                                                
}                                                                               

if [ $# -eq 0 ] ; then                                                          
        echo 'You need to enter a user'                                         
        read user                                                               
        checkUser $user                                                         
        ret_val=$?                                                              
else                                                                            
        user=$1                                                                 
        checkUser $user                                                         
        ret_val=$?                                                              
fi                                                                              

if [ $ret_val -eq 0 ]; then                                                     
        echo "User Logged In"                                                   
        exit 0                                                                  
else                                                                            
        echo "User Not Logged In"                                               
        exit 1                                                                  
fi
echo "Enter Username: "
read check
_user="$(id -u -n)"
if [ "$check" = "$_user" ]
then
echo "User Logged In"
else
echo "User Not Logged In"
fi

This will do your work perfectly fine!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM