一、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. c++实现对windwos 下socket 的封装(实现封包及拆包处理)

    SuperSocket.h #pragma once #include<string> #include<iostream> #include <WINSOCK2.H&g ...

  2. Connect To Ubuntu 16.04 / 17.10 / 18.04 Desktop Via Remote Desktop Connection (RDP) With Xrdp

    [1] https://websiteforstudents.com/connect-to-ubuntu-16-04-17-10-18-04-desktop-via-remote-desktop-co ...

  3. github使用步骤

    首先需要注册一个github账号 1.认识github首页界面 2.如何新建一个自己的仓库 3.创建README文件 4.创建自己的文件 5.解析文件 6.生成地址 7.如何修改编辑文件

  4. LAMP安装教程

    LAMP环境配置安装注意安装步骤及说明事项. Linux + apache+mysql+php 附件: 1. 访问ftp报错 解决: 关闭selinux vi  /etc/selinux/config ...

  5. genymotion常见问题解答

    [转]常见问题解答 很多人喜欢使用Genymotion这款安卓模拟器,但是虽然Genymotion很好用,可是却有各种问题存在哦,下面潇潇就一些常见的Genymotion问题来说下解决方法吧. 为什么 ...

  6. Numpy学习一:ndarray数组对象

    NumPy是Python的一个高性能科学计算和数据分析基础库,提供了功能强大的多维数组对象ndarray.jupyter notebook快速执行代码的快捷键:鼠标点击选中要指定的代码框,Shift ...

  7. HystrixCommand实战

    1. HystrixCommand实战 1.1. 需求 由于前端公共调用入口接口代码,封装在单独的jar包,它不属于springCloud管理,所以不适合用注解的方式@HystrixCommand进行 ...

  8. Spark基础-scala学习(七、类型参数)

    类型参数是什么 类似于java泛型,泛型类 泛型函数 上边界Bounds 下边界 View Bounds Context Bounds Manifest Context Bounds 协变和逆变 Ex ...

  9. chrome浏览器被reimage pair 劫持怎么处理

    不知道什么原因chrome浏览器被reimage pair劫持了,只要在浏览器内部一按回车,就马上进入了reimage pair下载的界面. 在网上找了很多解决方法,最后才在google的chrome ...

  10. Python档案袋(变量与流程控制)

    变量与运算 得到数据类型: ii=100 print(type(ii)) #输出:<class 'int'> 强制转换: ii=100 iix=str(ii) #可为int str flo ...