Shell函数

系统函数:

basename 获取文件名

basename /root/test1.sh => test1.sh

basename /root/test1.sh sh => test1

dirname 获取目录前缀

dirname /root/test1.sh => /root

自定义函数:

//定义

funname_()

{

    命令

}

//调用

funname 传递参数1 传递参数2

 

//无参有返回值

#!/bin/bash

sum()

{

    echo "求两个数的和"

    read -p "请输入第一个数字" n1

    read -p "请输入第二个数字" n2

    return $(($n1+$n2))

}

sum

echo "两个数的和为:$?"

//有参函数

#!/bin/bash

funParm()

{

    echo "第一个参数为:$1"

    echo "第二个参数为:$2"

    echo "第十个参数为:${10}"

    echo "参数的总数有:$#"

    echo "获取的所有参数作为一个字符串返回:$*"

}

funParm 1 2 3 4 5 6 7 6 8 9 2 3

posted @ 2021-04-25 22:24  zfxd  阅读(38)  评论(0)    收藏  举报