Shell编程—创建函数
1 基本的脚本函数 函数是一个脚本代码块,你可以为其命名并在代码中任何位置重用。要在脚本中使用该代码块时,只要使用所起的函数名就行了。 1.1 创建函数 有两种格式可以用来在bash shell脚本中创建函数。第一种格式采用关键字function,后跟分配给该代码块的函数名。 function name { commands } 二种格式更接近于其他编程语言中定义函数的方式。 name() { commands } 1.2 使用函数 $ cat test1 #!/bin/bash # using a function in a script function func1 { echo "This is an example of a function" } count=1 while [ $count -le 5 ] do func1 count=$[ $count + 1 ] done echo "This is the end of the loop" func1 echo "Now this is the end of the script" $ ./test1 This is an example of a function This is an example of a function This is an example of a function This is