On 20.11.2021 02:20, Kenny McCormack wrote:
I use shellcheck to check over my bash shell scripts. Most of what it generates is noise, but every so often, it produces something useful - something that should be fixed/cleaned up.
I recently added some code to the script and ran shellcheck on it, and it generated the output below:
--- Cut here ---
In myScript line 145:
[[ "$REPLY" =~ ^\^ ]] && REPLY="title not like '%${REPLY:1}%'"
^-- SC1001: This \^ will be a regular '^' in this context.
^-- SC2089: Quotes/backslashes will be treated literally. Use an array.
--- Cut here ---
The intent is to check to see if $REPLY starts with a ^ and if so, change
it to the indicated string (yes, I am building up a SQL query). In my testing, I determined that the right syntax in a Bash =~ test, to check for
a leading ^, is ^\^. So, what is the SC1001 complaining about? Isn't that the right syntax to use?
My interpretation is that ^^ is sufficient since there's just one
single ^ to indicate the start of the line and the second ^ thus
not interpreted as another start of line but taken literally.
I read the message just as suggestion to simplify the expression.
Apropos simplifying; I'd probably use the simpler shell patterns
here [[ $REPLY == ^* ]] && ... instead of regexp (looks more straightforward to me without explicit anchor; YMMV).
Second, I don't understand the second error at all. What does "Use an
array" mean?
The tool seems to have got confused and provides a suggestion for
a non-existing problem, maybe?
Note that the script works fine. There's nothing really wrong with it at all.
Looks quite fine to me.
Janis
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)