本文地址

 【夯实shell基础】shell基础面面观

 

      点击关注微信公众号 wenyuqinghuai

 

分享提纲:

  1. shell中的函数

  2. shell中的数组

  3. shell中的变量

  4. shell中的运算符

  5. Linux的一些命令

  6. 几个自定义的脚本(可以添加到命令中)

 

 

 

1. shell中的函数


 

  1.1)【定义shell函数(define function)】

 

    [ function ] funname [()]

    {

    action;

    [return int;]

    }

 

说明:

  • 1、可以带function fun() 定义,也可以直接fun() 定义,不带任何参数。
  • 2、参数返回,可以显示加:return 返回,如果不加,将以最后一条命令运行结果,作为返回值。 return后跟数值n(0-255

实例(testfun1.sh):

 

 1 #!/bin/sh
 2 
 3  fSum 3 2;
 4  function fSum()
 5  {
 6    echo $1,$2;
 7    return $(($1+$2));
 8  }
 9  fSum 5 7;
10  total=$(fSum 3 2);
11  echo $total,$?;
12 
13 sh testfun1.sh
14 testfun1.sh: line 3: fSum: command not found
15 5,7
16 3,2
17 1
18 5
testfun1.sh

相关文章: