linux shell中创建函数

发布时间 2023-10-14 23:20:55作者: 小鲨鱼2018

 

001、

[root@pc1 test]# cat test.sh    ## 函数脚本
#!/bin/bash

function db1                 ## function关键字来定义函数,db1是函数名
{
        read -p "请输入:" value
        return $[$value *2]       ## return返回函数值
}

db1                             ## 函数调用
echo $?

 

 

[root@pc1 test]# bash test.sh      ## 在脚本中运行函数
请输入:45
90

 

002、

[root@pc1 test]# ls
test.sh
[root@pc1 test]# cat test.sh     ## 修改上面的函数脚本
#!/bin/bash

function db1
{
        read -p "请输入:" value
        echo $[$value *2]        ## 将return改为了echo 
}

db1
## echo $?                       ## 函数返回值可以省略该句

 

调用测试:

[root@pc1 test]# ls
test.sh
[root@pc1 test]# cat test.sh      ## 函数脚本
#!/bin/bash

function db1
{
        read -p "请输入:" value
        echo $[$value *2]
}

db1
## echo $?
[root@pc1 test]# bash test.sh     ## 执行脚本
请输入:45
90

 

 

 。