From: t.feng Date: Tue, 6 Dec 2022 13:49:29 +0000 (+0800) Subject: util/bash-completion: Fix SC2207 shellcheck warning X-Git-Tag: grub-2.12-rc1~191 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=61e4f408b08d49c24cc2f183078ef687cf8e5bc5;p=thirdparty%2Fgrub.git 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 Reviewed-by: Daniel Kiper --- diff --git a/util/bash-completion.d/grub-completion.bash.in b/util/bash-completion.d/grub-completion.bash.in index 93d143480..d3399f0e1 100644 --- a/util/bash-completion.d/grub-completion.bash.in +++ b/util/bash-completion.d/grub-completion.bash.in @@ -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 + }) } #