tools: tests: fix a few issues in bash scripts
v2 cover tools: tests: fix a few issues in bash scripts
Fix a few issues with tools tests reported by Andy.
---
Changes in v2:
- Use double quotes around $@ to prevent globbing but allow variable expansion
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Bartosz Golaszewski (4):
tools: tests: use tabs for indentation consistently
tools: tests: use "$@" instead of $*
tools: tests: remove unneeded ';' in while loops
tools: tests: remove dependency on grep
tools/gpio-tools-test.bash | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-)
---
base-commit: 27fe10150f6d5fa78d1a1ef1e922dc8395d1154d
change-id: 20240524-fix-bash-tests-545bf26447b3
Best regards,
Signature · 1 line
Bartosz Golaszewski <[email protected]>
PATCH v2 1/4 tools: tests: use tabs for indentation consistently
From: Bartosz Golaszewski <[email protected]>
Replace all spaces used for indentation with tabs.
--- tools/gpio-tools-test.bash | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
tools/gpio-tools-test.bash +7 -7
@@ -203,9 +203,9 @@ dut_run_redirect() { dut_read_redirect() { output=$(<$SHUNIT_TMPDIR/$DUT_OUTPUT) - local ORIG_IFS="$IFS" - IFS=$'\n' lines=($output) - IFS="$ORIG_IFS" + local ORIG_IFS="$IFS" + IFS=$'\n' lines=($output) + IFS="$ORIG_IFS" } dut_read() {
@@ -269,12 +269,12 @@ dut_wait() { } dut_cleanup() { - if [ -n "$DUT_PID" ] - then + if [ -n "$DUT_PID" ] + then kill -SIGTERM $DUT_PID 2> /dev/null wait $DUT_PID || false - fi - rm -f $SHUNIT_TMPDIR/$DUT_OUTPUT + fi + rm -f $SHUNIT_TMPDIR/$DUT_OUTPUT } tearDown() {
Signature · 1 line
2.43.0
PATCH v2 2/4 tools: tests: use "$@" instead of $*
In reply to Bartosz GolaszewskiFrom: Bartosz Golaszewski <[email protected]>
$@ does not break up quoted arguments which is what we want in all cases in the bash test-suite. Use it instead of $*. While at it: prevent globbing with double quotes but allow variable expansion.
--- tools/gpio-tools-test.bash | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
tools/gpio-tools-test.bash +6 -6
@@ -27,10 +27,10 @@ GPIOSIM_APP_NAME="gpio-tools-test" MIN_KERNEL_VERSION="5.17.4" MIN_SHUNIT_VERSION="2.1.8" -# Run the command in $* and fail the test if the command succeeds. +# Run the command in $@ and fail the test if the command succeeds. assert_fail() { - $* || return 0 - fail " '$*': command did not fail as expected" + "$@" || return 0 + fail " '$@': command did not fail as expected" } # Check if the string in $2 matches against the pattern in $1.
@@ -71,7 +71,7 @@ gpiosim_chip() { mkdir -p $BANKPATH - for ARG in $* + for ARG in "$@" do local KEY=$(echo $ARG | cut -d"=" -f1) local VAL=$(echo $ARG | cut -d"=" -f2)
@@ -253,7 +253,7 @@ dut_regex_match() { } dut_write() { - echo $* >&${COPROC[1]} + echo "$@" >&${COPROC[1]} } dut_kill() {
@@ -283,7 +283,7 @@ tearDown() { } request_release_line() { - $SOURCE_DIR/gpioget -c $* >/dev/null + $SOURCE_DIR/gpioget -c "$@" >/dev/null } #
Signature · 1 line
2.43.0
Re: [PATCH libgpiod v2 2/4] tools: tests: use "$@" instead of $*
Quoted 19 lines · Bartosz Golaszewski
From: Bartosz Golaszewski <[email protected]>
$@ does not break up quoted arguments which is what we want in all cases in the bash test-suite. Use it instead of $*. While at it: prevent globbing with double quotes but allow variable expansion.
Suggested-by Andy Shevchenko <[email protected]>Signed-off-by Bartosz Golaszewski <[email protected]>--- tools/gpio-tools-test.bash | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)tools/gpio-tools-test.bash +0
-0@@ -27,10 +27,10 @@ GPIOSIM_APP_NAME="gpio-tools-test" MIN_KERNEL_VERSION="5.17.4" MIN_SHUNIT_VERSION="2.1.8"
tools/gpio-tools-test.bash GPIOSIM_APP_NAME="gpio-tools-test" · line 35
Quoted 10 lines
tools/gpio-tools-test.bash +3
-3-# Run the command in $* and fail the test if the command succeeds. +# Run the command in $@ and fail the test if the command succeeds. assert_fail() { - $* || return 0 - fail " '$*': command did not fail as expected" + "$@" || return 0 + fail " '$@': command did not fail as expected" }
Ironically, shellcheck doesn't like the '$@' in the fail string[1], so you should use $* there.
It also doesn't like looping on find results in patch 4[2], though that is not related to your change, so leave it and I'll fix it later?
Cheers,
Kent.
[1] https://www.shellcheck.net/wiki/SC2145
[2] https://www.shellcheck.net/wiki/SC2044
Re: [PATCH libgpiod v2 2/4] tools: tests: use "$@" instead of $*
Quoted 35 lines · Kent Gibson
Quoted 19 lines · Bartosz Golaszewski
From: Bartosz Golaszewski <[email protected]>
$@ does not break up quoted arguments which is what we want in all cases in the bash test-suite. Use it instead of $*. While at it: prevent globbing with double quotes but allow variable expansion.
Suggested-by Andy Shevchenko <[email protected]>Signed-off-by Bartosz Golaszewski <[email protected]>--- tools/gpio-tools-test.bash | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)tools/gpio-tools-test.bash +0
-0@@ -27,10 +27,10 @@ GPIOSIM_APP_NAME="gpio-tools-test" MIN_KERNEL_VERSION="5.17.4" MIN_SHUNIT_VERSION="2.1.8"tools/gpio-tools-test.bash GPIOSIM_APP_NAME="gpio-tools-test" · line 35
Quoted 10 lines
tools/gpio-tools-test.bash +3
-3-# Run the command in $* and fail the test if the command succeeds. +# Run the command in $@ and fail the test if the command succeeds. assert_fail() { - $* || return 0 - fail " '$*': command did not fail as expected" + "$@" || return 0 + fail " '$@': command did not fail as expected" }Ironically, shellcheck doesn't like the '$@' in the fail string[1], so you should use $* there.
Quoted 3 lines
It also doesn't like looping on find results in patch 4[2], though that is not related to your change, so leave it and I'll fix it later?
What does it want here? This looks correct to me? Should we do "$(find...)"?
Bart
Quoted 5 lines
Cheers,
Kent.[1] https://www.shellcheck.net/wiki/SC2145
[2] https://www.shellcheck.net/wiki/SC2044
Re: [PATCH libgpiod v2 2/4] tools: tests: use "$@" instead of $*
Quoted 36 lines · Bartosz Golaszewski
Quoted 35 lines · Kent Gibson
Quoted 19 lines · Bartosz Golaszewski
From: Bartosz Golaszewski <[email protected]>
$@ does not break up quoted arguments which is what we want in all cases in the bash test-suite. Use it instead of $*. While at it: prevent globbing with double quotes but allow variable expansion.
Suggested-by Andy Shevchenko <[email protected]>Signed-off-by Bartosz Golaszewski <[email protected]>--- tools/gpio-tools-test.bash | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)tools/gpio-tools-test.bash +0
-0@@ -27,10 +27,10 @@ GPIOSIM_APP_NAME="gpio-tools-test" MIN_KERNEL_VERSION="5.17.4" MIN_SHUNIT_VERSION="2.1.8"tools/gpio-tools-test.bash GPIOSIM_APP_NAME="gpio-tools-test" · line 35
Quoted 10 lines
tools/gpio-tools-test.bash +3
-3-# Run the command in $* and fail the test if the command succeeds. +# Run the command in $@ and fail the test if the command succeeds. assert_fail() { - $* || return 0 - fail " '$*': command did not fail as expected" + "$@" || return 0 + fail " '$@': command did not fail as expected" }Ironically, shellcheck doesn't like the '$@' in the fail string[1], so you should use $* there.
Quoted 6 lines
Quoted 3 lines
It also doesn't like looping on find results in patch 4[2], though that is not related to your change, so leave it and I'll fix it later?
What does it want here? This looks correct to me? Should we do "$(find...)"?
Refer to the referenced link - it is worried about filenames containing whitespace.
Not sure what the best option is here - I am only just looking into it...
Kent.
Quoted 7 lines
Bart
Quoted 5 lines
Cheers,
Kent.[1] https://www.shellcheck.net/wiki/SC2145
[2] https://www.shellcheck.net/wiki/SC2044
Re: [PATCH libgpiod v2 2/4] tools: tests: use "$@" instead of $*
Quoted 41 lines · Kent Gibson
Quoted 40 lines · Bartosz Golaszewski
Quoted 38 lines · Kent Gibson
Quoted 19 lines · Bartosz Golaszewski
From: Bartosz Golaszewski <[email protected]>
$@ does not break up quoted arguments which is what we want in all cases in the bash test-suite. Use it instead of $*. While at it: prevent globbing with double quotes but allow variable expansion.
Suggested-by Andy Shevchenko <[email protected]>Signed-off-by Bartosz Golaszewski <[email protected]>--- tools/gpio-tools-test.bash | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)tools/gpio-tools-test.bash +0
-0@@ -27,10 +27,10 @@ GPIOSIM_APP_NAME="gpio-tools-test" MIN_KERNEL_VERSION="5.17.4" MIN_SHUNIT_VERSION="2.1.8"tools/gpio-tools-test.bash GPIOSIM_APP_NAME="gpio-tools-test" · line 35
Quoted 10 lines
tools/gpio-tools-test.bash +3
-3-# Run the command in $* and fail the test if the command succeeds. +# Run the command in $@ and fail the test if the command succeeds. assert_fail() { - $* || return 0 - fail " '$*': command did not fail as expected" + "$@" || return 0 + fail " '$@': command did not fail as expected" }Ironically, shellcheck doesn't like the '$@' in the fail string[1], so you should use $* there.
It also doesn't like looping on find results in patch 4[2], though that is not related to your change, so leave it and I'll fix it later?
Quoted 7 lines
Quoted 2 lines
What does it want here? This looks correct to me? Should we do "$(find...)"?
Refer to the referenced link - it is worried about filenames containing whitespace.
Not sure what the best option is here - I am only just looking into it...
How about using this for the cleanup:
echo 0 > "$DEVPATH/live"
find "$DEVPATH" -type d -name hog -exec rmdir '{}' '+'
find "$DEVPATH" -type d -name "line*" -exec rmdir '{}' '+'
find "$DEVPATH" -type d -name "bank*" -exec rmdir '{}' '+'
rmdir "$DEVPATH"It is a bit less subtle, but that works for me.
Cheers,
Kent.
Re: [PATCH libgpiod v2 2/4] tools: tests: use "$@" instead of $*
Quoted 53 lines · Kent Gibson
Quoted 41 lines · Kent Gibson
Quoted 40 lines · Bartosz Golaszewski
Quoted 38 lines · Kent Gibson
Quoted 19 lines · Bartosz Golaszewski
From: Bartosz Golaszewski <[email protected]>
$@ does not break up quoted arguments which is what we want in all cases in the bash test-suite. Use it instead of $*. While at it: prevent globbing with double quotes but allow variable expansion.
Suggested-by Andy Shevchenko <[email protected]>Signed-off-by Bartosz Golaszewski <[email protected]>--- tools/gpio-tools-test.bash | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)tools/gpio-tools-test.bash +0
-0@@ -27,10 +27,10 @@ GPIOSIM_APP_NAME="gpio-tools-test" MIN_KERNEL_VERSION="5.17.4" MIN_SHUNIT_VERSION="2.1.8"tools/gpio-tools-test.bash GPIOSIM_APP_NAME="gpio-tools-test" · line 35
Quoted 10 lines
tools/gpio-tools-test.bash +3
-3-# Run the command in $* and fail the test if the command succeeds. +# Run the command in $@ and fail the test if the command succeeds. assert_fail() { - $* || return 0 - fail " '$*': command did not fail as expected" + "$@" || return 0 + fail " '$@': command did not fail as expected" }Ironically, shellcheck doesn't like the '$@' in the fail string[1], so you should use $* there.
It also doesn't like looping on find results in patch 4[2], though that is not related to your change, so leave it and I'll fix it later?
Quoted 7 lines
Quoted 2 lines
What does it want here? This looks correct to me? Should we do "$(find...)"?
Refer to the referenced link - it is worried about filenames containing whitespace.
Not sure what the best option is here - I am only just looking into it...How about using this for the cleanup:
Quoted 8 lines
echo 0 > "$DEVPATH/live"
find "$DEVPATH" -type d -name hog -exec rmdir '{}' '+' find "$DEVPATH" -type d -name "line*" -exec rmdir '{}' '+' find "$DEVPATH" -type d -name "bank*" -exec rmdir '{}' '+' rmdir "$DEVPATH"It is a bit less subtle, but that works for me.
Looks good and works fine. I'll use it, thanks a lot!
Bart
Re: [PATCH libgpiod v2 2/4] tools: tests: use "$@" instead of $*
In reply to Kent GibsonQuoted 1 line · Kent Gibson
On Mon, May 27, 2024 at 02:02:34PM +0200, Bartosz Golaszewski wrote:
...
Quoted 9 lines
tools/gpio-tools-test.bash GPIOSIM_APP_NAME="gpio-tools-test" · line 34
Quoted 6 lines
assert_fail(){tools/gpio-tools-test.bash +2
-2- $* || return 0 - fail " '$*': command did not fail as expected" + "$@" || return 0 + fail " '$@': command did not fail as expected" }Ironically, shellcheck doesn't like the '$@' in the fail string[1], so you should use $* there.
But why does it do like this?
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH libgpiod v2 2/4] tools: tests: use "$@" instead of $*
Quoted 15 lines · Andy Shevchenko
Quoted 1 line · Kent Gibson
On Mon, May 27, 2024 at 02:02:34PM +0200, Bartosz Golaszewski wrote:
...
Quoted 9 lines
tools/gpio-tools-test.bash GPIOSIM_APP_NAME="gpio-tools-test" · line 34
Quoted 6 lines
assert_fail(){tools/gpio-tools-test.bash +2
-2- $* || return 0 - fail " '$*': command did not fail as expected" + "$@" || return 0 + fail " '$@': command did not fail as expected" }Ironically, shellcheck doesn't like the '$@' in the fail string[1], so you should use $* there.
Quoted 2 lines
But why does it do like this?
Read the link[1].
Because $@ is an array being used to build a string, and that may not work the way you expect. In this case $* is clearer as that has already been concatenated.
Cheers,
Kent.
Re: [PATCH libgpiod v2 2/4] tools: tests: use "$@" instead of $*
Quoted 3 lines · Kent Gibson
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Kent Gibson
On Mon, May 27, 2024 at 02:02:34PM +0200, Bartosz Golaszewski wrote:
...
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
tools/gpio-tools-test.bash GPIOSIM_APP_NAME="gpio-tools-test" · line 34
Quoted 6 lines
assert_fail(){tools/gpio-tools-test.bash +2
-2- $* || return 0 - fail " '$*': command did not fail as expected" + "$@" || return 0 + fail " '$@': command did not fail as expected" }Ironically, shellcheck doesn't like the '$@' in the fail string[1], so you should use $* there.
Quoted 3 lines
Quoted 1 line
But why does it do like this?
Read the link[1].
Okay, this is only for some debug / error messages. Still if one wants to have clear understanding on what has been passed to some function, $* is not a correct option. Also note the single quotes, shouldn't that protect from the arguments loss?
Quoted 2 lines
Because $@ is an array being used to build a string, and that may not work the way you expect.
I think it's the opposite, $* works in a way I do not expect :-)
Quoted 2 lines
In this case $* is clearer as that has already
been concatenated.
...loosing information about which word refers to which argument, yes.
Quoted 1 line
TL;DR: I consider this is still a bug in shellcheck. But if you rely on the tool as on the ruleset carved in stone, I will not die. Just a remark to myself "even honourable tools may also be broken".
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH libgpiod v2 2/4] tools: tests: use "$@" instead of $*
Quoted 17 lines · Andy Shevchenko
Quoted 3 lines · Kent Gibson
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Kent Gibson
On Mon, May 27, 2024 at 02:02:34PM +0200, Bartosz Golaszewski wrote:
...
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
tools/gpio-tools-test.bash GPIOSIM_APP_NAME="gpio-tools-test" · line 34
Quoted 6 lines
assert_fail(){tools/gpio-tools-test.bash +2
-2- $* || return 0 - fail " '$*': command did not fail as expected" + "$@" || return 0 + fail " '$@': command did not fail as expected" }Ironically, shellcheck doesn't like the '$@' in the fail string[1], so you should use $* there.
Quoted 9 lines
Quoted 3 lines
Quoted 1 line
But why does it do like this?
Read the link[1].
Okay, this is only for some debug / error messages. Still if one wants to have clear understanding on what has been passed to some function, $* is not a correct option. Also note the single quotes, shouldn't that protect from the arguments loss?
That's right - I was only referring to this particular case where a string is being constructed. Wasn't that clear?
The single quotes are within double quotes, so aren't they just part of the text in this context?
Quoted 5 lines
Quoted 2 lines
Because $@ is an array being used to build a string, and that may not work the way you expect.
I think it's the opposite, $* works in a way I do not expect :-)
When passing arguments, sure. Not when constructing strings.
Quoted 5 lines
Quoted 2 lines
In this case $* is clearer as that has already
been concatenated....loosing information about which word refers to which argument, yes.
It is building a string, so arguments are irrelevant.
Quoted 6 lines
Quoted 1 line
TL;DR: I consider this is still a bug in shellcheck. But if you rely on the tool as on the ruleset carved in stone, I will not die. Just a remark to myself "even honourable tools may also be broken".
If you think it is a bug then raise it with shellcheck.
I think you are conflating cases, and I agree with shellcheck on this one.
Cheers,
Kent.
Re: [PATCH libgpiod v2 2/4] tools: tests: use "$@" instead of $*
Quoted 5 lines · Kent Gibson
Quoted 4 lines · Andy Shevchenko
Quoted 3 lines · Kent Gibson
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Kent Gibson
On Mon, May 27, 2024 at 02:02:34PM +0200, Bartosz Golaszewski wrote:
...
Quoted 14 lines
Quoted 14 lines
Quoted 13 lines
Quoted 11 lines
Quoted 9 lines
tools/gpio-tools-test.bash GPIOSIM_APP_NAME="gpio-tools-test" · line 34
Quoted 6 lines
assert_fail(){tools/gpio-tools-test.bash +2
-2- $* || return 0 - fail " '$*': command did not fail as expected" + "$@" || return 0 + fail " '$@': command did not fail as expected" }Ironically, shellcheck doesn't like the '$@' in the fail string[1], so you should use $* there.
But why does it do like this?
Read the link[1].
Quoted 7 lines
Quoted 4 lines
Okay, this is only for some debug / error messages. Still if one wants to have clear understanding on what has been passed to some function, $* is not a correct option. Also note the single quotes, shouldn't that protect from the arguments loss?
That's right - I was only referring to this particular case where a string is being constructed. Wasn't that clear?
I meant that if you want to have this knowledge in the debug / error message, you will fail with $*, that's why I consider shellcheck is incorrect.
Ex.
I have
foo bar "baz bar2"
and I want
"ERROR: 'foo bar "baz bar2"' failed"
type of message.
AFAIU this is not what shellcheck wants. It want me to mange this to
"ERROR: 'foo bar baz bar2' failed"
Thanks, but no thanks to shellcheck.
Quoted 2 lines
The single quotes are within double quotes, so aren't they just part of the text in this context?
I don't remember by heard the shell expansion rules. I presumable that it might affect the inner argument on the recursive expansion.
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
Because $@ is an array being used to build a string, and that may not work the way you expect.
I think it's the opposite, $* works in a way I do not expect :-)
When passing arguments, sure.
Not when constructing strings.
Why not? This is pure puzzle to me why anybody wants the mangled string.
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
In this case $* is clearer as that has already
been concatenated....loosing information about which word refers to which argument, yes.
It is building a string, so arguments are irrelevant.
See above why I think it's relevant.
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
TL;DR: I consider this is still a bug in shellcheck. But if you rely on the tool as on the ruleset carved in stone, I will not die. Just a remark to myself "even honourable tools may also be broken".
If you think it is a bug then raise it with shellcheck.
I think you are conflating cases, and I agree with shellcheck on this one.
Okay.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH libgpiod v2 2/4] tools: tests: use "$@" instead of $*
Quoted 38 lines · Andy Shevchenko
Quoted 5 lines · Kent Gibson
Quoted 4 lines · Andy Shevchenko
Quoted 3 lines · Kent Gibson
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Kent Gibson
On Mon, May 27, 2024 at 02:02:34PM +0200, Bartosz Golaszewski wrote:
...
Quoted 14 lines
Quoted 14 lines
Quoted 13 lines
Quoted 11 lines
Quoted 9 lines
Review
Quoted 6 lines
assert_fail(){(quoted diff) +2
-2- $* || return 0 - fail " '$*': command did not fail as expected" + "$@" || return 0 + fail " '$@': command did not fail as expected" }Ironically, shellcheck doesn't like the '$@' in the fail string[1], so you should use $* there.
But why does it do like this?
Read the link[1].
Quoted 7 lines
Quoted 4 lines
Okay, this is only for some debug / error messages. Still if one wants to have clear understanding on what has been passed to some function, $* is not a correct option. Also note the single quotes, shouldn't that protect from the arguments loss?
That's right - I was only referring to this particular case where a string is being constructed. Wasn't that clear?
I meant that if you want to have this knowledge in the debug / error message, you will fail with $*, that's why I consider shellcheck is incorrect.
Ex.
I have
Quoted 8 lines
foo bar "baz bar2"
and I want
"ERROR: 'foo bar "baz bar2"' failed"
type of message.
Fair point, but $@ doesn't give you that either:
boo() {
echo "star '$*'"
echo "hash '$@'"
}boo foo bar "baz bar2"
gives:
star 'foo bar baz bar2'
hash 'foo bar baz bar2'
Is there any form that gives you the format you want?
Cheers,
Kent.
Re: [PATCH libgpiod v2 2/4] tools: tests: use "$@" instead of $*
Quoted 7 lines · Kent Gibson
Quoted 6 lines · Andy Shevchenko
Quoted 5 lines · Kent Gibson
Quoted 4 lines · Andy Shevchenko
Quoted 3 lines · Kent Gibson
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Kent Gibson
On Mon, May 27, 2024 at 02:02:34PM +0200, Bartosz Golaszewski wrote:
...
Quoted 45 lines
Quoted 29 lines
Quoted 14 lines
Quoted 14 lines
Quoted 13 lines
Quoted 11 lines
Quoted 9 lines
Review
Quoted 6 lines
assert_fail(){(quoted diff) +2
-2- $* || return 0 - fail " '$*': command did not fail as expected" + "$@" || return 0 + fail " '$@': command did not fail as expected" }Ironically, shellcheck doesn't like the '$@' in the fail string[1], so you should use $* there.
But why does it do like this?
Read the link[1].
Quoted 7 lines
Quoted 4 lines
Okay, this is only for some debug / error messages. Still if one wants to have clear understanding on what has been passed to some function, $* is not a correct option. Also note the single quotes, shouldn't that protect from the arguments loss?
That's right - I was only referring to this particular case where a string is being constructed. Wasn't that clear?
I meant that if you want to have this knowledge in the debug / error message, you will fail with $*, that's why I consider shellcheck is incorrect.
Ex.
I have
Quoted 8 lines
foo bar "baz bar2"
and I want
"ERROR: 'foo bar "baz bar2"' failed"
type of message.
Fair point, but $@ doesn't give you that either:
boo() { echo "star '$*'" echo "hash '$@'" }
Quoted 6 lines
boo foo bar "baz bar2"
gives:
star 'foo bar baz bar2'
hash 'foo bar baz bar2'
Oh, this is unfortunate. It seems entire model with quotation depends on the commands, printf makes it different, print -r -- makes it better, though, if one uses non-space IFS for it.
Quoted 1 line
Is there any form that gives you the format you want?
Yes, but it requires an iteration over arguments, roughly something like below (which is not yet what I want, but closer):
for a in "$@"; do echo -n '"$a" ' # echo -n seems not portable IIRC done echo
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH libgpiod v2 2/4] tools: tests: use "$@" instead of $*
Quoted 33 lines · Andy Shevchenko
Quoted 7 lines · Kent Gibson
Quoted 6 lines · Andy Shevchenko
Quoted 5 lines · Kent Gibson
Quoted 4 lines · Andy Shevchenko
Quoted 3 lines · Kent Gibson
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Kent Gibson
On Mon, May 27, 2024 at 02:02:34PM +0200, Bartosz Golaszewski wrote:
...
Quoted 9 lines
Quoted 1 line
Fair point, but $@ doesn't give you that either:
boo() { echo "star '$*'" echo "hash '$@'" }Quoted 6 lines
boo foo bar "baz bar2"
gives:
star 'foo bar baz bar2'
hash 'foo bar baz bar2'Oh, this is unfortunate. It seems entire model with quotation depends on the commands, printf makes it different, print -r -- makes it better, though, if one uses non-space IFS for it.
Quoted 1 line
Is there any form that gives you the format you want?
Quoted 8 lines
Yes, but it requires an iteration over arguments, roughly something like below (which is not yet what I want, but closer):
for a in "$@"; do echo -n '"$a" ' # echo -n seems not portable IIRC done echo
Ok, we're heading into the weeds here.
The issue isn't that shellcheck is wrong, it is that the error message is not formatted the way you would like, and fixing that requires writing a function to perform that formatting as bash can't do it out of the box. That isn't a huge issue, as we currently don't have any parameters containing whitespace, but it is something that might want to be addressed at some point.
Cheers,
Kent.
Re: [PATCH libgpiod v2 2/4] tools: tests: use "$@" instead of $*
Quoted 9 lines · Kent Gibson
Quoted 8 lines · Andy Shevchenko
Quoted 7 lines · Kent Gibson
Quoted 6 lines · Andy Shevchenko
Quoted 5 lines · Kent Gibson
Quoted 4 lines · Andy Shevchenko
Quoted 3 lines · Kent Gibson
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Kent Gibson
On Mon, May 27, 2024 at 02:02:34PM +0200, Bartosz Golaszewski wrote:
...
Quoted 31 lines
Quoted 22 lines
Quoted 9 lines
Quoted 1 line
Fair point, but $@ doesn't give you that either:
boo() { echo "star '$*'" echo "hash '$@'" }Quoted 6 lines
boo foo bar "baz bar2"
gives:
star 'foo bar baz bar2'
hash 'foo bar baz bar2'Oh, this is unfortunate. It seems entire model with quotation depends on the commands, printf makes it different, print -r -- makes it better, though, if one uses non-space IFS for it.
Quoted 1 line
Is there any form that gives you the format you want?
Quoted 8 lines
Yes, but it requires an iteration over arguments, roughly something like below (which is not yet what I want, but closer):
for a in "$@"; do echo -n '"$a" ' # echo -n seems not portable IIRC done echo
Quoted 7 lines
Ok, we're heading into the weeds here.
The issue isn't that shellcheck is wrong, it is that the error message is not formatted the way you would like, and fixing that requires writing a function to perform that formatting as bash can't do it out of the box. That isn't a huge issue, as we currently don't have any parameters containing whitespace, but it is something that might want to be addressed at some point.
Yeah, I have no objection now for using $* in the strings for reporting.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH libgpiod v2 2/4] tools: tests: use "$@" instead of $*
In reply to Kent GibsonQuoted 1 line · Kent Gibson
On Mon, May 27, 2024 at 02:02:34PM +0200, Bartosz Golaszewski wrote:
...
Quoted 2 lines
It also doesn't like looping on find results in patch 4[2], though that is not related to your change, so leave it and I'll fix it later?
Does it really mean _to fix_ rather than _to "fix"_? I mean how do we know that shellcheck is 100% correct tool and has no bugs?
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH libgpiod v2 2/4] tools: tests: use "$@" instead of $*
Quoted 11 lines · Andy Shevchenko
Quoted 1 line · Kent Gibson
On Mon, May 27, 2024 at 02:02:34PM +0200, Bartosz Golaszewski wrote:
...
Quoted 2 lines
It also doesn't like looping on find results in patch 4[2], though that is not related to your change, so leave it and I'll fix it later?
Does it really mean
_tofix_rather than_to"fix"_? I mean how do we know that shellcheck is 100% correct tool and has no bugs?
How do we know anything?
In this case you can read the description of the faults, which I had linked, and see if that makes sense to you. And we test the fixed code to ensure it still works as intended.
I'm not claiming shellcheck is fool-proof, or 100% correct, or 100% complete, but it is more available and repeatable than Andy's Eyeballs.
And if we do find bugs in it we can always fix those too.
As I stated earlier, if you have a better metric to use then I'm more than happy to compare, but so far shellcheck seems a reasonable option to me.
Cheers,
Kent.
Re: [PATCH libgpiod v2 2/4] tools: tests: use "$@" instead of $*
Quoted 3 lines · Kent Gibson
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Kent Gibson
On Mon, May 27, 2024 at 02:02:34PM +0200, Bartosz Golaszewski wrote:
...
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
It also doesn't like looping on find results in patch 4[2], though that is not related to your change, so leave it and I'll fix it later?
Does it really mean
_tofix_rather than_to"fix"_? I mean how do we know that shellcheck is 100% correct tool and has no bugs?How do we know anything?
Quoted 7 lines
In this case you can read the description of the faults, which I had linked, and see if that makes sense to you. And we test the fixed code to ensure it still works as intended.
I'm not claiming shellcheck is fool-proof, or 100% correct, or 100% complete, but it is more available and repeatable than Andy's Eyeballs.
And if we do find bugs in it we can always fix those too.
Sure, any tool has its own limitations. Esp. Andy's Eyeballs!
Quoted 2 lines
As I stated earlier, if you have a better metric to use then I'm more than happy to compare, but so far shellcheck seems a reasonable option to me.
No problem!
Signature · 2 lines
With Best Regards, Andy Shevchenko
PATCH v2 3/4 tools: tests: remove unneeded ';' in while loops
In reply to Bartosz GolaszewskiFrom: Bartosz Golaszewski <[email protected]>
We're already breaking the line between while and do so there's no need for the ';' character.
--- tools/gpio-tools-test.bash | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
tools/gpio-tools-test.bash +2 -2
@@ -211,7 +211,7 @@ dut_read_redirect() { dut_read() { local LINE lines=() - while read -t 0.2 -u ${COPROC[0]} LINE; + while read -t 0.2 -u ${COPROC[0]} LINE do if [ -n "$DUT_FIRST_CHAR" ] then
@@ -232,7 +232,7 @@ dut_flush() { lines=() output= unset DUT_FIRST_CHAR - while read -t 0 -u ${COPROC[0]} JUNK; + while read -t 0 -u ${COPROC[0]} JUNK do read -t 0.1 -u ${COPROC[0]} JUNK || true done
Signature · 1 line
2.43.0
PATCH v2 4/4 tools: tests: remove dependency on grep
In reply to Bartosz GolaszewskiFrom: Bartosz Golaszewski <[email protected]>
We only use grep in one place where we can use the -regex option of find instead. Switch to using find --regex and drop grep entirely from the tools test-suite.
--- tools/gpio-tools-test.bash | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
tools/gpio-tools-test.bash +1 -2
@@ -162,7 +162,7 @@ gpiosim_cleanup() { ls $BANKPATH/line* > /dev/null 2>&1 if [ "$?" = "0" ] then - for LINE in $(find $BANKPATH/ | grep -E "line[0-9]+$") + for LINE in $(find $BANKPATH/ -regex ".*line[0-9]+$") do test -e $LINE/hog && rmdir $LINE/hog rmdir $LINE
@@ -3078,7 +3078,6 @@ check_prog() { check_prog shunit2 check_prog modprobe check_prog timeout -check_prog grep # Check if we're running a kernel at the required version or later check_kernel $MIN_KERNEL_VERSION
Signature · 1 line
2.43.0
Re: [PATCH libgpiod v2 4/4] tools: tests: remove dependency on grep
Quoted 4 lines · Bartosz Golaszewski
From: Bartosz Golaszewski <[email protected]>
We only use grep in one place where we can use the -regex option of find instead. Switch to using find --regex and drop grep entirely from the
-regex
Quoted 1 line
tools test-suite.
Signature · 2 lines
With Best Regards, Andy Shevchenko