Shell编程-自定义函数和shell脚本调试

10 篇文章 0 订阅
1.自定义函数
函数代表着一个或一组命令的集合,表示一个功能模块,常用于模块化编程
一下是关于函数的重要说明
    在shell中,函数必须先定义,再调用
    使用 return value来获取函数的返回值
    函数在当前shell中执行,可以使用脚本中的变量


函数的格式如下:
函数名()
{
 命令1...
 命令2...
}


标准格式:其中function和()可省略,至少保留一个
[ function ] functionname [()]
{
    action
    [return int;]
}



例子:
#!/bin/bash

function start(){
echo "starting"
service httpd start
}

start

stop(){
echo "stopping"
service httpd stop
}

restart(){
echo "restart"
service httpd restart
}

stop

执行:
./func.sh



例子:
#!/bin/bash

function start(){
echo "starting"
service httpd start
}

start

stop(){
echo '$#='$#
echo "stopping"
service httpd stop
}

restart(){
echo "restart"
service httpd restart
}

stop

执行结果:

[root@VM_0_16_centos shell]# ./func2.sh hello
starting
Redirecting to /bin/systemctl start  httpd.service
$#=0
stopping
Redirecting to /bin/systemctl stop  httpd.service
[root@VM_0_16_centos shell]#


例子:修改,我们传入了参数,需要在里面调用的时候获取到参数,否则,在自定义函数里面相当于没有调用到参数:
#!/bin/bash

function start(){
echo "starting"
service httpd start
}

start

stop(){
echo '$#='$#
echo "stopping"
service httpd stop
}

restart(){
echo "restart"
service httpd restart
}

stop $1


执行结果:
[root@VM_0_16_centos shell]# ./func1.sh hello
starting
Redirecting to /bin/systemctl start  httpd.service
$#=1
stopping
Redirecting to /bin/systemctl stop  httpd.service



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


例子:返回值必须是int类型0-255范围
#!/bin/bash

function start(){
echo "starting"
service httpd start
}

start

stop(){
echo '$#='$#
echo "stopping"
service httpd stop
}

restart(){
return "abc"
}

stop $1

restart

执行报错:
[root@VM_0_16_centos shell]# ./func3.sh
starting
Redirecting to /bin/systemctl start  httpd.service
$#=0
stopping
Redirecting to /bin/systemctl stop  httpd.service
./func3.sh: line 17: return: abc: numeric argument required
[root@VM_0_16_centos shell]#


我们可以使用最后一条命令的执行结果$?,也可以在function中显示返回int值(0-255)




3.数据库脚本调试
针对某些shell脚本拷贝到某些环节可能会报错,或者其中的业务逻辑不符合结果,那么就需要进行脚本调试。

shell提供了脚本调试的方法:
1)语法问题:bash -n shellname
bash -n func3.sh


例子:
#!/bin/bash

test{
hello
}

function start(){
echo "starting"
service httpd start
}

start

stop(){
echo '$#='$#
echo "stopping"
service httpd stop
}

restart(){
return "abc"
}

stop $1

restart

其中方法test的定义有问题:这样我们可以检测语法的问题。
[root@VM_0_16_centos shell]# bash -n func3.sh
func3.sh: line 5: syntax error near unexpected token `}'
func3.sh: line 5: `}'
[root@VM_0_16_centos shell]#



2)调试shell脚本:bash -x shellname

[root@VM_0_16_centos shell]# bash -x func3.sh
+ start
+ echo starting
starting
+ service httpd start
Redirecting to /bin/systemctl start  httpd.service
+ stop
+ echo '$#=0'
$#=0
+ echo stopping
stopping
+ service httpd stop
Redirecting to /bin/systemctl stop  httpd.service
+ restart
+ return abc
func3.sh: line 17: return: abc: numeric argument required

可以看到脚本的调用顺序,以及每一步的执行打印的结果

3)显示包内容,并显示执行的结果bash -v shellname

[root@VM_0_16_centos shell]# bash -v func3.sh
#!/bin/bash

function start(){
echo "starting"
service httpd start
}

start
starting
Redirecting to /bin/systemctl start  httpd.service

stop(){
echo '$#='$#
echo "stopping"
service httpd stop
}

restart(){
return "abc"
}

stop $1
$#=0
stopping
Redirecting to /bin/systemctl stop  httpd.service

restart
func3.sh: line 17: return: abc: numeric argument required

[root@VM_0_16_centos shell]#


4)指定调试的代码:set -x放到代码中的位置
例子:
原来的if的判断shell:
#!/bin/bash

read -t 10 -p "please input your name and age:" name age
echo $name $age

#read -t 30  -sp "input your age:" age
#echo $age


#read -t 30 -n 1  -p "input your gender[m|f]:" gender
#echo $gender


[root@VM_0_16_centos es]# cat if.sh
#!/bin/bash

read -p "please input your name " name

echo $name

if [ "$name"==root ];then
        echo "welcome super man!"
elif [ "$name"==spark ];then
        echo "welcome spark "$name
else
        echo "welcome out bye"
fi

执行:
[root@VM_0_16_centos es]# bash -x if.sh
+ read -p 'please input your name ' name
please input your name root
+ echo root
root
+ '[' root==root ']'
+ echo 'welcome super man!'
welcome super man!


如果加上set -x
如下:
[root@VM_0_16_centos es]# bash if.sh
please input your name root
root
+ echo 'welcome super man!'
welcome super man!


可以看到调试的范围不同。

另外-n是检测代码的问题
-v是显示代码















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值