zoukankan      html  css  js  c++  java
  • (七)函数

    7.1 系统函数

    1.basename基本语法

    basename [string / pathname] [suffix]   (功能描述:basename命令会删掉所有的前缀包括最后一个(‘/’)字符,然后将字符串显示出来。

    选项:

    suffix为后缀,如果suffix被指定了,basename会将pathnamestring中的suffix去掉。

    2.案例实操

    1)截取该/home/atguigu/banzhang.txt路径的文件名称

    [atguigu@hadoop101 datas]$ basename /home/atguigu/banzhang.txt

    banzhang.txt

    [atguigu@hadoop101 datas]$ basename /home/atguigu/banzhang.txt .txt

    banzhang

    3. dirname基本语法

    dirname 文件绝对路径 (功能描述:从给定的包含绝对路径的文件名中去除文件名(非目录的部分),然后返回剩下的路径(目录的部分))

    4.案例实操

    1)获取banzhang.txt文件的路径

    [atguigu@hadoop101 ~]$ dirname /home/atguigu/banzhang.txt

    /home/atguigu

    7.2 自定义函数

    1.基本语法

    [ function ] funname[()]

    {

    Action;

    [return int;]

    }

    funname

    2.经验技巧

    1)必须在调用函数地方之前,先声明函数,shell脚本是逐行运行。不会像其它语言一样先编译。

    2)函数返回值,只能通过$?系统变量获得,可以显示加:return返回,如果不加,将以最后一条命令运行结果,作为返回值。return后跟数值n(0-255)

    3.案例实操

    1)计算两个输入参数的和

    [atguigu@hadoop101 datas]$ touch fun.sh

    [atguigu@hadoop101 datas]$ vim fun.sh

     

    #!/bin/bash

    function sum()

    {

        s=0

        s=$[ $1 + $2 ]

        echo "$s"

    }

     

    read -p "Please input the number1: " n1;

    read -p "Please input the number2: " n2;

    sum $n1 $n2;

     

    [atguigu@hadoop101 datas]$ chmod 777 fun.sh

    [atguigu@hadoop101 datas]$ ./fun.sh

    Please input the number1: 2

    Please input the number2: 5

    7

  • 相关阅读:
    [UE4]Spline
    [UE4]Spline Mesh Actor
    SpringBoot的jar包引用外部properties文件
    如何将.crt文件转换为.cer格式
    消息队列之 RabbitMQ
    The last packet successfully received from the server was 8 milliseconds ago.
    DEBUG、INFO、WARN、ERROR、CRITICAL开发日志级别定义
    灰度发布-流程篇
    k8s+istio:流量控制之灰度发布
    微服务、SpringCloud、k8s、Istio杂谈
  • 原文地址:https://www.cnblogs.com/hg-super-man/p/10514019.html
Copyright © 2011-2022 走看看