In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
这篇文章主要介绍"有哪些让代码整洁、过程清晰的BASH Shell编程技巧",在日常操作中,相信很多人在有哪些让代码整洁、过程清晰的BASH Shell编程技巧问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"有哪些让代码整洁、过程清晰的BASH Shell编程技巧"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
一、不可改变的全局变量
1.尽量少用全局变量
2.以大写命名
3.只读声明
4.用全局变量来代替隐晦的$0,$1等
在我的程序中常使用的全局变量:
代码如下:
readonly PROGNAME=$(basename $0)
readonly PROGDIR=$(readlink -m $(dirname $0))
readonly ARGS="$@"
二、一切皆是局部的
所有变量都应为局部的。
代码如下:
change_owner_of_file() {
local filename=$1
local user=$2
local group=$3
chown $user:$group $filename
}
change_owner_of_files() {
local user=$1; shift
local group=$1; shift
local files=$@
local i
for i in $files
do
chown $user:$group $i
done
}
1.自注释(self documenting)的参数
2.通常作为循环用的变量i,把它声明为局部变量是很重要的。
3.局部变量不作用于全局域。
代码如下:
kfir@goofy ~ $ local a
bash: local: can only be used in a function
三、main()
1.有助于保持所有变量的局部性
2.直观的函数式编程
3.代码中唯一的全局命令是:main
代码如下:
main() {
local files="/tmp/a /tmp/b"
local i
for i in $files
do
change_owner_of_file kfir users $i
done
}
main
四、一切皆是函数
唯一全局性运行的代码是:
- 不可变的全局变量声明
- main()函数
1.保持代码整洁
2.过程变得清晰
代码如下:
main() {
local files=$(ls /tmp | grep pid | grep -v daemon)
}
代码如下:
temporary_files() {
local dir=$1
ls $dir \
| grep pid \
| grep -v daemon
}
main() {
local files=$(temporary_files /tmp)
}
1.第二个例子好得多。查找文件是temporary_files()的问题而非main()的。这段代码用temporary_files()的单元测试也是可测试的。
2.如果你一定要尝试第一个例子,你会得到查找临时文件以和main算法的大杂烩。
代码如下:
test_temporary_files() {
local dir=/tmp
touch $dir/a-pid1232.tmp
touch $dir/a-pid1232-daemon.tmp
returns "$dir/a-pid1232.tmp" temporary_files $dir
touch $dir/b-pid1534.tmp
returns "$dir/a-pid1232.tmp $dir/b-pid1534.tmp" temporary_files $dir
}
如你所见,这个测试不关心main()。
五、调试函数
带-x标志运行程序:
代码如下:
bash -x my_prog.sh
只调试一小段代码,使用set-x和set+x,会只对被set -x和set +x包含的当前代码打印调试信息。
代码如下:
temporary_files() {
local dir=$1
set -x
ls $dir \
| grep pid \
| grep -v daemon
set +x
}
打印函数名和它的参数:
代码如下:
temporary_files() {
echo $FUNCNAME $@
local dir=$1
ls $dir \
| grep pid \
| grep -v daemon
}
调用函数:
代码如下:
temporary_files /tmp
会打印到标准输出:
代码如下:
temporary_files /tmp
六、代码的清晰度
这段代码做了什么?
代码如下:
main() {
local dir=/tmp
[[ -z $dir ]] \
&& do_something...
[[ -n $dir ]] \
&& do_something...
[[ -f $dir ]] \
&& do_something...
[[ -d $dir ]] \
&& do_something...
}
main
让你的代码说话:
代码如下:
is_empty() {
local var=$1
[[ -z $var ]]
}
is_not_empty() {
local var=$1
[[ -n $var ]]
}
is_file() {
local file=$1
[[ -f $file ]]
}
is_dir() {
local dir=$1
[[ -d $dir ]]
}
main() {
local dir=/tmp
is_empty $dir \
&& do_something...
is_not_empty $dir \
&& do_something...
is_file $dir \
&& do_something...
is_dir $dir \
&& do_something...
}
main
七、每一行只做一件事
用反斜杠\来作分隔符。例如:
代码如下:
temporary_files() {
local dir=$1
ls $dir | grep pid | grep -v daemon
}
可以写得简洁得多:
代码如下:
temporary_files() {
local dir=$1
ls $dir \
| grep pid \
| grep -v daemon
}
符号在缩进行的开始
符号在行末的坏例子:(译注:原文在此例中用了temporary_files()代码段,疑似是贴错了。结合上下文,应为print_dir_if_not_empty())
代码如下:
print_dir_if_not_empty() {
local dir=$1
is_empty $dir && \
echo "dir is empty" || \
echo "dir=$dir"
}
好的例子:我们可以清晰看到行和连接符号之间的联系。
代码如下:
print_dir_if_not_empty() {
local dir=$1
is_empty $dir \
&& echo "dir is empty" \
|| echo "dir=$dir"
}
八、打印用法
不要这样做:
代码如下:
echo "this prog does:..."
echo "flags:"
echo "-h print help"
它应该是个函数:
代码如下:
usage() {
echo "this prog does:..."
echo "flags:"
echo "-h print help"
}
echo在每一行重复。因此我们得到了这个文档:
代码如下:
usage() {
cat
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.