Bash 脚本 - 检查用户是否登录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43736041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 16:06:55  来源:igfitidea点击:

Bash Script - Check if user is logged in or not

linuxbashshellubuntu

提问by LinuxN00b

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.

我正在尝试编写一个脚本来检查用户(作为参数发送)是否已登录。如果用户已登录,我希望答案将 0 返回到 shell,如果用户未登录,则将 1 返回到 shell。

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 }' | sort | uniq)
  do
    if [ "$u" = "" ]; 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 [[  -eq 0 ]] ; then
  echo 'You need to enter a user'
  read u
else
  user=
fi

采纳答案by mf_starboi_8041

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 }' | sort | uniq)                        
        do                                                                      
            if [ "$u" == "" ]; 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=                                                                 
        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

回答by Ville Oikarinen

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

if [[ $1 -eq 0 ]]检查是在函数体外部。这就是为什么它无权访问$1函数调用的参数。此外,您甚至没有在任何地方调用该函数。

Try this:

尝试这个:

#!/bin/bash

set -eu

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

USER=

who | awk '{print }' | 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

更新:退出以获得正确的退出代码