编写像下面示例那样的bash脚本有哪些禁忌?您会将变量赋值或函数调用与检查返回代码分开吗?如果是,为什么?如果有的话,这种条件句有什么错呢?
local return_code=0
if username=$(get_username "${user}")
&& is_from_earth "${user}"; then
echo "${username} is an earthling"
else
echo "ERROR: no name or not from the Earth"
return_code=1
fi
本身没有什么问题,但是为了获得更准确的错误消息,我可能会写像
这样的东西local return_code=1
if ! username=$(get_username "$user"); then
echo "ERROR: no such user '$user'" >&2
elif ! is_from_earth "$username"; then
echo "ERROR: $username is not from Earth" >&2
else
echo "$username is an earthing"
return_code=0
fi