[转载]Bourne Shell及shell编程 七
时间:2005-11-26 来源:ghostzhu
版权声明:
本文内容为大连理工大学LINUX选修课讲义,欢迎大家转载,但禁止使用本材料进行
任何商业性或赢利性活动。转载时请保留本版权声明。
作者:何斌武,[email protected],大连理工大学网络中心,April 1999.
[请点击阅读全文]
——by ghostzhu
下面详细讲述了每一条建议以及用于说明问题的脚本。若要下载此脚本,请参阅本文后面
的参考资料部分。
1. 记录运行脚本的先决条件和主要步骤
记录,尤其是以有自述标题的单个文件(例如 "README-testing.txt")记录功能测试的主
要想法是很重要的,包括,如先决条件、服务器和客户机的设置、脚本遵循的整个(或详
细的)步骤、如何检查脚本的成功/失败、如何执行清除和重新启动测试。
2. 将操作分成若干个逻辑组
如果仅仅执行数量非常少的操作,可以将它们全部放在一个简单的 shell 脚本中。
但是,如果需要执行一些数量很多的操作,那最好是将它们分成若干个逻辑集合,例如将
一些服务器操作放在一个文件而将客户机操作放在在另一个文件中。通过这种方法,划分
适当的颗粒度来执行测试和维护测试。
3. 基于一般方案制定执行步骤
一旦决定对操作进行分组,需要根据一般方案考虑执行操作的步骤。此想法是模拟实际生
活中最终用户的情形。作为一个总体原则,只需集中测试 80% 最常调用函数的 20% 用法
即可。
例如,假设应用程序要求 3 个测试组以某个特定的顺序排列。每个测试组可以放在一个带
有自我描述文件名(如果可能)的文件中,并用号码来帮助识别每个文件的顺序,例如:
代码:
1. fvt-setup-1: To perform initial setup.
2. fvt-server-2: To perform server commands.
3. fvt-client-3: To perform client commands.
4. fvt-cleanup: To cleanup the temporary files,
in order to prepare for the repetition
of the above test cases.
4. 在每个 shell 脚本中提供注释和说明
在每个 shell 脚本的头文件中提供相关的注释和说明是一个良好的编码习惯。这样的话,
当另一个测试者运行该脚本时,测试者就能清楚地了解每个脚本中测试的范围、所有先决
条件和警告。
下面是一个 Bash 脚本 "test-bucket-1" 的示例 。
代码:
#!/bin/bash
#
# Name: test-bucket-1
#
# Purpose:
# Performs the test-bucket number 1 for Product X.
# (Actually, this is a sample shell script,
# which invokes some system commands
# to illustrate how to construct a Bash script)
#
# Notes:
# 1) The environment variable TEST_VAR must be set
# (as an example).
# 2) To invoke this shell script and redirect standard
# output and standard error to a file (such as
# test-bucket-1.out) do the following (the -s flag
# is "silent mode" to avoid prompts to the user):
#
# ./test-bucket-1 -s 2>&1 | tee test-bucket-1.out
#
# Return codes:
# 0 = All commands were successful
# 1 = At least one command failed, see the output file
# and search for the keyword "ERROR".
#
########################################################
5. 做一个初始备份以创建基准线
您可能需要多次执行功能测试。第一次运行它时,也许会找到脚本或进程中的一些错误。
因而,为了避免因从头重新创建服务器环境而浪费大量时间 -- 特别是如果涉及到数据库
-- 您在测试之前或许想做个备份。
在运行完功能测试之后,就可以从备份中恢复服务器了,同时也为下一轮测试做好了准备
。
6. 检查输入参数和环境变量
最好校验一下输入参数,并检查环境变量是否设置正确。如果有问题,显示问题的原因及
其修复方法,然后终止脚本。
当测试者准备运行脚本,而此时如果没有正确设置脚本所调用的环境变量,但由于发现及
时,终止了脚本,那测试者会相当感谢。没有人喜欢等待脚本执行了很久却发现没有正确
设置变量。
代码:
# --------------------------------------------
# Main routine for performing the test bucket
# --------------------------------------------
CALLER=`basename $0` # The Caller name
SILENT="no" # User wants prompts
let "errorCounter = 0"
# ----------------------------------
# Handle keyword parameters (flags).
# ----------------------------------
# For more sophisticated usage of getopt in Linux,
# see the samples file: /usr/lib/getopt/parse.bash
TEMP=`getopt hs $*`
if [ $? != 0 ]
then
echo "$CALLER: Unknown flag(s)"
usage
fi
# Note quotes around `$TEMP : they are essential!
eval set -- "$TEMP"
while true
do
case "$1" in
-h) usage "HELP"; shift;; # Help requested
-s) SILENT="yes"; shift;; # Prompt not needed
--) shift ; break ;;
*) echo "Internal error!" ; exit 1 ;;
esac
done
# ------------------------------------------------
# The following environment variables must be set
# ------------------------------------------------
if [ -z "$TEST_VAR" ]
then
echo "Environment variable TEST_VAR is not set."
usage
fi
关于此脚本的说明如下:
使用语句 CALLER=`basename $0` 可以得到正在运行的脚本名称。这样的话,无须在脚本
中硬编码脚本名称。因此当复制脚本时,采用新派生的脚本可以减少工作量。
调用脚本时,语句 TEMP=`getopt hs $*` 用于得到输入变量(例如 -h 代表帮助,-s 代
表安静模式)。
语句 [ -z "$X" ] 和 echo "The environment variable X is not set." 以及 usage 都
是用于检测字符串是否为空 (-z),如果为空,随后就执行 echo 语句以显示未设置字符串
并调用下面要讨论的 "usage" 函数。
若脚本未使用标志,可以使用变量 "$#",它可以返回正在传递到脚本的变量数量。