当您使用 local 时,Bashate linter 会引发错误,但它没有说明如何重写代码以使其通过 linting。
显然,我可以禁用该检查,但这不是重点,我的想法是找到一种以更好的方式编写代码的方法。
来自https://github.com/openstack/kolla-ansible/blob/master/tools/kolla-ansible#L6的示例
function find_base_dir {
local real_path=$(python -c "import os;print(os.path.realpath('$0'))")
local dir_name="$(dirname "$real_path")"
...
}