**
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