shell编程之函数

与其他编程语言一样,bash 也可以定义函数。一个函数就是一个子程序,用于完成特定的任务,当有重复代码时,或者一个任务只需要少数的修改就能被重复执行时。

函数一般的格式:

function  func_name {

commands

}

或者

func_name () {

commands

}

#函数基本调用 
1 #! /bin/bash
  2 
  3 func1(){
  4         echo "the parameter's count: $#"
  5         echo "the first parameter: $1"
  6         echo "the second parameter: $2"
  7 }
  8 
  9 func1  a  b      

 

下面再介绍一下,调用其他文件中的函数的方法:

例如tool文件中定义函数test

  1 test(){
  2 echo "hello world!!!"
  3 }

在另一个文件中调用

  1 #! /bin/bash
  2 
  3 . tool   #点号相当于C语言中的#include
  4 
  5 test

 

posted on 2019-05-06 16:52  wsw_seu  阅读(130)  评论(0编辑  收藏  举报

导航