Defining functions
Function name {
Functional body of a function
......
}
Function name () {
Functional body of a function
......
}
How to call a function
Name of function
What are the rules for using functions?
1) first defined and then called, you can only define not calling the
2) define place above, call down face
3) Function names are case-sensitive
4) function name is the same, after the definition of the effective
5) Function name is not allowed to use special symbols and key words
6) variables defined in the function body are called local variables and can only be used within the current function
7) variables defined outside the function body are called global variables
8) hit the function name of the first function in the second function, so that the variable of the first function can be called
Cases
#!/bin/bash
PRI () {
echo "Hello"
}
Pri
function Passing Value
PRI () {
Echo $#
Echo $*
echo $
Echo
}
PRI 20 40
#!/bin/bash
Sum_two () {
sum= ' Expr 2 +3 '
echo "1 + 2 = $sum"
}
Sum_two #只能计算2 +3 and
function recursive invocation
Fork the bomb. (){.|. &};.
Analysis
1. ()
2 {
3. |. &
4}
9 ·
6.
Array:
Use a variable to store a set of data and be able to manipulate a single piece of data in that set of data
Array type: one-dimensional array two-dimensional array multidimensional array
*
DECLARE specifies the type of the variable when the variable is defined, the type of the default variable is the character type
Format 1
DECLARE option variable name
Variable name = value
Format 2
DECLARE option variable name = value
Options
- I integer variable
- x environment variable
- R read-only variable, defined variable name not allowed to modify
- A array variable
Example 1:
Declare-i a=10 b=20
Declare-i c=a*b
Echo C
Example 2:
Declare-r Name=wsyht
Name=peter
Example 3:
Declare-a Array #定义数组
declare-a | grep array#查看数组
Array= (Jim Tom Jerry Lucy) #定义数组元素
echo ${array[1]}#输出数组元素
Array[4]=yht#如果元素下标值没有被已有元素使用, is to add a new element to the array
Array[1]=tom#如果元素下标值已被已有元素使用, is the value of modifying an existing element
array= ()#如果给数值赋值时, the element list is empty, is to delete all elements in the array
Array= (Baidu 163)#如果给数组赋值时, the element list entered a new value, is to re-assign the array [0][1][2]
Display definition array declare-a array name
How to assign a value to an array variable:
Method 1
Array name = (List of array elements)
Array= (Jim Tom Jerry Lucy) #定义数组元素
Method 2
Array name [element subscript]= Value
Array[4]=yht
Using array elements
${array name [element subscript]}
Echo ${array[1]}
Manipulating array element-related commands
All elements in the output array
Echo ${host[@]}
Echo ${host[*]}
Number of output array elements
echo ${#host [@]}
echo ${#host [*]}
Outputs a number of successive array elements
Num= (a b c d e F g h i j K)
Echo ${num[@]:3} #从下标是3的元素开始显示, not later, indicates default display to last
Echo ${num[@]:3:5} #从下标是3的开始显示 showing 5
echo ${num[@]:3:} #从第三个开始显示, showing 0
echo ${#host [0]} #输出元素下标为0的元素个数
Output part of a value
echo ${host[0]} #输出某个元素的值
Www.baidu.com
Echo ${host[0]:3} #从数组元素值的和四个字符开始显示, displayed to the end,
. baidu.com
Echo ${host[0]:0} #从第一个字符开始显示, always displayed to the last
Www.baidu.com
Echo ${host[0]:0:3} #从第一个字符开始显示 with a total of 3 characters
. baid
Output array elements
Outputs the length of an array element
-echo ${array name [subscript]}
Part of the output array element value
-Echo $ (#数组名 [subscript]: Start: Number of characters)
Example
Server= (www.baidu.com www.163.com)
echo $ (#server [@])
echo $ (#server [1])
Wall "error" sends a warning message to all terminals, with a warning message in double quotes
Shell variable substitution (changing the way variables are assigned)
$ (Variable name variable substitution user custom string)
:-
Returns the value if name exists and is not null;
Purpose: Returns the default value if the variable is not defined
Cases:
#name =tom
#echo $name
Tom
#y =$ (NAME:-PLJ)
#echo $y
Tom
#unset Name
#y =${NAME:-PLJ}
$echo $y
Plj
:=
$ (Name:=word)
Returns a value if name exists and is not NULL, otherwise set to Word and return its value
Purpose: Assign a value to a variable if it is not defined
Cases:
#x =$ (name:=100)
#echo $x
100
#echo $name
100
#name =200
#x =${name:=100}
#echo $x
200
:+
Returns word if name exists and is not NULL, otherwise returns null
Purpose: Test whether the variable is defined
#name =jim
#echo $name
#a =${name:=is OK}
#echo $a
is OK
#unset Name
#a =${name:=is OK}
#echo $a
:?
$ (Name:?word)
Returns a value if name exists and is not NULL, otherwise displays Name:word and exits the current command or script
Show Name:parameter null or not set if Word is omitted
Purpose: Captures errors caused by undefined variables
#name =PLJ
#echo $ (name:?undefined)
Pkj
#unset Name
#echo $ (name:?undefined)
bash:name:undefined
#echo $ (name:?)
Bash:name:parameter null or not set
number=$ (n:=100)
:-
:+
:?
Get part of the value of a normal variable data
${variable name substitution symbol replaced by source data expression}
# left to right shortest match Delete, return the data left after deletion
Cases:
#echo MAIL
/var/spool/mail/root
#mailidir =${mail#*/}
#echo $maildir//Shortest match, match to first/
Var/spool/mail/root
# # Left-to-right longest match delete, return the data left after deletion
Cases:
#echo $MAIL
/var/spool/mail/root
#echo ${mail##*/}//longest match, match to last/
Root
#maildir =${mail##*/}
Echo $maildir
Root
% right-to-left shortest match Delete, return data left after deletion
Cases:
#echo $MAIL
/var/spool/mail/root
#maildir =$ (mail%r*)
#echo $maildir
/var/spool/mail/
Percent is removed from right-to-left longest match, returning the data left after deletion
Cases:
#echo $MAIL
/var/spool/mail/root
#maildir =${mail%%/*}
Echo $maildir
This article is from the "Wsyht blog" blog, make sure to keep this source http://wsyht2015.blog.51cto.com/9014030/1786327
Shell function, shell array, variable substitution