util/bash-completion: Fix SC2207 shellcheck warning

SC2207 (warning): Prefer mapfile or read -a to split
command output (or quote to avoid splitting).

In grub-completion.bash.in line 56:
        COMPREPLY=($(compgen -P "${2-}" -W "${1-}" -S "${4-}" -- "$cur"))
                   ^-- SC2207 (warning)

In grub-completion.bash.in line 119:
        COMPREPLY=( $(compgen \
                    ^-- SC2207 (warning)

In grub-completion.bash.in line 128:
    COMPREPLY=( $( compgen -f -X '!*/*.mod' -- "${grub_dir}/$cur" | {
                ^-- SC2207 (warning)

COMPREPLY=($(command)) are doing unquoted command expansion in an array.
This will invoke the shell's sloppy word splitting and glob expansion.

If we want to split the output into lines or words, use read -r and
loops will be better. This prevents the shell from doing unwanted
splitting and glob expansion, and therefore avoiding problems with
output containing spaces or special characters.

More: https://github.com/koalaman/shellcheck/wiki/SC2207

Signed-off-by: t.feng <fengtao40@huawei.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
This commit is contained in:
t.feng 2022-12-06 21:49:29 +08:00 committed by Daniel Kiper
parent 2029c4822b
commit 61e4f408b0

View File

@ -52,8 +52,11 @@ __grubcomp () {
COMPREPLY=()
;;
*)
local IFS=' '$'\t'$'\n'
COMPREPLY=($(compgen -P "${2-}" -W "${1-}" -S "${4-}" -- "$cur"))
local line IFS=' '$'\t'$'\n'
COMPREPLY=()
while read -r line; do
COMPREPLY+=("${line}")
done < <(compgen -P "${2-}" -W "${1-}" -S "${4-}" -- "$cur")
;;
esac
}
@ -115,25 +118,30 @@ __grub_list_menuentries () {
local config_file=$(__grub_dir)/grub.cfg
if [ -f "$config_file" ];then
local IFS=$'\n'
COMPREPLY=( $(compgen \
-W "$( awk -F "[\"']" '/menuentry/ { print $2 }' $config_file )" \
-- "$cur" )) #'# Help emacs syntax highlighting
local line IFS=$'\n'
COMPREPLY=()
while read -r line; do
COMPREPLY+=("${line}")
done < <(compgen \
-W "$( awk -F "[\"']" '/menuentry/ { print $2 }' $config_file )" \
-- "$cur" ) #'# Help emacs syntax highlighting
fi
}
__grub_list_modules () {
local grub_dir=$(__grub_dir)
local IFS=$'\n'
COMPREPLY=( $( compgen -f -X '!*/*.mod' -- "${grub_dir}/$cur" | {
while read -r tmp; do
[ -n "$tmp" ] && {
tmp=${tmp##*/}
printf '%s\n' ${tmp%.mod}
}
done
}
))
local line tmp IFS=$'\n'
COMPREPLY=()
while read -r line; do
COMPREPLY+=("${line}")
done < <(compgen -f -X '!*/*.mod' -- "${grub_dir}/$cur" | {
while read -r tmp; do
[ -n "$tmp" ] && {
tmp=${tmp##*/}
printf '%s\n' ${tmp%.mod}
}
done
})
}
#