bash 测试脚本
在本系列的第一篇文章中,您创建了您的第一个非常小的单行Bash脚本,并探讨了创建Shell脚本的原因。 在第二篇文章中 ,您开始创建一个相当简单的模板,该模板可以作为其他Bash程序的起点,并开始对其进行测试。 在第三篇文章中 ,您创建并使用了一个简单的Help函数,并了解了有关使用函数以及如何处理命令行选项(例如-h)的知识 。
本系列的第四篇也是最后一篇文章介绍了变量并对其进行初始化,以及如何进行一些健全性测试以帮助确保程序在适当的条件下运行。 请记住,本系列文章的目的是构建可用于将来Bash编程项目的模板的工作代码。 这个想法是通过使模板中已有可用的通用元素,使新编程项目的入门变得容易。
变数
像所有编程语言一样,Bash shell可以处理变量。 变量是一个符号名称,它引用内存中包含某种值的特定位置。 变量的值是可变的,即它是可变的。 如果您不熟悉使用变量,请先阅读我的文章“ 如何使用Bash编程:语法和工具” 。
做完了吗 大! 现在让我们看一下使用变量时的一些好的做法。
到目前为止,您的小脚本只有一个变量$ option 。 如图所示,通过插入以下行来进行设置:
################################################################################
################################################################################
# Main program #
################################################################################
################################################################################
# Initialize variables
option =
""
################################################################################
# Process the input options. Add options as needed. #
################################################################################
测试此内容以确保一切正常,并且此更改不会导致任何损坏。
常数
常数也是变量,至少应该如此。 在命令行界面(CLI)程序中尽可能使用变量,而不要使用硬编码的值。 即使您认为只使用一次特定值(例如目录名,文件名或文本字符串),也要创建一个变量并将其用于放置硬编码名称的位置。
例如,作为程序主体的一部分打印的消息是字符串文字, 回显“ Hello world!”。 。 将其更改为变量。 首先,将以下语句添加到变量初始化部分:
Msg = "Hello world!"
现在,从以下位置更改程序的最后一行:
echo "Hello world!"
至:
echo " $Msg "
测试结果。
健全性检查
健全性检查只是为了使程序正确运行而必须满足的条件的测试,例如:程序必须以root用户身份运行,或者必须在该发行版的特定发行版和发行版上运行。 在简单程序模板中以运行用户身份添加对root的检查。
测试root用户正在运行该程序很容易,因为程序与启动它的用户一起运行。
id命令可用于确定程序正在其下运行的数字用户ID(UID)。 不带任何选项的情况下,它提供了一些信息:
[ student
@ testvm1 ~
] $
id
uid =
1001
( student
)
gid =
1001
( student
)
groups =
1001
( student
) ,
5000
( dev
)
使用-u选项仅返回用户的UID,可以在您的Bash程序中轻松使用它:
[ student
@ testvm1 ~
] $
id
-u
1001
[ student
@ testvm1 ~
] $
在程序中添加以下功能。 我在“帮助”过程之后添加了它,但是您可以将其放在“过程”部分的任何位置。 逻辑是,如果UID不为零(始终是root用户的UID),则程序退出:
################################################################################
# Check for root. #
################################################################################
CheckRoot
(
)
{
if
[
`
id -u
`
! =
0
]
then
echo
"ERROR: You must be root user to run this program"
exit
fi
}
现在,在变量初始化之前添加对CheckRoot过程的调用。 测试一下,首先以学生用户身份运行程序:
[ student
@ testvm1 ~
] $ .
/ hello
ERROR: You must be root user to run this program
[ student
@ testvm1 ~
] $
然后以root用户身份:
[ root
@ testvm1 student
]
# ./hello
Hello world
!
[ root
@ testvm1 student
]
#
您可能并不总是需要此特定的健全性测试,因此请注释掉对CheckRoot的调用,但将所有代码保留在模板中。 这样,在以后的程序中使用该代码所需要做的就是取消对该调用的注释。
代码
完成上述概述后,您的代码应如下所示:
#!/usr/bin/bash
################################################################################
# scriptTemplate #
# #
# Use this template as the beginning of a new program. Place a short #
# description of the script here. #
# #
# Change History #
# 11/11/2019 David Both Original code. This is a template for creating #
# new Bash shell scripts. #
# Add new history entries as needed. #
# #
# #
################################################################################
################################################################################
################################################################################
# #
# Copyright (C) 2007, 2019 David Both #
# LinuxGeek46@both.org #
# #
# This program is free software; you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation; either version 2 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program; if not, write to the Free Software #
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #
# #
################################################################################
################################################################################
################################################################################
################################################################################
# Help #
################################################################################
Help
(
)
{
# Display Help
echo
"Add description of the script functions here."
echo
echo
"Syntax: scriptTemplate [-g|h|v|V]"
echo
"options:"
echo
"g Print the GPL license notification."
echo
"h Print this Help."
echo
"v Verbose mode."
echo
"V Print software version and exit."
echo
}
################################################################################
# Check for root. #
################################################################################
CheckRoot
(
)
{
# If we are not running as root we exit the program
if
[
`
id -u
`
! =
0
]
then
echo
"ERROR: You must be root user to run this program"
exit
fi
}
################################################################################
################################################################################
# Main program #
################################################################################
################################################################################
################################################################################
# Sanity checks #
################################################################################
# Are we rnning as root?
# CheckRoot
# Initialize variables
option =
""
Msg =
"Hello world!"
################################################################################
# Process the input options. Add options as needed. #
################################################################################
# Get the options
while
getopts
":h" option;
do
case
$option
in
h
)
# display Help
Help
exit
;;
\?
)
# incorrect option
echo
"Error: Invalid option"
exit
;;
esac
done
echo
" $Msg "
最后的练习
您可能已经注意到,代码中的“帮助”功能是指代码中未包含的功能。 作为最后的练习,请弄清楚如何将这些功能添加到您创建的代码模板中。
摘要
在本文中,您创建了两个函数来执行完整性测试,以检查您的程序是否以root用户身份运行。 您的程序变得越来越复杂,因此测试变得越来越重要,并且需要更多测试路径才能完成。
本系列研究了一个非常小的Bash程序以及如何一次构建一个脚本。 结果是一个简单的模板,可以作为其他更有用的Bash脚本的起点,并且包含使启动新脚本容易的有用元素。
到目前为止,您已经明白了:编译程序是必需的,并且满足非常重要的需求。 但是对于系统管理员来说,总有更好的方法。 始终使用shell脚本来满足您工作的自动化需求。 Shell脚本是开放的; 他们的内容和目的是可以理解的。 它们可以很容易地修改以满足不同的要求。 我从来没有发现我在sysadmin角色中需要做的任何事情都无法用Shell脚本完成。
到目前为止,您在本系列中创建的内容仅仅是开始。 当您编写更多的Bash程序时,您会发现更多经常使用的代码,这些代码应包含在程序模板中。
资源资源
本系列文章部分基于David Both的Linux自学课程(共三部分)的第2卷第10章,该课程使用和管理Linux —从零到SysAdmin 。
翻译自: https://opensource.com/article/19/12/testing-bash-script
bash 测试脚本