大数据兼云计算(王明龙)讲师-LINUX-DAY19-SHELL-编写与函数定义

本文深入探讨Shell脚本的编写方法与格式,包括如何定义和调用Shell函数,以及函数参数传递和文件调用技巧。同时,文章还介绍了如何在脚本中使用函数进行时间显示、参数传递和嵌套调用等实际操作。

**

SHELL-编写与函数定义

**

Shell编写

Shell脚本格式
-------------------------------------------------------------------------------------------------------------------
# vim /脚本名.sh
#!/bin/bash     指定使用shell,不设置也可执行,但要确定系统自定义了SHELL的变量
# 脚本作用描述
# 创建者
动作

执行脚本
-------------------------------------------------------------------------------------------------------------------
第1种方法:不用给x权限
# . 脚本名
# bash 脚本名
# sh  脚本名

第2种方法:给执行x权限
# chmod +x 脚本名   或  chmod 777 脚本名
# ./脚本名

Shell函数

函数定义:
shell允许将一组命令集成或语句形成一个可用块,这些块称为shell函数


定义函数的格式
----------------------------------------------------------------------------------------------------------
第1种格式
---------------------------------------------------------------------------------------------
函数名()
{
命令1
.....
return 1或0
}
函数名或相关命令        //调用函数



第2种格式
---------------------------------------------------------------------------------------------
function 函数名()
{
命令1
...
return 1或0
} 
函数名或相关命令        //调用函数

注意:return 1或0 表示检查函数值是否正确,返回0为正确,1为错误


举例
---------------------------------------------------------------------------------------------

1. 做一个显示时间的函数
[root@localhost ~]# vim wml.sh
-------------------------------------------------------
#!/bin/bash
hello ()
{
echo "wml,today is `date`"
return 1
}
hello 
-------------------------------------------------------

[root@localhost ~]# bash wml.sh
wml,today is 2017年 06月 26日 星期一 04:32:37 CST


2.参数传递:参数是通过位置变量$1,$2....$9传递到函数中去
[root@localhost ~]# vim wml.sh
-------------------------------------------------------
#!/bin/bash
hello ()
{
echo "$1 today is `date`"
return 1
}
hello wangminglong 
-------------------------------------------------------

[root@localhost ~]# bash wml.sh
wangminglong today is 2017年 06月 26日 星期一 04:35:07 CST


3.函数文件调用到脚本
[root@localhost ~]# vim wml.sh
-------------------------------------------------------
#!/bin/bash
hello ()
{
echo "$1 today is `date`"
return 1
}
-------------------------------------------------------

[root@localhost ~]# chmod +x wml.sh 
[root@localhost ~]# vim long.sh

-------------------------------------------------------
#!/bin/bash
. wml.sh 
hello
-------------------------------------------------------
. 后面是调用函数文件
hello是函数文件里的函数

[root@localhost ~]# chmod +x long.sh 
[root@localhost ~]# ./long.sh
today is 2017年 06月 26日 星期一 04:38:38 CST


不在一目录下,执行函数文件,w.sh
-----------------------------------------------------------
#!/bin/bash
. /root/wml/wml.sh
hello liuhuixin
-----------------------------------------------------------



4.查看脚本中函数调用的内容,set查看载入函数
[root@localhost ~]# vim long.sh
-------------------------------------------------------
#!/bin/bash
. wml
set
hello
-------------------------------------------------------
[root@localhost ~]# ./long.sh
hello () 
{ 
   echo "$1 today is `date`";
   return 1
}

wangminglong today is 2014年 02月 22日 星期六 22:57:21 CST

嵌套函数
[root@web wml]# vim l.sh
#!/bin/bash
. /root/wml/wml.sh
b ()
{
  df -h
  hello wml
  c ()
  {
    ps -ef
  }
  c
}
free -m
b
c



5.unset删除函数
[root@localhost ~]# vim long.sh
-------------------------------------------------------
#!/bin/bash
. wml
unset hello
hello
-------------------------------------------------------
[root@localhost ~]# ./long.sh
      bash: .wml.sh: command not found


6.函数文件指定多个函数,调用文件执行多个函数
[root@localhost ~]# vim wml.sh
------------------------------------------------------------------------------------
#!/bin/bash
# date function
hello ()
{
   echo "$1,today is `date`"
}

tjd ()
{
   read -p "tjd mr please input abc : " a
}
------------------------------------------------------------------------------------

[root@localhost ~]# vim long.sh 
------------------------------------------------------------------------------------
#!/bin/bash
. wml.sh
hello wml
echo "-------------------------------------"
tjd
------------------------------------------------------------------------------------

执行结果:
[root@localhost ~]# ./long.sh
wml,today is 2017年 08月 09日 星期三 15:14:32 CST
-------------------------------------
tjd mr please input abc : abc

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT灵魂摆渡者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值