Scripting of Bash functions under Linux (11)

Source: Internet
Author: User

Scripting of Bash functions under Linux (11)

1. Function of script programming:

Function: Structured programming, cannot run independently, needs to be executed when called, can be invoked multiple times

Format 1:

Function name {

Statement

...

}

Format 2:

Function name () {

Statement

...

}

2. Call function Execution status return value:

Format: return value, value between 0-255


3. Use the function to complete the script to determine whether the user exists

3.1. The function can accept a parameter, the parameter is the user name;

Returns the normal state value if the user is present;

Returns the error status value if the user does not exist;

3.2. Call the function in the main program, and if the user exists, displays the user's shell and UID

If the user does not exist, the display user does not exist the prompt message

If the user enters Q or q, exit;

Note: Continue prompting for user account regardless of user presence

#!/bin/bash

Userexist () {

If id-u $ &>/dev/null; Then

return 0

Else

Return 1

Fi

}

Read-p "Please input User:" User

until [$USER = = "Q"-o $USER = = "Q"]; Do

Userexist $USER Calling functions

If [$?-eq 0]; Then

cat/etc/passwd | grep "^ $USER" | Awk-f: ' {print $3$7} '

Else

echo "$USER is not exist ..."

Fi

Read-p "Please input User:" User

Done

~


This article is from the "Xavier Willow" blog, please be sure to keep this source http://willow.blog.51cto.com/6574604/1767596

Scripting of Bash functions under Linux (11)

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.