function 函数

function可以在脚本中起到简化代码,调用方便的用处

  • 语法格式
function fname()  {
                程序段
}

函数设置一定要设置到脚本的最前面,因为程序是从上到下执行的。

  • 例子
#!/bin/bash
function printit(){
              echo "Your choice is $1"  #这个$1是函数内部的内置函数。依赖下面指令的下达
}

echo "This progarm will print your  selection !"
case  $1  in
"one")
        printit  1 #函数后面接参数
        ;;
"two")
       printit  2
        ;;
"three")
      printit   3
        ;;
*)
      echo  "Usage $0 {one|two|three}"
        ;;
esa

输入 上面的选项就会出现对应的输出 如输出

[root@k8s-node2 tmp]# ./show-fction.sh one
This progarm will print your  selection !
Your choice is 1

学习笔记

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容