[轉(zhuǎn)自]http://www.jb51.net/article/52586.htm
做了小幅修改
本文列舉BASH Shell的一些編程規(guī)范,旨在提高代碼可讀性和健壯性。
- 不可改變的全局變量
- 盡量少用全局變量
- 以大寫命名
- 只讀聲明
- 用全局變量來代替隱晦的$0,$1等
代碼如下:
#以大寫命名只讀全局變量,并使用只讀聲明
readonly PROGNAME=$(basename $0)
#將隱晦的參數(shù)轉(zhuǎn)變?yōu)槿菀桌斫獾淖兞?readonly PROGDIR=$(readlink -m $(dirname $0))
readonly ARGS="$@"
- 一切皆是局部的
所有變量都應(yīng)為局部的。
代碼如下:
change_owner_of_file() {
#自注釋(self documenting)的參數(shù)
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=$@
#通常作為循環(huán)用的變量i,把它聲明為局部變量是很重要的湿故。
local i
for i in $files
do
chown $user:$group $i
done
}
#局部變量不作用于全局域
kfir@goofy ~ $ local abash: local: can only be used in a function
- main()
- 有助于保持所有變量的局部性
- 直觀的函數(shù)式編程
- 代碼中唯一的全局命令是:main
- 有助于單元測試引入測試代碼
#test.sh
main() {
local files="/tmp/a /tmp/b"
local i
for i in $files
do
change_owner_of_file kfir users $i
done
}
if [ $(basename "$0") == "test.sh" ]; then
main
fi
- 一切皆是函數(shù)
- 唯一全局性運(yùn)行的代碼是:
- 不可變的全局變量聲明
- main()函數(shù)
- 保持代碼整潔
- 過程變得清晰
- 有助于單元測試引入測試代碼
例1
main() {
local files=$(ls /tmp | grep pid | grep -v daemon)
}
例2
temporary_files() {
local dir=$1
ls $dir \
| grep pid \
| grep -v daemon
}
main() {
local files=$(temporary_files /tmp)
}
第二個(gè)例子好得多阿趁。查找文件是temporary_files()的問題而非main()的。這段代碼用temporary_files()的單元測試也是可測試的坛猪。
如果你一定要嘗試第一個(gè)例子脖阵,你會得到查找臨時(shí)文件以和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
}
如你所見墅茉,這個(gè)測試不關(guān)心main()命黔。
- 調(diào)試函數(shù)
帶-x標(biāo)志運(yùn)行程序:
bash -x my_prog.sh
只調(diào)試一小段代碼,使用set-x和set+x就斤,會只對被set -x和set +x包含的當(dāng)前代碼打印調(diào)試信息悍募。
temporary_files() {
local dir=$1
set -x
ls $dir \
| grep pid \
| grep -v daemon
set +x
}
打印函數(shù)名和它的參數(shù):
temporary_files() {
echo $FUNCNAME $@
local dir=$1
ls $dir \
| grep pid \
| grep -v daemon
}
調(diào)用函數(shù):
temporary_files /tmp
會打印到標(biāo)準(zhǔn)輸出:
temporary_files /tmp
- 每一行只做一件事
用反斜杠\來作分隔符。例如:
temporary_files() {
local dir=$1
ls $dir | grep pid | grep -v daemon
}
可以寫得簡潔得多:
temporary_files() {
local dir=$1
ls $dir \
| grep pid \
| grep -v daemon
}
- 符號在縮進(jìn)行的開始
符號在行末的壞例子
print_dir_if_not_empty() {
local dir=$1
is_empty $dir && \
echo "dir is empty" || \
echo "dir=$dir"
}
好的例子:我們可以清晰看到行和連接符號之間的聯(lián)系洋机。
print_dir_if_not_empty() {
local dir=$1
is_empty $dir \
&& echo "dir is empty" \
|| echo "dir=$dir"
}
- awk的使用
使用awk讀取文件時(shí)坠宴,需要在讀取文件前面增加文件格式的注釋說明。
#$1=id $2=name
awk '{print $2}' file
- 打印用法
不要這樣做:
echo "this prog does:..."echo "flags:"echo "-h print help"
它應(yīng)該是個(gè)函數(shù):
usage() {
echo "this prog does:..."
echo "flags:"
echo "-h print help"
}
echo在每一行重復(fù)绷旗。因此我們得到了這個(gè)文檔:
usage() {
cat <<- EOF
usage: $PROGNAME options
Program deletes files from filesystems to release space.
It gets config file that define fileystem paths to work on, and whitelist rules to
keep certain files.
OPTIONS:
-c --config configuration file containing the rules. use --help-config to see the syntax.
-n --pretend do not really delete, just how what you are going to do.
-t --test run unit test to check the program
-v --verbose Verbose. You can specify more then one -v to have more verbose
-x --debug debug
-h --help show this help
--help-config configuration help
Examples:
Run all tests:
$PROGNAME --test all
Run specific test:
$PROGNAME --test test_string.sh
Run:
$PROGNAME --config /path/to/config/$PROGNAME.conf
Just show what you are going to do:
$PROGNAME -vn -c /path/to/config/$PROGNAME.conf
EOF
}
注意在每一行的行首應(yīng)該有一個(gè)真正的制表符‘\t'喜鼓。
在vim里,如果你的tab是4個(gè)空格衔肢,你可以用這個(gè)替換命令:
:s/^ /\t/
- 命令行參數(shù)
這里是一個(gè)例子庄岖,完成了上面usage函數(shù)的用法。我從http://kirk.webfinish.com/2009/10/bash-shell-script-to-use-getopts-with-gnu-style-long-positional-parameters/ 得到這段代碼
cmdline() {
# got this idea from here:
# http://kirk.webfinish.com/2009/10/bash-shell-script-to-use-getopts-with-gnu-style-long-positional-parameters/
local arg=
for arg
do
local delim=""
case "$arg" in
#translate --gnu-long-options to -g (short options)
--config) args="${args}-c ";;
--pretend) args="${args}-n ";;
--test) args="${args}-t ";;
--help-config) usage_config && exit 0;;
--help) args="${args}-h ";;
--verbose) args="${args}-v ";;
--debug) args="${args}-x ";;
#pass through anything else
*) [[ "${arg:0:1}" == "-" ]] || delim="\""
args="${args}${delim}${arg}${delim} ";;
esac
done
#Reset the positional parameters to the short options
eval set -- $args
while getopts "nvhxt:c:" OPTION
do
case $OPTION in
v)
readonly VERBOSE=1
;;
h)
usage
exit 0
;;
x)
readonly DEBUG='-x'
set -x
;;
t)
RUN_TESTS=$OPTARG
verbose VINFO "Running tests"
;;
c)
readonly CONFIG_FILE=$OPTARG
;;
n)
readonly PRETEND=1
;;
esac
done
if [[ $recursive_testing || -z $RUN_TESTS ]]; then
[[ ! -f $CONFIG_FILE ]] \
&& eexit "You must provide --config file"
fi
return 0
}
你像這樣角骤,使用我們在頭上定義的不可變的ARGS變量:
main() {
cmdline $ARGS
}
main
- 使用ShellCheck審查代碼
- 在線審查http://www.shellcheck.net/
- 在本地開發(fā)環(huán)境集成審查https://github.com/koalaman/shellcheck#user-content-in-your-editor
- 單元測試
- 在更高級的語言中很重要隅忿。
- 使用shunit2做單元測試
test_config_line_paths() {
local s='partition cpm-all, 80-90,'
returns "/a" "config_line_paths '$s /a, '"
returns "/a /b/c" "config_line_paths '$s /a:/b/c, '"
returns "/a /b /c" "config_line_paths '$s /a : /b : /c, '"
}
config_line_paths() {
local partition_line="$@"
echo $partition_line \
| csv_column 3 \
| delete_spaces \
| column 1 \
| colons_to_spaces
}
source /usr/bin/shunit2
這里是另一個(gè)使用df命令的例子:
DF=df
mock_df_with_eols() {
cat <<- EOF
Filesystem 1K-blocks Used Available Use% Mounted on
/very/long/device/path
124628916 23063572 100299192 19% /
EOF
}
test_disk_size() {
returns 1000 "disk_size /dev/sda1"
DF=mock_df_with_eols
returns 124628916 "disk_size /very/long/device/path"
}
df_column() {
local disk_device=$1
local column=$2
$DF $disk_device \
| grep -v 'Use%' \
| tr '\n' ' ' \
| awk "{print \$$column}"
}
disk_size() {
local disk_device=$1
df_column $disk_device 2
}
這里我有個(gè)例外,為了測試启搂,我在全局域中聲明了DF為非只讀硼控。這是因?yàn)閟hunit2不允許改變?nèi)钟蚝瘮?shù)刘陶。