一、Shell脚本中的函数

和C语言类似,Shell中也有函数的概念,但是函数定义中没有返回值也没有参数列表。例如:

#! /bin/sh

fun(){ echo "Function fun is called";}
echo "-=start=-"
fun
echo "-=end=-"

注意函数体的左花括号'{'和后面的命令之间必须有空格或换行,如果将最后一条命令和右花括号'}'写在同一行,命令末尾必须有;号。

在定义fun()函数时并不执行函数体中的命令,就像定义变量一样,只是给fun这个名字一个定义,到后面调用fun函数的时候(注意Shell中的函数调用不写括号)才执行函数体中的命令。Shell脚本中的函数必须先定义后调用,一般把函数定义都写在脚本的前面,把函数调用和其它命令写在脚本的最后(类似C语言中的main函数,这才是整个脚本实际开始执行命令的地方)。

Shell函数没有参数列表并不表示不能传参数,事实上,函数就像是迷你脚本,调用函数时可以传任意个参数,在函数内同样是用$0、$1、$2等变量来提取参数,函数中的位置参数相当于函数的局部变量,改变这些变量并不会影响函数外面的$0、$1、$2等变量。函数中可以用return命令返回,如果return后面跟一个数字则表示函数的Exit Status。

下面这个脚本可以一次创建多个目录,各目录名通过命令行参数传入,脚本逐个测试各目录是否存在,如果目录不存在,首先打印信息然后试着创建该目录。

编辑脚本
[root@VM_0_5_centos test]# vi tfun.sh
查看脚本内容
[root@VM_0_5_centos test]# cat tfun.sh
#! /bin/sh is_directory()
{
DIR_NAME=$1
if [ ! -d $DIR_NAME ]; then
return 1
else
return 0
fi
} for DIR in "$@"; do
if is_directory "$DIR"
then :
else
echo "Directory $DIR doesn't exist. Creating it now..."
mkdir $DIR > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "Can't create directory $DIR"
exit 1
fi
fi
done
运行测试脚本
[root@VM_0_5_centos test]# sh tfun.sh aaa abb acc add
Directory aaa doesn't exist. Creating it now...
Directory abb doesn't exist. Creating it now...
Directory acc doesn't exist. Creating it now...
Directory add doesn't exist. Creating it now...
查看是否成功创建目录
[root@VM_0_5_centos test]# ll
total 20
drwxr-xr-x 2 root root 4096 Jul 16 11:16 aaa
drwxr-xr-x 2 root root 4096 Jul 16 11:16 abb
drwxr-xr-x 2 root root 4096 Jul 16 11:16 acc
drwxr-xr-x 2 root root 4096 Jul 16 11:16 add
-rw-r--r-- 1 root root 340 Jul 16 11:15 tfun.sh

注意:is_directory()返回0表示真返回1表示假。mkdir $DIR > /dev/null 2>&1这条语句时因为直接使用mkdir $DIR或mkdir $DIR > /dev/null可能会出错,所以将可能出错的情况都重定向输出到/dev/null这个黑洞目录当中。

二、Shell脚本的调试方法

Shell提供了一些用于调试脚本的选项,如下所示:

-n

读一遍脚本中的命令但不执行,用于检查脚本中的语法错误

-v

一边执行脚本,一边将执行过的脚本命令打印到标准错误输出

-x

提供跟踪执行信息,将执行的每一条命令和结果依次打印出来

使用这些选项有三种方法,一是在命令行提供参数

[root@VM_0_5_centos test]# sh -x tfun.sh a1 a2 a3
 [root@VM_0_5_centos test]# sh -x tfun.sh a1 a2 a3
+ for DIR in '"$@"'
+ is_directory a1
+ DIR_NAME=a1
+ '[' '!' -d a1 ']'
+ return 1
+ set -x
+ echo 'Directory a1 doesn'\''t exist. Creating it now...'
Directory a1 doesn't exist. Creating it now...
+ mkdir a1
+ '[' 0 -ne 0 ']'
+ set +x
+ echo 'Directory a2 doesn'\''t exist. Creating it now...'
Directory a2 doesn't exist. Creating it now...
+ mkdir a2
+ '[' 0 -ne 0 ']'
+ set +x
+ echo 'Directory a3 doesn'\''t exist. Creating it now...'
Directory a3 doesn't exist. Creating it now...
+ mkdir a3
+ '[' 0 -ne 0 ']'
+ set +x

第一种方法测试结果:

二是在脚本开头提供参数

#! /bin/sh -x

第三种方法是在脚本中用set命令启用或禁用参数

for DIR in "$@"; do
if is_directory "$DIR"
then :
else
set -x
echo "Directory $DIR doesn't exist. Creating it now..."
mkdir $DIR > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "Can't create directory $DIR"
exit 1
fi
set +x
fi
done
 [root@VM_0_5_centos test]# sh tfun.sh b bb bbb
+ echo 'Directory b doesn'\''t exist. Creating it now...'
Directory b doesn't exist. Creating it now...
+ mkdir b
+ '[' 0 -ne 0 ']'
+ set +x
+ echo 'Directory bb doesn'\''t exist. Creating it now...'
Directory bb doesn't exist. Creating it now...
+ mkdir bb
+ '[' 0 -ne 0 ']'
+ set +x
+ echo 'Directory bbb doesn'\''t exist. Creating it now...'
Directory bbb doesn't exist. Creating it now...
+ mkdir bbb
+ '[' 0 -ne 0 ']'
+ set +x

