Linux-shell custom functions

 

(1) With return function

#!/bin/bash
# author:
cainiao tutorial # url:www.runoob.com
funWithReturn(){     echo "This function will add the two numbers entered..."     echo "Enter the first number: "     read aNum     echo" Enter the second number: "     read anotherNum     echo" The two numbers are $aNum and $anotherNum !"     return $(($aNum+$anotherNum)) }







funWithReturn
echo "The sum of the two numbers entered is $? !" #The
output is similar to the following: #This
function will add the two numbers
entered ... #input the first number: 
#1
#input the second Numbers: 
#2
#The two numbers are 1 and 2 respectively!
#The sum of the two numbers entered is 3!


(2) Function with parameters

Get the value of the parameter in the form of $n. For example, $1 means the first parameter, and $2 means the second parameter.
Note that $10 cannot get the tenth parameter, and ${10} is required to get the tenth parameter. When n>=10, you need to use ${n} to get the parameters.
#!/bin/bash
# author:
cainiao tutorial # url:www.runoob.com
funWithParam(){     echo "The first parameter is $1 !"     echo "The second parameter is $2 !"     echo "The tenth parameter is $10 !"     echo "The tenth parameter is ${10} !"     echo "The eleventh parameter is ${11} !"     echo "The total number of parameters is $#!"     echo "outputs all parameters as a string $ * !" }







funWithParam 1 2 3 4 5 6 7 8 9 34 73 #Output
result: #The
first parameter is 1! #The
second parameter is 2! #The
tenth parameter is 10!
#The tenth parameter is 34!
#第Eleven parameters are 73!
#The total number of parameters is 11!
#Output all parameters as a string 1 2 3 4 5 6 7 8 9 34 73!


 

Guess you like

Origin blog.csdn.net/helunqu2017/article/details/113815536