First, the function
1, Function: Functions, functions
2. function: To encapsulate a particular function or code that should be executed independently into a separate function and take a name, call it when used;
3. function Features: Structured programming, can not be run independently, need to be executed when the call, may be called multiple times;
Second, function programming and application
1, define a function, method one:
function FUNCNAME {
Command (commands that are executed independently)
}
2. Define a function, method two:
FUNCNAME () {
Command
}
3. function Custom Execution status return value: Return #
#表示: 0-255
4. functions that accept parameters: ./shellname.sh m N
Parameter $1:m
Parameter $2:n
such as 1:
#!/bin/bash
#
ShowMenu () {
Cat << EOF
d| D) show disk usages.
m| M) show menory usages.
s| S) show swap usages.
q| Q) quit.
Eof
}
ShowMenu
Read-p "Your is choice:" Choice
until [$CHOICE = = ' Q '-o $CHOICE = = ' Q '];d o
Case $CHOICE in
d| D
DF-HP;;
m| M
free-m | grep "^mem";;
s| S
free-m | grep "^swap";;
*)
Read-p "Your is choice again:" Choice;;
Esac
ShowMenu
Read-p "Your is choice:" Choice
Done
such as 2:
#!/bin/bash
#
ADDUSER () {
Username=user20
if! Id-u $USERNAME &>/dev/null;then
Useradd $USERNAME
echo $USERADD | passwd--stdin $USERADD &>/dev/null
return 0
Else
Return 1
Fi
}
ADDUSER
If [$? = = 0];then
echo "user add finished."
Else
echo "User add Failuer."
Fi
such as 3:
#!/bin/bash
#
Twosum () {
Echo $[$1+$2]
}
For I in {1..10};d o
Let j=$[$I +1]
echo "$I plus $J ' twosum $I $J '"----> Pass 2 parameters
Done
Practice, write a script:
Determine which hosts are online between 192.168.0.200-192.168.0.254. Requirements:
1, the use of functions to achieve a host of the decision process;
2. Call this function in the main program to determine the online status of all hosts within the specified range.
Method One:
#!/bin/bash
#
PING () {
For I in {1..10};d o
If Ping-c 1-w 1 192.8.8. $I &>/dev/null;then
echo "192.8.8. $I is up."
Else
echo "192.8.8. $I is down."
Fi
Done
}
Ping
Method Two:
#!/bin/bash
#
PING () {
If Ping-c 1-w 1 $ &>/dev/null;then
echo "is up."
Else
echo "is down."
Fi
}
For I in {1..10};d o
PING 192.8.8. $I
Done
Method Three:
#!/bin/bash
#
PING () {
If Ping-c 1-w 1 $ &>/dev/null;then
return 0
Else
Return 3
Fi
}
For I in {1..10};d o
PING 192.8.8. $I
If [$? = = 0];then
echo "192.8.8. $I is up."
Else
echo "192.8.8. $I is down."
Fi
Done
Write a script: Use the function to complete
1, the function can accept a parameter, the parameter is the user name;
Determine if a user exists
Returns the shell and UID of this user if it exists, and returns the normal status value;
If it does not exist, it says that the user does not exist and returns an error status value;
2, the function is called in the main program;
Extension 1: In the main program, let the user enter the user name, passed to the function to judge;
Extension 2: In the main program, after entering the user name judgment does not exit the script, but prompts the user to continue to enter the next user name;
If the user entered the user does not exist, please re-enter the user, but if the user entered Q or Q to exit;
Reference:
#!/bin/bash
#
USER () {
If id-u $ &>/dev/null;then
echo "is UID and SHELL: ' grep" ^$1 "/etc/passwd | cut-d:-f3,7 ' "
return 0
Else
echo "Not user $"
Return 5
Fi
}
Read-p "Your input username:" Username
until [$USERNAME = = Q-o $USERNAME = = Q];d o
USER $USERNAME
If [$? = = 0];then
Read-p "Your input username:" Username
Else
Read-p "Your input username again:" username
Fi
Done
This article is from "Nick Liu's blog," Please make sure to keep this source http://zkhylt.blog.51cto.com/3638719/1411508