第三种方法测试结果:

set -x和set +x分别表示启用和禁用-x参数,这样可以只对脚本中的某一段进行跟踪调试。

shell编程基础(四): shell脚本语法之函数及调试的更多相关文章

  1. shell编程基础(二): shell脚本语法之分支语句和循环语句

    一.分支语句 1.条件测试:test [ 命令test或[可以测试一个条件是否成立,如果测试结果为真,则该命令的Exit Status为0,如果测试结果为假,则命令的Exit Status为1(注意与 ...

  2. Shell编程(五)脚本语法

    ${}: 数据“内容”删除,替换:{}: 列表 1. 条件测试: test =~:正则匹配 2. if/then/elif/else/fi #!/bin/bash echo "Is it o ...

  3. 【Shell 编程基础第二部分】Shell里的流程控制、Shell里的函数及脚本调试方法!

    http://blog.csdn.net/xiaominghimi/article/details/7603003 本站文章均为李华明Himi原创,转载务必在明显处注明:转载自[黑米GameDev街区 ...

  4. 【转】Shell编程基础篇-下

    [转]Shell编程基础篇-下 1.1 条件表达式 1.1.1 文件判断 常用文件测试操作符 常用文件测试操作符 说明 -d文件,d的全拼为directory 文件存在且为目录则为真,即测试表达式成立 ...

  5. 【转】Shell编程基础篇-上

    [转]Shell编程基础篇-上 1.1 前言 1.1.1 为什么学Shell Shell脚本语言是实现Linux/UNIX系统管理及自动化运维所必备的重要工具, Linux/UNIX系统的底层及基础应 ...

  6. shell编程基础(转载)

    Shell编程基础 原作者 Leal:请参阅页面底部的编者列表. 授权许可: 创作共享署名协议 GNU 自由文档许可证 注意:本文仍然在持续的修订之中,且错漏之处可能较多.如果能够阅读英语的话,可以考 ...

  7. 6-2 shell编程基础

    shell编程基础 编程基础 Linus:Talk is cheap, show me the code 程序和编程风格 程序: 程序:算法+数据结构 数据:是程序的核心 算法:处理数据的方式 数据结 ...

  8. Linux学习之二十一-shell编程基础

    Shell编程基础 Shell 是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁.Shell 既是一种命令语言,又是一种程序设计语言.Shell 是指一种应用程序,这个应用程序提供了一个 ...

  9. shell编程系列26--大型脚本工具开发实战

    shell编程系列26--大型脚本工具开发实战 大型脚本工具开发实战 拆分脚本功能,抽象函数 .function get_all_group 返回进程组列表字符串 .function get_all_ ...

随机推荐

  1. Appium之Android功能脚本

    Android功能脚本 注:这里只写了登录和退出功能,以下不提供app的包名,下面我使用的是线上包 准备:1.Eclipse的Java环境:2.Appium环境:3.Android真机一台. 创建一个 ...

  2. 时序扩展的UML状态图的测试用例生成研究

    一.基本信息 标题:时序扩展的UML状态图的测试用例生成研究 时间:2014 出版源:西南大学 领域分类:时序扩展:UML状态图:测试用例:需求规格说明:模型 二.研究背景 问题定义:时序扩展的UML ...

  3. 针对zstack虚拟机导出的问题的解决办法!

    1. nfs 首先定位image所在位置: 云主机--〉云盘操作--〉配置信息--〉更多信息--安装路径 /opt/zstack/nfsprimarystorage/prim-aab63dc6284f ...

  4. 网络操作系统 第七章 管理TCP/IP网络

    本章小结 本章介绍了TCP/IP的相关概念,并且在此处基础上,介绍了Windows Server 2008中使用TCP/IP网络配置工具实现网络连接和管理的方法,在Linux系统中,讲解了是如何使用图 ...

  5. vue图片上传

    之前花了两个月用vue做了一个建筑照片我的webApp,前端就我一人,负责用vue写页面对接接口,后台一个程序员负责写接口,嵌套个安卓ios的壳.搞的是风风火火,过程也是很心累,大多不在技术,在于所谓 ...

  6. appium+python自动化脚本

    用pycharm,首先得把appium导入,操作如下(否则,运行程序后会报错,没有module appium) Settings->Project Interpreter,双击pip,搜索app ...

  7. PIL库图像处理

    PIL有如下几个模块 Image模块.ImageChops模块.ImageCrackCode模块 ImageDraw模块.ImageEnhance模块.ImageFile模块 ImageFileIO模 ...

  8. [转] IPTables for KVM Host

    IPTables for KVM Host January 26, 2012 By Andrew Galdes Use the following IPTables rules “/etc/sysco ...

  9. [编译] 2、minGW gcc在windows搭建编译win32程序环境

    1.普通下载一个MinGW程序.安装之后可以直接将MinGW目录拷贝到总工程的tool里面: demo_mesh_common tree -L 2 . ├── app ├── bin ├── buil ...

  10. 像纸质笔记本一样给div,textarea添加行的分割线

    想要给textarea添加一个背景图来实现 但是背景图有几个问题, 1.每个div或者textarea的line-height不一样,对于每个不同的line-height都需要一个不同的背景图 2.当 ...