0

I'm trying to understand this bash code but I'm pretty new on this. I'm not sure how to interpreter the next snippet. In more specific way I have doubts with the if "@"; then line.

check() {
    LABEL=$1
    shift
    echo -e "\n Testing $LABEL"
    if "$@"; then 
        echo "✅  Passed!"
        return 0
    else
        echoStderr "❌ $LABEL check failed."
        FAILED+=("$LABEL")
        return 1
    fi
}

I think that is just like the python list syntax to test an empty list the if will be success when the command tested return something. But I have doubts because bash use a lot error signals and I may be missing something. The use case is:

check "distro" lsb_release -c
check "color" [ $(cat /tmp/color.txt | grep red) ]

The snippeds were taken from this repository

related question: What does mean $@ in bash script

StandardIO
  • 156
  • 1
  • 7
  • 3
    That runs the command stored in `$@`, and if the result is successful (exitcode is 0) it runs the first part of the `if` statement; if the exit code is non-zero, it runs the `else` portion. – larsks Jan 30 '23 at 18:49
  • As a special case, if there are no arguments, the expansion of `"$@"` is empty and such a null command is defined to succeed. – chepner Jan 30 '23 at 18:50
  • 3
    The second use case is fragile. What `check` receives as arguments is the *result* of the command substitution, not the command substitution itself. (Further, if the command substitution produces more than one result, `[` is likely to fail with a syntax error.) – chepner Jan 30 '23 at 18:53
  • As for the function itself, see http://mywiki.wooledge.org/BashFAQ/050. – chepner Jan 30 '23 at 19:08
  • I was totally wrong. I am grateful for your answers. – StandardIO Jan 30 '23 at 19:10
  • @larsks You might want to convert that to an answer so this can be marked as solved. – konsolebox Jan 30 '23 at 19:33
  • Extending chepner's comment: `check "color" grep -q red /tmp/color.txt` would be more robust – glenn jackman Jan 30 '23 at 20:55

0 Answers0