The Linux shell can be user-defined functions and can be called casually in shell scripts.
The functions in the shell are defined in the following format:
[Function] funname [()]
{
action;
[return int;]
}
Description
1, can be with function fun () definition, you can also directly fun () definition, without any parameters.
2, the parameter returns, can display add: return returns, if not added, will run the result as the last command, as the return value. Return followed by a value of N (0-255
The following example defines a function and makes a call:
#!/bin/bash
Demofun () {
echo "This is your first shell function!"
}
echo "function begin ..."
Hello
echo "function end!"
Output Result:
Function begin ...
This is your first shell function!
Function end!
The following defines a function with a return statement:
#!/bin/bash
Funwithreturn () {
echo "the function is to get the sum of the numbers ..."
echo-n "Input fi RST number: "
read anum
echo-n" Input Another number: "
read Anothernum echo" the "the" and "
num" Bers is $aNum and $anotherNum! "
Return $ (($aNum + $anotherNum)
}
Funwithreturn
echo "The sum of the numbers is $?!"
The output resembles the following:
The function is to get the sum of the numbers ...
Input first number:25
input another number:50 the and the
numbers are!
The sum of numbers is 75!
function return value after calling this function through $? To obtain.
Note: All functions must be defined before they are used. This means that the function must be placed at the beginning of the script until the shell interpreter discovers it for the first time before it can be used. The calling function uses only its name of functions. function Parameters
In the shell, arguments can be passed to a function when it is called. Inside the function body, the value of the parameter is obtained in the form of a $n, for example, $ $ for the first argument, and $ = for the second argument ...
Examples of functions with parameters:
#!/bin/bash Funwithparam () {echo "The value of the first parameter is $!"
echo "The value of the second parameter is $!"
echo "The value of the tenth parameter is $!"
echo "The value of the tenth parameter is ${10}!"
echo "The value of the eleventh parameter is ${11}!"
echo "The amount of the parameters is $#!"
echo "The string of the parameters is $*!"} Funwithparam 1 2 3 4 5 6 7 8 9 34 73
Output Result:
The value of the first parameter is 1!
The value of the second parameter is 2!
The value of the tenth parameter is ten!
The value of the tenth parameter is!
The value of the eleventh parameter is!
The amount of the parameters is!
The string of the parameters is 1 2 3 4 5 6 7 8 9 34 73! "
Note that the $ $ cannot get the tenth parameter, and the tenth parameter requires ${10}. When n>=10, you need to use ${n} to get the parameters.
In addition, there are several special characters for handling parameters:
parameter Handling |
Description |
$# |
The number of arguments passed to the script |
$* |
Displays all parameters passed to the script in a single string |
$$ |
The current process ID number for the script to run |
$! |
ID number of the last process running in the background |
$@ |
With $ #相同, but use quotation marks and return each parameter in quotation marks. |
$- |
Displays the current options used by the shell, the same as the SET command function. |
$? |
Displays the exit status of the last command. 0 means there is no error, and any other value indicates an error. |
The echo command of the Shell is similar to the echo command of PHP, and is used for the output of strings. Command format:
echo string
You can use echo to achieve more complex output format control. 1. Display normal string:
echo "It is a test"
The double quotes here are completely omitted, and the following commands are identical to the previous instance:
Echo It is a test
2. Show escape characters
echo "\" It is a test\ ""
The result will be:
"It is a test"
Similarly, double quotes can also be omitted 3. Show Variables
The read command reads a row from the standard input and assigns the value of each field in the input row to the shell variable
#!/bin/sh
Read name
echo "$name It is a test"
The above code is saved as test.sh,name to receive the standard input variables, the result will be:
[root@www ~]# sh test.sh
ok #标准输入
OK It is a test #输出
4. Show line breaks
echo "It it a test"
Output Result:
ok!
It it a test
5. Show No Line break
#!/bin/sh
echo "It is a test"
Output Result:
Ok! It is a test
6. Show results directed to file
echo "It is a test" > myfile
7. Output strings As-is, without escaping or taking variables (in single quotes)
echo ' $name \ '
Output Result:
$name \ "
8. Show command execution results
Echo ' Date '
The result will show the current date
Thu Jul 10:08:46 CST 2014
The test command in the shell is used to check if a condition is true, and it can be tested in three aspects of numeric, character, and file. Numerical Test
Parameters |
Description |
-eq |
Equals is True |
-ne |
Not equal to True |
-gt |
is greater than true |
-ge |
Greater than or equal to true |
-lt |
is less than true |
-le |
is less than or equal to true |
Example Demo:
num1=100
num2=100
if test $[num1]-eq $[num2]
then
Echo ' The both numbers is equal! '
else
Echo ' The numbers is not equal! '
Fi
Output Result:
The numbers is equal!
String Test
Parameters |
Description |
= |
Equals is True |
!= |
Not Equal is true |
-Z String |
The string length pseudo is True |
-N String |
True if the string length is not pseudo |