有人知道针对 shell 脚本(sh、bash 等)的最佳实践或设计模式的任何资源吗?
有人知道针对 shell 脚本(sh、bash 等)的最佳实践或设计模式的任何资源吗?
CommandLineOptions__config_file=""
CommandLineOptions__debug_level=""
getopt_results=`getopt -s bash -o c:d:: --long config_file:,debug_level:: -- "$@"`
if test $? != 0
then
echo "unrecognized option"
exit 1
fi
eval set -- "$getopt_results"
while true
do
case "$1" in
--config_file)
CommandLineOptions__config_file="$2";
shift 2;
;;
--debug_level)
CommandLineOptions__debug_level="$2";
shift 2;
;;
--)
shift
break
;;
*)
echo "$0: unparseable option $1"
EXCEPTION=$Main__ParameterException
EXCEPTION_MSG="unparseable option $1"
exit 1
;;
esac
done
if test "x$CommandLineOptions__config_file" == "x"
then
echo "$0: missing config_file parameter"
EXCEPTION=$Main__ParameterException
EXCEPTION_MSG="missing config_file parameter"
exit 1
fi
另一个重要的点是,如果程序成功完成,应该始终返回零,如果出现错误,则返回非零。
函数调用
在bash中,您可以调用函数,只需记得在调用之前定义它们。函数就像脚本一样,它们只能返回数值。这意味着您必须发明一种不同的策略来返回字符串值。我的策略是使用名为RESULT的变量存储结果,并且如果函数干净地完成,则返回0。 此外,如果返回值与零不同,则可以引发异常,然后设置两个“异常变量”(我的是:EXCEPTION和EXCEPTION_MSG),第一个包含异常类型,第二个包含可读性的消息。
当您调用函数时,函数的参数被分配给特殊变量$0,$1等。我建议您将它们放入更有意义的名称中。在函数内部声明变量时,请将它们声明为局部变量:
function foo {
local bar="$0"
}
容易出错的情况
在Bash中,除非你声明否则,未设置的变量将被使用为空字符串。这样很危险,如果有拼写错误,错误的变量不会被报告,而且会被评估为空。请使用
set -o nounset
if test "x${foo:-notset}" == "xnotset"
then
echo "foo not set"
fi
您可以将变量声明为只读:
readonly readonly_var="foo"
模块化
如果你使用以下代码,你可以实现类似 Python 的模块化:
set -o nounset
function getScriptAbsoluteDir {
# @description used to get the script path
# @param $1 the script $0 parameter
local script_invoke_path="$1"
local cwd=`pwd`
# absolute path ? if so, the first character is a /
if test "x${script_invoke_path:0:1}" = 'x/'
then
RESULT=`dirname "$script_invoke_path"`
else
RESULT=`dirname "$cwd/$script_invoke_path"`
fi
}
script_invoke_path="$0"
script_name=`basename "$0"`
getScriptAbsoluteDir "$script_invoke_path"
script_absolute_dir=$RESULT
function import() {
# @description importer routine to get external functionality.
# @description the first location searched is the script directory.
# @description if not found, search the module in the paths contained in $SHELL_LIBRARY_PATH environment variable
# @param $1 the .shinc file to import, without .shinc extension
module=$1
if test "x$module" == "x"
then
echo "$script_name : Unable to import unspecified module. Dying."
exit 1
fi
if test "x${script_absolute_dir:-notset}" == "xnotset"
then
echo "$script_name : Undefined script absolute dir. Did you remove getScriptAbsoluteDir? Dying."
exit 1
fi
if test "x$script_absolute_dir" == "x"
then
echo "$script_name : empty script path. Dying."
exit 1
fi
if test -e "$script_absolute_dir/$module.shinc"
then
# import from script directory
. "$script_absolute_dir/$module.shinc"
elif test "x${SHELL_LIBRARY_PATH:-notset}" != "xnotset"
then
# import from the shell script library path
# save the separator and use the ':' instead
local saved_IFS="$IFS"
IFS=':'
for path in $SHELL_LIBRARY_PATH
do
if test -e "$path/$module.shinc"
then
. "$path/$module.shinc"
return
fi
done
# restore the standard separator
IFS="$saved_IFS"
fi
echo "$script_name : Unable to find module $module."
exit 1
}
接下来,您可以使用以下语法导入扩展名为.shinc的文件
import "AModule/ModuleFile"
文件将在SHELL_LIBRARY_PATH中搜索。由于您总是在全局命名空间中导入,请记得为所有函数和变量添加适当的前缀,否则可能会出现名称冲突。我使用双下划线作为Python点。
此外,请将此作为模块中的第一件事。
# avoid double inclusion
if test "${BashInclude__imported+defined}" == "defined"
then
return 0
fi
BashInclude__imported=1
面向对象编程
在bash中,你不能做面向对象编程,除非你构建一个相当复杂的对象分配系统(我曾考虑过这个想法,但它是可行的,却是疯狂的)。 实际上,你可以进行“单例模式编程”:你只有每个对象的一个实例。
我的做法是:我在一个模块中定义一个对象(请参见模块化入口)。然后我定义空变量(类似于成员变量)、一个 init 函数(构造函数)和成员函数,就像这个示例代码。
# avoid double inclusion
if test "${Table__imported+defined}" == "defined"
then
return 0
fi
Table__imported=1
readonly Table__NoException=""
readonly Table__ParameterException="Table__ParameterException"
readonly Table__MySqlException="Table__MySqlException"
readonly Table__NotInitializedException="Table__NotInitializedException"
readonly Table__AlreadyInitializedException="Table__AlreadyInitializedException"
# an example for module enum constants, used in the mysql table, in this case
readonly Table__GENDER_MALE="GENDER_MALE"
readonly Table__GENDER_FEMALE="GENDER_FEMALE"
# private: prefixed with p_ (a bash variable cannot start with _)
p_Table__mysql_exec="" # will contain the executed mysql command
p_Table__initialized=0
function Table__init {
# @description init the module with the database parameters
# @param $1 the mysql config file
# @exception Table__NoException, Table__ParameterException
EXCEPTION=""
EXCEPTION_MSG=""
EXCEPTION_FUNC=""
RESULT=""
if test $p_Table__initialized -ne 0
then
EXCEPTION=$Table__AlreadyInitializedException
EXCEPTION_MSG="module already initialized"
EXCEPTION_FUNC="$FUNCNAME"
return 1
fi
local config_file="$1"
# yes, I am aware that I could put default parameters and other niceties, but I am lazy today
if test "x$config_file" = "x"; then
EXCEPTION=$Table__ParameterException
EXCEPTION_MSG="missing parameter config file"
EXCEPTION_FUNC="$FUNCNAME"
return 1
fi
p_Table__mysql_exec="mysql --defaults-file=$config_file --silent --skip-column-names -e "
# mark the module as initialized
p_Table__initialized=1
EXCEPTION=$Table__NoException
EXCEPTION_MSG=""
EXCEPTION_FUNC=""
return 0
}
function Table__getName() {
# @description gets the name of the person
# @param $1 the row identifier
# @result the name
EXCEPTION=""
EXCEPTION_MSG=""
EXCEPTION_FUNC=""
RESULT=""
if test $p_Table__initialized -eq 0
then
EXCEPTION=$Table__NotInitializedException
EXCEPTION_MSG="module not initialized"
EXCEPTION_FUNC="$FUNCNAME"
return 1
fi
id=$1
if test "x$id" = "x"; then
EXCEPTION=$Table__ParameterException
EXCEPTION_MSG="missing parameter identifier"
EXCEPTION_FUNC="$FUNCNAME"
return 1
fi
local name=`$p_Table__mysql_exec "SELECT name FROM table WHERE id = '$id'"`
if test $? != 0 ; then
EXCEPTION=$Table__MySqlException
EXCEPTION_MSG="unable to perform select"
EXCEPTION_FUNC="$FUNCNAME"
return 1
fi
RESULT=$name
EXCEPTION=$Table__NoException
EXCEPTION_MSG=""
EXCEPTION_FUNC=""
return 0
}
信号的捕获与处理
我发现这对于捕获和处理异常非常有用。
function Main__interruptHandler() {
# @description signal handler for SIGINT
echo "SIGINT caught"
exit
}
function Main__terminationHandler() {
# @description signal handler for SIGTERM
echo "SIGTERM caught"
exit
}
function Main__exitHandler() {
# @description signal handler for end of the program (clean or unclean).
# probably redundant call, we already call the cleanup in main.
exit
}
trap Main__interruptHandler INT
trap Main__terminationHandler TERM
trap Main__exitHandler EXIT
function Main__main() {
# body
}
# catch signals and exit
trap exit INT TERM EXIT
Main__main "$@"
提示和技巧
如果某些原因导致某些东西无法正常工作,请尝试重新排列代码。顺序很重要,而且并不总是直观的。
请不要考虑使用tcsh。它不支持函数,并且总体上很糟糕。
请注意:如果您必须使用我在这里写的东西,这意味着您的问题过于复杂,不能用shell解决。请使用另一种语言。我不得不使用它,是由于人为因素和遗留问题。
getopt
而不是 getopts
?因为 getopts
更加可移植,可以在任何 POSIX shell 中运行。特别是由于这个问题是关于 shell 最佳实践 而不是特定于 bash 的最佳实践,我支持遵循 POSIX 标准以支持尽可能多的 shell。 - Wimateeka看一下高级Bash脚本指南,了解有关shell脚本的许多智慧-不仅仅是Bash。
不要听信那些告诉你去看其他较复杂语言的人。如果shell脚本满足您的需求,请使用它。您需要的是功能性而不是花哨。新语言为您的简历提供了有价值的新技能,但如果您已经知道如何使用shell,并且有工作需要完成,那就没有帮助了。
正如所述,对于shell脚本没有很多“最佳实践”或“设计模式”。不同的用途有不同的指导方针和偏见-就像任何其他编程语言一样。
Shell脚本是一种设计用于操作文件和进程的语言。虽然它非常适合于此,但不是一种通用的语言,因此始终尝试通过使用现有工具的逻辑来连接代码,而不是在shell脚本中重新创建新的逻辑。
除了这个通用原则外,我还收集了一些常见的shell脚本错误。
知道何时使用它。 对于快速且粗略的命令粘合,这是可以的。如果需要做出任何超过少数非平凡决策、循环等内容,请选择Python、Perl和模块化。
Shell脚本的最大问题通常是最终结果看起来像一个巨大的混乱,在4000行的Bash代码中不断增长......而你无法摆脱它,因为现在你整个项目都依赖于它。当然,它最初只有40行美丽的Bash代码。
今年(2008年)在OSCON上有一个很棒的讨论,内容正是关于这个话题:http://assets.en.oreilly.com/1/event/12/Shell%20Scripting%20Craftsmanship%20Presentation%201.pdf
使用set -e,这样在出现错误后就不会继续执行。如果你希望它可以在非Linux系统上运行,请尝试使其兼容sh而不是仅依赖于bash。
简单易懂: 使用Python代替Shell脚本。 这样可以使可读性提高近100倍,而无需复杂化任何不必要的事情,并保留将脚本的部分演变为函数、对象、持久对象(ZODB)、分布式对象(Pyro)的能力,几乎不需要额外的代码。