Use the shell to determine whether a user exists (the user name is passed by parameter)

Source: Internet
Author: User
Tags exit

Write a script:

1. Use function to realize:

To determine whether a user exists, the user name passed through the parameters;

If present, the shell and UID of this user are displayed

If it does not exist, it is said that the user does not exist;

2, prompts the user to enter the user name, then passes it to the above function;

3, not exit after the judgment, but prompts the user to continue to enter other user name, or input (quit) exit;

No nonsense, the code is as follows:

#!/bin/bash  
#Author: MOS  
#Script name:funUinfo.sh  
#Date & time:2012-10-09/21:06:02  
#Version: 1.0.1  
#Description:  
#  
#定义函数  
Find_u () {  
#判断输入值是否为空, if null, function ends, return value 1 [-  
Z $] && Return 1  
#判断用户是否存在, exists to display the request, does not exist, the function ends, returns a value of 1  
if ID &>/dev/null then 
    echo "" Is ' id-u ' 
 echo "Shell is ' grep" ^$1: "/etc/passwd \
    |cut-d ': '-f7 '"  
else return
    1  
fi  
}  
#循环执行 to  Meet the topic requirements while  
:;d o
read-p "Please input A username[quit to exit]:" User 
    if [$User = Quit];then 
        exit 0  
    Else
        find_u $User 
        res=$?  
        [$Res-eq 1] && echo "No such $User."
    Fi done  
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.