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)