Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor fixes to pass shellcheck #14996

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

miervasi-edalab
Copy link

These are just two minor patches to the following two scripts that comes with the base-files package to solve errors given by the ShellCheck utility:

  • /bin/board_detect
  • /sbin/firstboot

The first script contained parsing of the ls command which is not recommended:

In shell scripting, the use of command expansion - for looping over a set of files - is not
recommended. Command expansion, in fact, may cause word splitting. This issue refers to the
process by which the shell splits a string into separate words based on whitespace or other
delimiters. Word splitting can cause problems for certain filenames.

While the second script contained potential re-splitting:

In shell scripting, "splitting" refers to the process by which the shell splits a string into
separate words based on whitespace or other delimiters. This can happen when a variable is
expanded. For example, if an array's string item contains the whitespaces, each resulting
substring will be considered as a different array's element. This can cause several runtime
issues and bugs.

@github-actions github-actions bot added the core packages pull request/issue for core (in-tree) packages label Mar 27, 2024
@@ -5,8 +5,8 @@ CFG=$1
[ -n "$CFG" ] || CFG=/etc/board.json

[ -d "/etc/board.d/" -a ! -s "$CFG" ] && {
for a in $(ls /etc/board.d/*); do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I agree with this change, is there any reason why someone would want to do ls <some glob>? Probably this is done because ls guarantees that they are sorted which makes sense in this context.

Copy link
Contributor

@rany2 rany2 Mar 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P.S. it does seem like echo /etc/board.d/* is sorted, but the ls man page explicitly mentions that the output would be sorted by default. I'm not sure if we can rely on it for glob.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The commit message for when this was introduced didn't shed any light on this. I think @rany2 almost certainly has the right of it though. $(ls /etc/board.d/*) is used since it gives the files in sorted order. A favorite approach is to name the scripts 0-first-script.sh, 1-second-script.sh, 2-third-script.sh, etc in order to ensure ordering of the scripts. Same idea as patch filenames starting with 3 digits. As a result this first line is almost certainly wrong.

Copy link
Contributor

@ehem ehem left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think @rany2 almost certainly has the situation correct, which means a fix is needed.

@@ -1,3 +1,3 @@
#!/bin/sh

/sbin/jffs2reset $@
/sbin/jffs2reset "$@"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First commit looks simple and reasonable. The subject line (first line used by shortlog) should be prefixed with "base-files:".

for a in $(ls /etc/board.d/*); do
[ -s $a ] || continue;
for a in /etc/board.d/*; do
[ -s "$a" ] || continue;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even though it is unlikely for files in /etc/board.d/ to have spaces in their filenames, this is quite appropriate.

@@ -5,8 +5,8 @@ CFG=$1
[ -n "$CFG" ] || CFG=/etc/board.json

[ -d "/etc/board.d/" -a ! -s "$CFG" ] && {
for a in $(ls /etc/board.d/*); do
[ -s $a ] || continue;
for a in /etc/board.d/*; do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would be awfully tempted to substitute find /etc/board.d -maxdepth 1 -print | sort | while read a; do as this avoids having the shell scan the directory.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which would not yield equivalent results though, as it returns /etc/board.d at depth 0 as well, so it should probably be -mindepth 1 -maxdepth 1.

While generally preferring find for finding operations, the line gets pretty unhandy here and would also depend on different flavors of find and sort (BusyBox, findutils, coreutils) installed on the target. (though they all should behave the same in this case). I'd therefore slightly prefer the shorter glob version here.

@@ -5,8 +5,8 @@ CFG=$1
[ -n "$CFG" ] || CFG=/etc/board.json

[ -d "/etc/board.d/" -a ! -s "$CFG" ] && {
for a in $(ls /etc/board.d/*); do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The commit message for when this was introduced didn't shed any light on this. I think @rany2 almost certainly has the right of it though. $(ls /etc/board.d/*) is used since it gives the files in sorted order. A favorite approach is to name the scripts 0-first-script.sh, 1-second-script.sh, 2-third-script.sh, etc in order to ensure ordering of the scripts. Same idea as patch filenames starting with 3 digits. As a result this first line is almost certainly wrong.

@stklcode
Copy link
Contributor

Fix shellcheck issue in firstboot script
Fix board_detect script to pass shellcheck

The actual "issue" is not "not passing shellcheck". The tool just detects potential issues like unintended behavior. And the solution is not # shellcheck disable=... here. So I'd rather point to the actual changes in the commit message than just blaming the tool.

@miervasi-edalab
Copy link
Author

Fix shellcheck issue in firstboot script
Fix board_detect script to pass shellcheck

The actual "issue" is not "not passing shellcheck". The tool just detects potential issues like unintended behavior. And the solution is not # shellcheck disable=... here. So I'd rather point to the actual changes in the commit message than just blaming the tool.

Ok I'll rephrase the commits then. What about the file loop? I've understood that we want the files to be sorted while the glob in fact doesn't guarantee that, but the alternative then is to let the line unchanged or to use find.

Before this change, the firstboot script was not
passing shellcheck test as it invoked jffs2reset
without quotes, leading to potential re-splitting.

Signed-off-by: Matteo Iervasi <matteo.iervasi@edalab.it>
Before this change, the board_detect script was
parsing the output of ls to get files in /etc/board.d.
The correct method is to use globs.

Signed-off-by: Matteo Iervasi <matteo.iervasi@edalab.it>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core packages pull request/issue for core (in-tree) packages
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants