]> git.ipfire.org Git - thirdparty/grub.git/commitdiff
util/bash-completion: Fix SC2207 shellcheck warning
authort.feng <fengtao40@huawei.com>
Tue, 6 Dec 2022 13:49:29 +0000 (21:49 +0800)
committerDaniel Kiper <daniel.kiper@oracle.com>
Wed, 7 Dec 2022 22:38:27 +0000 (23:38 +0100)
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>
util/bash-completion.d/grub-completion.bash.in

index 93d1434808e1b8630c85f13ea376c010f44f2db5..d3399f0e125b20965c2c4f69b471624a3b464a44 100644 (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
+    })
 }
 
 #