Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1p02tg-0005jf-Bb for mharc-grub-devel@gnu.org; Tue, 29 Nov 2022 10:51:48 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1p02td-0005jJ-Jt for grub-devel@gnu.org; Tue, 29 Nov 2022 10:51:45 -0500 Received: from szxga03-in.huawei.com ([45.249.212.189]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1p02ta-00057F-Cl for grub-devel@gnu.org; Tue, 29 Nov 2022 10:51:45 -0500 Received: from dggemv703-chm.china.huawei.com (unknown [172.30.72.54]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4NM6Dt4XYMzJnyr for ; Tue, 29 Nov 2022 23:48:14 +0800 (CST) Received: from kwepemm600019.china.huawei.com (7.193.23.64) by dggemv703-chm.china.huawei.com (10.3.19.46) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Tue, 29 Nov 2022 23:51:37 +0800 Received: from [10.136.112.228] (10.136.112.228) by kwepemm600019.china.huawei.com (7.193.23.64) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Tue, 29 Nov 2022 23:51:36 +0800 Subject: Re: [PATCH 2/2] bash-completion:fix shellcheck warning To: Daniel Kiper , References: <20220919012014.499082-1-fengtao40@huawei.com> <20220919012014.499082-3-fengtao40@huawei.com> <20221123160953.n7mp3uzfokt4fiel@tomti.i.net-space.pl> From: "Fengtao (fengtao, Euler)" CC: Message-ID: <2ecb590b-9132-1f8a-cd10-4601945d8f91@huawei.com> Date: Tue, 29 Nov 2022 23:51:35 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.6.0 MIME-Version: 1.0 In-Reply-To: <20221123160953.n7mp3uzfokt4fiel@tomti.i.net-space.pl> Content-Type: text/plain; charset="utf-8" Content-Language: en-US Content-Transfer-Encoding: 7bit X-Originating-IP: [10.136.112.228] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To kwepemm600019.china.huawei.com (7.193.23.64) X-CFilter-Loop: Reflected Received-SPF: pass client-ip=45.249.212.189; envelope-from=fengtao40@huawei.com; helo=szxga03-in.huawei.com X-Spam_score_int: -44 X-Spam_score: -4.5 X-Spam_bar: ---- X-Spam_report: (-4.5 / 5.0 requ) BAYES_00=-1.9, NICE_REPLY_A=-0.258, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 29 Nov 2022 15:51:46 -0000 On 2022/11/24 0:09, Daniel Kiper wrote: > On Mon, Sep 19, 2022 at 09:20:14AM +0800, t.feng via Grub-devel wrote: >> SC2207 (warning): Prefer mapfile or read -a to split >> command output (or quote to avoid splitting). >> SC2120 (warning): __grub_get_options_from_help references arguments, >> but none are ever passed. >> SC2155 (warning): Declare and assign separately to avoid >> masking return values. >> >> In grub-completion.bash.in line 56: >> COMPREPLY=($(compgen -P "${2-}" -W "${1-}" -S "${4-}" -- >> "$cur")) >> ^-- SC2207 (warning) >> >> In grub-completion.bash.in line 63: >> __grub_get_options_from_help () { >> ^-- SC2120 (warning) >> >> In grub-completion.bash.in line 115: >> local config_file=$(__grub_dir)/grub.cfg >> ^---------^ SC2155 (warning) >> >> In grub-completion.bash.in line 119: >> COMPREPLY=( $(compgen \ >> ^-- SC2207 (warning) >> >> In grub-completion.bash.in line 126: >> local grub_dir=$(__grub_dir) >> ^------^ SC2155 (warning) >> >> In grub-completion.bash.in line 128: >> COMPREPLY=( $( compgen -f -X '!*/*.mod' -- "${grub_dir}/$cur" | { >> ^-- SC2207 (warning) >> >> SC2120: the current code meets the exception and does not need to be >> modified >> >> ref:https://github.com/koalaman/shellcheck/wiki/SC2207 >> ref:https://github.com/koalaman/shellcheck/wiki/SC2120 >> ref:https://github.com/koalaman/shellcheck/wiki/SC2155 > > I think this should be split into three patches. > > And again missing SOB... > ok, I will fix this. >> --- >> .../bash-completion.d/grub-completion.bash.in | 40 ++++++++++++------- >> 1 file changed, 25 insertions(+), 15 deletions(-) >> >> diff --git a/util/bash-completion.d/grub-completion.bash.in b/util/bash-completion.d/grub-completion.bash.in >> index 93d143480..7449e629a 100644 >> --- a/util/bash-completion.d/grub-completion.bash.in >> +++ b/util/bash-completion.d/grub-completion.bash.in >> @@ -53,7 +53,10 @@ __grubcomp () { >> ;; >> *) >> local IFS=' '$'\t'$'\n' >> - COMPREPLY=($(compgen -P "${2-}" -W "${1-}" -S "${4-}" -- "$cur")) >> + COMPREPLY=() >> + while read -r line; do >> + COMPREPLY+=("${line}") >> + done < <(compgen -P "${2-}" -W "${1-}" -S "${4-}" -- "$cur") >> ;; >> esac >> } >> @@ -112,28 +115,35 @@ __grub_get_last_option () { >> >> __grub_list_menuentries () { >> local cur="${COMP_WORDS[COMP_CWORD]}" >> - local config_file=$(__grub_dir)/grub.cfg >> + local config_file >> + 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 >> + COMPREPLY=() >> + while read -r line; do > > This looks strange. The shellcheck suggest "read -a" and you use "read -r" here. > Could you explain that? > I print the compgen -W "$( awk -F "[\"']" '/menuentry/ { print $2 }' $config_file )" -- "$cur" It was multiple lines like: --id EulerOS (4.19.90-vhulk2111.1.0.h889.eulerosv2r10.aarch64) 2.0 (SP10) EulerOS (0-rescue) 2.0 (SP10) System setup So, ref to: https://github.com/koalaman/shellcheck/wiki/SC2207, if it outputs a line with multiple words, it should use read -a and if it outputs multiple lines, we can use mapfile or read -r with a loop. Anyway, we can make a easy test: # IFS='\n' read -r -a array <<< "$(echo -e "1\n2\n3\n")" # echo ${array[*]} # 1 <<< this is incorrect --------------------------------------- # array=();while IFS='\n' read -r line; do array+=("$line"); done < <(echo -e "1\n2\n3") # echo ${array[*]} # 1 2 3 <<< this is right Final, SC2207 said use mapfile in bash 4.4+, and read -r with a loop will be ok in bash 3.x+ So I choice read -r.(I did not test mapfile) >> + 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 grub_dir >> + 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 >> - } >> - )) >> + COMPREPLY=() >> + while read -r line; do >> + COMPREPLY+=("${line}") >> + done < <(compgen -f -X '!*/*.mod' -- "${grub_dir}/$cur" | { >> + while read -r tmp; do >> + [ -n "$tmp "] && { > > It seems to me last '"' is in wrong place. > I will fix this :) >> + tmp=${tmp##*/} >> + printf '%s\n' ${tmp%.mod} >> + } >> + done >> + }) >> } > > Daniel > > _______________________________________________ > Grub-devel mailing list > Grub-devel@gnu.org > https://lists.gnu.org/mailman/listinfo/grub-devel >