Shell--函数

    xiaoxiao2021-12-14  21

    1、定义格式: function  functionName()      #关键字function可以省略 {     list of commands     [return value] }   函数返回值:                          #函数的返回值只能是整数 可以显式增加return语句;如果不加,会将最后一条命令运行结果作为返回值。 eg: #!/bin/bash funWithReturn(){ echo -n "Input first number: " read aNum echo -n "Input another number: " read anotherNum echo "The two numbers are $aNum and $anotherNum !" return $(($aNum+$anotherNum)) } funWithReturn      #调用函数funWithReturn ret=$?          #读取返回值 echo "The sum of two numbers is $ret !"   2、带参函数 特殊变量: $#传递给函数的参数个数。$*显示所有传递给函数的参数。$@与$*相同,但是略有区别$?函数的返回值。 eg: #!/bin/bash funWithParam(){ echo "The value of the first parameter is $1 !" echo "The value of the second parameter is $2 !" echo "The value of the tenth parameter is $10 !" 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 #参数之间以空格区分 3、删除函数 $unset  .f  functionName  4、文件包含 在一个脚本文件中调用另一个脚本文件(被调用的shell脚本可以没有执行权限) .空格fileName eg:    . /home/text.sh 或 source空格fileName eg:   source /home/text.sh 这样就可以调用另一个脚本中的函数或者变量了
    转载请注明原文地址: https://ju.6miu.com/read-968163.html

    最新回复(0)