Skip to content
New issue

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

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

Already on GitHub? # to your account

Validate shell scripts with Shellcheck #418

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft

Validate shell scripts with Shellcheck #418

wants to merge 5 commits into from

Conversation

wjt
Copy link
Member

@wjt wjt commented Oct 18, 2024

Currently all shell scripts found in the tree are at least syntax-checked by bash -n or dash -n based on their shebang.

But as I reviewed #417 I was suddenly gripped with the desire to run them all through shellcheck. This devolved into over-engineering the runner emitting TAP-formatted output and wiring up a TAP parser in Automake.

Having done all this, I think it may be unnecessary, but here's the PR anyway.

wjt added 2 commits October 18, 2024 13:25
Shellcheck says:

```
In eos-live-boot-overlayfs-setup line 17:
	if [ $? != 0 ]; then
             ^-- SC2181 (style): Check exit code directly with e.g. 'if ! mycmd;', not indirectly with $?.

In eos-live-boot-overlayfs-setup line 47:
	if [ $? == 0 ]; then
             ^-- SC2181 (style): Check exit code directly with e.g. 'if mycmd;', not indirectly with $?.
```
Shellcheck says:

```
In eos-live-boot-overlayfs-setup line 24:
	echo ${persistent_loop}
             ^----------------^ SC2086 (info): Double quote to prevent globbing and word splitting.

Did you mean:
	echo "${persistent_loop}"
```

We know this will not be a problem in practice but it's not hard to
placate shellcheck.
@starnight
Copy link
Contributor

Tried to build it on OBS. The shellcheck alarms much more syntax issues.

@wjt
Copy link
Member Author

wjt commented Oct 22, 2024

Interesting – it should be the same version of shellcheck and I added some logic to make the shellcheck check not fatal for certain files.

Previously, shellcheck would warn:

    In eos-firstboot line 9:
    > /var/lib/eos-firstboot
    ^----------------------^ SC2188 (warning): This redirection doesn't have a command. Move to its command (or use 'true' as no-op).

    For more information:
      https://www.shellcheck.net/wiki/SC2188 -- This redirection doesn't have a c...

This is intended to catch cases where the redirect was meant to apply to
the command on the line before.

In this case the usage is correct. Use `:`, which means the same as the
`true` builtin: do nothing, successfully, with no output.
@wjt wjt marked this pull request as draft October 22, 2024 12:42
wjt added 2 commits October 22, 2024 13:44
    In eos-enable-zram line 5:
    if [ $# -lt 1 -o "$1" -lt 0 -o "$1" -gt 1 ]; then
                  ^-- SC2166 (warning): Prefer [ p ] || [ q ] as [ p -o q ] is not well defined.
                                ^-- SC2166 (warning): Prefer [ p ] || [ q ] as [ p -o q ] is not well defined.

    In eos-enable-zram line 17:
        echo $(($ram_size_kb * 3 / 2))K > /sys/block/zram0/disksize
                ^----------^ SC2004 (style): $/${} is unnecessary on arithmetic variables.

    For more information:
      https://www.shellcheck.net/wiki/SC2166 -- Prefer [ p ] || [ q ] as [ p -o q...
      https://www.shellcheck.net/wiki/SC2004 -- $/${} is unnecessary on arithmeti...
@wjt
Copy link
Member Author

wjt commented Oct 22, 2024

Thanks for the OBS test. I don't understand why the offending scripts are not being skipped in OBS.

I think for now I'll pause this and occasionally nudge it forwards by fixing the warnings so that the list of scripts that are expected to fail can be removed.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants